Class: ObservableObject::Wrapper

Inherits:
BasicObject
Defined in:
lib/observable_object.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj, methods, deep, notifier = nil, &event) ⇒ Wrapper

Returns a new instance of Wrapper.



86
87
88
89
90
91
# File 'lib/observable_object.rb', line 86

def initialize(obj,methods,deep,notifier=nil,&event)
  @deep = deep
  @notifier = notifier || Notifier.new(self,&event)
  @watcher = Watcher::create(obj,methods)
  @obj = @deep ? DeepWrap::map_obj(obj,@notifier) : obj
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mname, *args, &block) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/observable_object.rb', line 114

def method_missing(mname,*args,&block)
  @watcher.remember { @obj }

  res = @obj.__send__(mname,*args,&block)
  chain = @obj.equal?(res)                # did the wrapped object return itself from this method?
  
  if @watcher.is_state_changing(@obj,mname)
    @obj = DeepWrap::map_obj(@obj,@notifier) if @deep # remap; some nested objects could have changed
    @notifier.call 
  end
  
  chain ? self : res                      # for chaining return self when the underlying object returns self
end

Instance Method Details

#!Object



102
103
104
# File 'lib/observable_object.rb', line 102

def !
  !@obj
end

#!=(other) ⇒ Object



99
100
101
# File 'lib/observable_object.rb', line 99

def !=(other)
  @obj != other
end

#==(other) ⇒ Object



93
94
95
# File 'lib/observable_object.rb', line 93

def ==(other)
  @obj == other
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/observable_object.rb', line 96

def eql?(other)
  @obj.eql?(other)
end

#respond_to?(mname) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/observable_object.rb', line 111

def respond_to?(mname)
  @obj.respond_to?(mname)
end