lib/plumb/pipeline.rb in plumb-0.0.4 vs lib/plumb/pipeline.rb in plumb-0.0.5

- old
+ new

@@ -17,16 +17,32 @@ def call(result) @block.call(@step, result) end end + class << self + def around_blocks + @around_blocks ||= [] + end + + def around(callable = nil, &block) + around_blocks << (callable || block) + self + end + + def inherited(subclass) + around_blocks.each { |block| subclass.around(block) } + super + end + end + attr_reader :children def initialize(type = Types::Any, &setup) @type = type @children = [type].freeze - @around_blocks = [] + @around_blocks = self.class.around_blocks.dup return unless block_given? configure(&setup) freeze end @@ -40,11 +56,12 @@ unless is_a_step?(callable) raise ArgumentError, "#step expects an interface #call(Result) Result, but got #{callable.inspect}" end - callable = @around_blocks.reduce(callable) { |cl, bl| AroundStep.new(cl, bl) } if @around_blocks.any? + callable = prepare_step(callable) + callable = @around_blocks.reverse.reduce(callable) { |cl, bl| AroundStep.new(cl, bl) } if @around_blocks.any? @type >>= callable self end def around(callable = nil, &block) @@ -68,7 +85,9 @@ def is_a_step?(callable) return false unless callable.respond_to?(:call) true end + + def prepare_step(callable) = callable end end