Class: Stone::Callbacks

Inherits:
Hash
  • Object
show all
Defined in:
lib/stone/callbacks.rb

Constant Summary collapse

CALLBACKS =
[
:before_save,
:after_save,
:before_create,
:after_create,
:before_destroy,
:after_destroy
]

Instance Method Summary collapse

Instance Method Details

#fire(cb_sym, obj) ⇒ Object

Sends any methods registered under cb_sym to obj

Parameters

cb_sym

Used to retrieve the methods to send

obj

The object to which the retrieved methods are sent



41
42
43
44
45
46
47
48
# File 'lib/stone/callbacks.rb', line 41

def fire(cb_sym, obj)
  unless obj.class.to_s.make_key == :class
    self[obj.class.to_s.make_key][cb_sym].each do |meth|
      obj.send(meth) unless meth.blank?
    end
  end
  true
end

#register(cb_sym, meth, klass) ⇒ Object

Adds a given meth to the cb_sym array in the current Callbacks hash based on klass

Parameters

cb_sym

where meth will be added

meth

method to be added

klass

determines which cb_sym array will be used



33
34
35
# File 'lib/stone/callbacks.rb', line 33

def register(cb_sym, meth, klass)
  self[klass.to_s.make_key][cb_sym] << meth
end

#register_klass(klass) ⇒ Object

Registers the klass with the current instance of Callbacks in Resource

Parameters

klass

The class to be registered



20
21
22
23
24
25
# File 'lib/stone/callbacks.rb', line 20

def register_klass(klass)
  self[klass.to_s.make_key] = {}
  CALLBACKS.each do |cb_sym|
    self[klass.to_s.make_key][cb_sym] = []
  end
end