Module: RubyExt::Callbacks

Defined in:
lib/ruby_ext/more/callbacks.rb

Defined Under Namespace

Modules: ClassMethods Classes: AbstractCallback, AfterCallback, AroundCallback, BeforeCallback

Instance Method Summary collapse

Instance Method Details

#run_after_callbacks(name, method, data = {}) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/ruby_ext/more/callbacks.rb', line 158

def run_after_callbacks name, method, data = {}
  name.must.be_a Symbol
  self.class.callbacks[name].try :each do |callback|
    if callback.is_a?(AfterCallback) and callback.run?(self, method, data)
      callback.run(self, data)
    end
  end
end

#run_before_callbacks(name, method, data = {}) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/ruby_ext/more/callbacks.rb', line 148

def run_before_callbacks name, method, data = {}
  name.must.be_a Symbol
  self.class.callbacks[name].try :each do |callback|
    if callback.is_a?(BeforeCallback) and callback.run?(self, method, data)
      return false unless callback.run(self, data)
    end
  end
  true
end

#run_callbacks(name, method, data = {}, &block) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ruby_ext/more/callbacks.rb', line 167

def run_callbacks name, method, data = {}, &block
  name.must.be_a Symbol
  run_callbacks_once name, method, block do
    if callbacks = self.class.callbacks[name]
      chain = block || -> {}
      chain = callbacks.reverse.reduce chain do |chain, callback|
        callback.build_block self, method, data, &chain
      end
      chain.call
    else
      block.call if block
    end
  end
end