Class: Interphase::Helpers::Observable
- Inherits:
- BasicObject
- Defined in:
- lib/interphase/helpers/observable.rb
Overview
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.
Instance Method Summary collapse
-
#initialize(object, &block) ⇒ Observable
constructor
Wrap an object in a new instance of
Observable. -
#method_missing(name, *args, &block) ⇒ Object
TODO responds_to?.
- #respond_to_missing? ⇒ Boolean
Constructor Details
#initialize(object, &block) ⇒ Observable
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.
15 16 17 18 19 20 21 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
TODO responds_to?
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# 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 |
Instance Method Details
#respond_to_missing? ⇒ Boolean
38 39 40 |
# File 'lib/interphase/helpers/observable.rb', line 38 def respond_to_missing?(*) true end |