class RuGUI::ObservablePropertyProxy

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.

Constants

NON_DELEGATABLE_METHODS

Public Class Methods

new(context, observable, property) click to toggle source
# File lib/rugui/observable_property_proxy.rb, line 23
def initialize(context, observable, property)
  @context = context
  @observable = observable
  @property = property
end

Private Instance Methods

context_changed(new_value, old_value) click to toggle source

Called when the context has changed.

Notifies all registered observers

# File lib/rugui/observable_property_proxy.rb, line 69
def context_changed(new_value, old_value)
  @observable.property_changed(@property, new_value, old_value)
end
get_context_copy() click to toggle source

Returns a copy of the context.

# File lib/rugui/observable_property_proxy.rb, line 56
def get_context_copy
  begin
    return @context.clone
  rescue TypeError
    return @context
  end
end
method_missing(method, *args, &block) click to toggle source

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.

# 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