class Interphase::Helpers::Observable

An object wrapper which can invoke a method whenever the wrapped object changes. Unlike most classes, this class inherits from `BasicObject`, which has absolutely no methods whatsoever. This means that everything, even inspect and class, fall into +Observable#method_missing+.

Public Class Methods

new(object, &block) click to toggle source

Wrap an object in a new instance of Observable. Takes a block which executes upon the object changing. This block is passed object as a paremeter.

object

The object to wrap.

# File lib/interphase/helpers/observable.rb, line 15
def initialize(object, &block)
  # :: is required because we inherit BasicObject, not Object
  ::Kernal.raise ::ArgumentError, 'Requires a block' if block.nil?

  @object = object
  @on_change = block
end

Public Instance Methods

method_missing(name, *args, &block) click to toggle source

TODO responds_to?

Calls superclass method
# File lib/interphase/helpers/observable.rb, line 24
def method_missing(name, *args, &block)
  if @object.respond_to?(name)
    before_hash = @object.hash
    ret_val = @object.send(name, *args, &block)
    after_hash = @object.hash

    @on_change.call(@object) if before_hash != after_hash

    ret_val
  else
    super
  end
end
respond_to_missing?(*) click to toggle source
# File lib/interphase/helpers/observable.rb, line 38
def respond_to_missing?(*)
  true
end