Class: Rubydora::ArrayWithCallback
- Inherits:
-
Array
- Object
- Array
- Rubydora::ArrayWithCallback
- Defined in:
- lib/rubydora/array_with_callback.rb
Overview
This is an attempt to implement an Array-like object that calls a method after data is modified
Instance Method Summary collapse
-
#call_on_change(changes = {}) ⇒ Object
duck-typing Rubydora::Callbacks call_* methods.
-
#method ⇒ Object
FIXME: It would be nice to use Rubydora::Callbacks here, however, this method requires instance-level callbacks.
-
#on_change ⇒ Object
duck-typing Rubydora::Callbacks @hooks and accessor.
Instance Method Details
#call_on_change(changes = {}) ⇒ Object
duck-typing Rubydora::Callbacks call_* methods
28 29 30 31 32 |
# File 'lib/rubydora/array_with_callback.rb', line 28 def call_on_change changes = {} self.on_change.each do |h| h.call(self, changes) end end |
#method ⇒ Object
FIXME: It would be nice to use Rubydora::Callbacks here, however, this method requires instance-level callbacks
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rubydora/array_with_callback.rb', line 10 [:<<, :collect!, :map!, :compact!, :concat, :delete, :delete_at, :delete_if, :pop, :push, :reject!, :replace, :select!, :[]=, :slice!, :uniq! ].each do |method| class_eval <<-RUBY def #{method.to_s} *args, &blk old = self.dup res = super(*args, &blk) call_on_change({:+ => self - old, :- => old - self}) res end RUBY end |
#on_change ⇒ Object
duck-typing Rubydora::Callbacks @hooks and accessor
22 23 24 25 |
# File 'lib/rubydora/array_with_callback.rb', line 22 def on_change @hooks ||= {} @hooks[:on_change] ||= [] end |