Module: PopulateMe::DocumentMixins::Callbacks::ClassMethods

Defined in:
lib/populate_me/document_mixins/callbacks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#callbacksObject

Returns the value of attribute callbacks.



95
96
97
# File 'lib/populate_me/document_mixins/callbacks.rb', line 95

def callbacks
  @callbacks
end

Instance Method Details

#after(name, item = nil, options = {}, &block) ⇒ Object



116
117
118
# File 'lib/populate_me/document_mixins/callbacks.rb', line 116

def after name, item=nil, options={}, &block
  register_callback "after_#{name}", item, options, &block
end

#before(name, item = nil, options = {}, &block) ⇒ Object



112
113
114
# File 'lib/populate_me/document_mixins/callbacks.rb', line 112

def before name, item=nil, options={}, &block
  register_callback "before_#{name}", item, options, &block
end

#register_callback(name, item = nil, options = {}, &block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/populate_me/document_mixins/callbacks.rb', line 97

def register_callback name, item=nil, options={}, &block
  name = name.to_sym
  if block_given?
    options = item || {}
    item = block
  end
  self.callbacks ||= {}
  self.callbacks[name] ||= []
  if options[:prepend]
    self.callbacks[name].unshift item
  else
    self.callbacks[name] << item
  end
end