Class: RuGUI::ObservablePropertyProxy

Inherits:
BaseObject show all
Includes:
LogSupport
Defined in:
lib/rugui/observable_property_proxy.rb

Overview

A proxy class for observable properties.

When creating an ObservablePropertyProxy you pass an instance of any object to it (which will now be the context for this proxy), the observable and the property name.

The ObservablePropertyProxy instance will work as a proxy for each method send to the context. If the context is changed, it will notify any PropertyObservers registered for its observable, by calling their property_changed method.

CAUTION: When using observable string properties as keys in a Hash make sure you call the Object#to_s or Object#to_sym methods before putting the property value as key. Hashes uses the method Object#eql? when comparing keys, and for some unknown reason it is always returning false when comparing observable string properties.

Instance Method Summary collapse

Methods included from LogSupport

included, #logger

Methods inherited from BaseObject

#inspect

Methods included from FrameworkAdapters::FrameworkAdapterSupport

#framework_adapter_for, included, #load_framework_adapter

Constructor Details

#initialize(context, observable, property) ⇒ ObservablePropertyProxy

Returns a new instance of ObservablePropertyProxy.



23
24
25
26
27
# File 'lib/rugui/observable_property_proxy.rb', line 23

def initialize(context, observable, property)
  @context = context
  @observable = observable
  @property = property
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)

Here we reimplement the method missing, adding code to notify observers when the property has changed, i.e., when the context before calling the method is different than the context after the method is called.



47
48
49
50
51
52
53
# File 'lib/rugui/observable_property_proxy.rb', line 47

def method_missing(method, *args, &block)
  old_context = get_context_copy
  return_value = @context.send(method, *args, &block)

  context_changed(@context, old_context) unless @context == old_context
  return_value
end