Class Delayed::Callback
In: lib/delayed/lifecycle.rb
Parent: Object

Methods

add   execute   new  

Public Class methods

[Source]

    # File lib/delayed/lifecycle.rb, line 55
55:     def initialize
56:       @before = []
57:       @after = []
58:       
59:       # Identity proc. Avoids special cases when there is no existing around chain.
60:       @around = lambda { |*args, &block| block.call(*args) }
61:     end

Public Instance methods

[Source]

    # File lib/delayed/lifecycle.rb, line 70
70:     def add(type, &callback)      
71:       case type
72:       when :before
73:         @before << callback
74:       when :after
75:         @after << callback
76:       when :around
77:         chain = @around # use a local variable so that the current chain is closed over in the following lambda
78:         @around = lambda { |*a, &block| chain.call(*a) { |*b| callback.call(*b, &block) } }
79:       else
80:         raise InvalidCallback, "Invalid callback type: #{type}"
81:       end
82:     end

[Source]

    # File lib/delayed/lifecycle.rb, line 63
63:     def execute(*args, &block)      
64:       @before.each { |c| c.call(*args) }
65:       result = @around.call(*args, &block)
66:       @after.each { |c| c.call(*args) }
67:       result
68:     end

[Validate]