Sha256: aa4ea9d437e92143ef75f6d653704a92676c8007e0e6052dcd1d2c070a678d0a
Contents?: true
Size: 1010 Bytes
Versions: 70
Compression:
Stored size: 1010 Bytes
Contents
module Rubydora ## # This is an attempt to implement an Array-like # object that calls a method after data is modified class ArrayWithCallback < Array ## # FIXME: It would be nice to use Rubydora::Callbacks here, # however, this method requires instance-level callbacks [:<<, :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 # duck-typing Rubydora::Callbacks @hooks and accessor def on_change @hooks ||= {} @hooks[:on_change] ||= [] end # duck-typing Rubydora::Callbacks call_* methods def call_on_change changes = {} self.on_change.each do |h| h.call(self, changes) end end end end
Version data entries
70 entries across 70 versions & 1 rubygems