Class: Wonkavision::Plugins::Callbacks::CallbackChain

Inherits:
Array
  • Object
show all
Defined in:
lib/wonkavision/plugins/callbacks.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(kind, *methods, &block) ⇒ Object



62
63
64
65
66
# File 'lib/wonkavision/plugins/callbacks.rb', line 62

def self.build(kind, *methods, &block)
  methods, options = extract_options(*methods, &block)
  methods.map! { |method| Callback.new(kind, method, options) }
  new(methods)
end

Instance Method Details

#delete(callback) ⇒ Object



94
95
96
# File 'lib/wonkavision/plugins/callbacks.rb', line 94

def delete(callback)
  super(callback.is_a?(Callback) ? callback : find(callback))
end

#find(callback, &block) ⇒ Object



90
91
92
# File 'lib/wonkavision/plugins/callbacks.rb', line 90

def find(callback, &block)
  select { |c| c == callback && (!block_given? || yield(c)) }.first
end

#replace_or_append!(chain) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/wonkavision/plugins/callbacks.rb', line 81

def replace_or_append!(chain)
  if index = index(chain)
    self[index] = chain
  else
    self << chain
  end
  self
end

#run(object, options = {}, &terminator) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/wonkavision/plugins/callbacks.rb', line 68

def run(object, options={}, &terminator)
  enumerator = options[:enumerator] || :each

  unless block_given?
    send(enumerator) { |callback| callback.call(object) }
  else
    send(enumerator) do |callback|
      result = callback.call(object)
      break result if terminator.call(result, object)
    end
  end
end