Class: MotionSupport::Callbacks::CallbackChain

Inherits:
Array show all
Defined in:
motion/callbacks.rb

Overview

An Array with a compile method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#deep_dup, #extract_options!, #from, #has_hash_key?, #has_hash_value?, #in_groups, #in_groups_of, #reverse_each, #second, #split, #to, #to_formatted_s, #to_param, #to_query, #to_sentence, wrap

Constructor Details

#initialize(name, config) ⇒ CallbackChain


245
246
247
248
249
250
251
# File 'motion/callbacks.rb', line 245

def initialize(name, config)
  @name = name
  @config = {
    :terminator => lambda { |result| false },
    :scope => [ :kind ]
  }.merge!(config)
end

Instance Attribute Details

#configObject (readonly)

:nodoc:#


243
244
245
# File 'motion/callbacks.rb', line 243

def config
  @config
end

#nameObject (readonly)

:nodoc:#


243
244
245
# File 'motion/callbacks.rb', line 243

def name
  @name
end

Instance Method Details

#append(*callbacks) ⇒ Object


273
274
275
# File 'motion/callbacks.rb', line 273

def append(*callbacks)
  callbacks.each { |c| append_one(c) }
end

#compileObject


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'motion/callbacks.rb', line 253

def compile
  lambda do |obj, &block|
    value = nil
    halted = false

    callbacks = lambda do |obj, value, halted|
      value = !halted && (block.call if block)
      [value, halted]
    end
    
    reverse_each do |callback|
      callbacks = callback.apply(callbacks)
    end
    
    value, halted = *(callbacks.call(obj, value, halted))

    value
  end
end

#prepend(*callbacks) ⇒ Object


277
278
279
# File 'motion/callbacks.rb', line 277

def prepend(*callbacks)
  callbacks.each { |c| prepend_one(c) }
end