Class: MongoMapper::Plugins::Callbacks::CallbackChain

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Array

from_mongo, to_mongo, #to_xml

Class Method Details

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



120
121
122
123
124
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 120

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



153
154
155
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 153

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

#find(callback, &block) ⇒ Object



149
150
151
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 149

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

#replace_or_append!(chain) ⇒ Object

TODO: Decompose into more Array like behavior



140
141
142
143
144
145
146
147
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 140

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

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



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 126

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