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

Class Method Details

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



115
116
117
118
119
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 115

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



146
147
148
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 146

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

#find(callback, &block) ⇒ Object



142
143
144
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 142

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



133
134
135
136
137
138
139
140
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 133

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

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



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/mongo_mapper/plugins/callbacks.rb', line 121

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