design patterns - Implement Apect-oriented-like nested around filters with Ruby? -
I am trying to write a class that supports nested filters without starting the nested-oriented library.
class Foo attr_accessor: around_filter def initialf #filters The following one wrappers are those that are interesting arguments with #vanilla do-nothing filter @around_filter = lambda {yield} # or lambda {| & Amp; Blk | Blk.call} executes the filter around the end valve @ times, though it is deeply nested, then "meti code" @ around_filter.call #meter code here puts 'end' end end # and hopefully i That's just point to the filter at the top, recurse def add_around_filter (& amp; blk) before_around_filter = @around_filter @around_filter = #? Secret code here ?? Pre_Arand_Filter End End refers to
to be able to add filter to any number of targets:
foo = Foo.new foo.add_around_filter 'Strawberry' yield 'blueberry' and foul puts foo.add_around_filter does puts 'orange' yield 'grapes' end foo.bar # = & gt; Orange, Strawberries, Curries, Blueberries, Grapes
I know that there is a bunch of holes in the example. I have just written so that the general direction is to covet, to disassemble with a very big genuine square.
Although I like the syntax of yield
, I do not oppose blocking references:
foo.add_around_filter do | And Black | The 'orange' blk.call puts the 'grapes' end
I just got the job around the filter. I tried a lot of things with the nesting hunt, but never puzzleped down. If you have any solution, then I appreciate it a bunch!
yield
will try to produce defined blocks in your filter The issue of their definition, what you want. This will work with clear blocking form (in 1.9):
square fu attr_accessor: around_filter def to start #filters which have the following one wrap with interesting arguments #vanilla do-nothing filter @ Round_filter = Pro.Nue {| & Amp; Black | Blk.call} executes the filter around the end valve @ times, though it is deeply nested, then "meti code" @ around_filter.call #meter code here puts 'end' end end # and hopefully i That is to indicate the filter at the top only, recurse def add_around_filter (& amp; filter) pre_ound_filter = @ round_filter @ round_filter = Proc.new do & amp; Blk | Filter.call do the pre_awound_filters.color (& amp; blk) end-end end end foo = Foo.new foo.add_around_filter do | & Amp; Blk | Puts 'strawberries' blk.call says 'blueberries' end foo.add_around_filter do & amp; Black | Puts 'orange' blk.call 'grapes' end foo.bar # = & gt; Orange, strawberry, curry, blueberries, grapes
Comments
Post a Comment