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

Version Path
rubydora-1.2.1 lib/rubydora/array_with_callback.rb
rubydora-1.2.0 lib/rubydora/array_with_callback.rb
rubydora-1.1.2 lib/rubydora/array_with_callback.rb
rubydora-1.1.1 lib/rubydora/array_with_callback.rb
rubydora-1.1.0 lib/rubydora/array_with_callback.rb
rubydora-1.0.2 lib/rubydora/array_with_callback.rb
rubydora-1.0.1 lib/rubydora/array_with_callback.rb
rubydora-1.0.0 lib/rubydora/array_with_callback.rb
rubydora-0.8.0 lib/rubydora/array_with_callback.rb
rubydora-0.7.0 lib/rubydora/array_with_callback.rb
rubydora-0.5.13 lib/rubydora/array_with_callback.rb
rubydora-0.5.12 lib/rubydora/array_with_callback.rb
rubydora-0.5.11 lib/rubydora/array_with_callback.rb
rubydora-0.5.10 lib/rubydora/array_with_callback.rb
rubydora-0.5.9 lib/rubydora/array_with_callback.rb
rubydora-0.5.8 lib/rubydora/array_with_callback.rb
rubydora-0.5.7 lib/rubydora/array_with_callback.rb
rubydora-0.5.6 lib/rubydora/array_with_callback.rb
rubydora-0.5.5 lib/rubydora/array_with_callback.rb
rubydora-0.5.4 lib/rubydora/array_with_callback.rb