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 included from Extensions::Array

#from_mongo, #to_mongo

Class Method Details

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



122
123
124
125
126
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 122

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



155
156
157
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 155

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

#find(callback, &block) ⇒ Object



151
152
153
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 151

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



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

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

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



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

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