module RuGUI::ObservablePropertySupport::ClassMethods

Public Instance Methods

core_observable_properties() click to toggle source

Returns the names of core observable properties for this class.

# File lib/rugui/observable_property_support.rb, line 166
def core_observable_properties
  core_observable_properties = []
  observable_properties_options.each do |property, options|
    core_observable_properties << property if options[:core] == true
  end
  core_observable_properties
end
create_class_inheritable_attributes() click to toggle source

Creates the necessary class inheritable attributes an initializes them.

# File lib/rugui/observable_property_support.rb, line 121
def create_class_inheritable_attributes
  self.class_inheritable_accessor :observable_properties_options

  self.observable_properties_options = {}
end
observable_properties() click to toggle source

Returns the names of all observable properties for this class.

# File lib/rugui/observable_property_support.rb, line 175
def observable_properties
  observable_properties_options.keys
end
observable_property(property, options = {}) click to toggle source

Register a observable properties for this model.

Properties may be given as symbols, or strings. You can pass some options, in a hash, which will be used when the observable is created:

  • initial_value: The initial value for the property. This value will

be set when the observable instance is initialized (i.e., when the initialize method is called). Defaults to nil.

  • reset_value: The reset value for the property. This value will be

set when the observable instance is reset (i.e., when the reset! method is called). If this is not given, the initial_value will be used instead.

  • core: Defines whether the property should be used when comparing two

observables. Defaults to false.

  • prevent_reset: If this is true the property will not be

reseted. Defaults to false.

  • boolean: If this is true a “question” method will be

created for the property (i.e., for a property named foo a method named foo? will be created).

Examples:

class MyObservable
  include RuGUI::ObservablePropertySupport

  observable_property :foo, :initial_value => "bar"
  observable_property :bar, :initial_value => "foo", :reset_value => "bar"
  observable_property :core_property, :core => true
  observable_property :non_resetable_property, :prevent_reset => true

  # And so on...
end
# File lib/rugui/observable_property_support.rb, line 159
def observable_property(property, options = {})
  create_observable_property_options(property, options)
  create_observable_property_accessors(property)
  create_observable_property_boolean_readers(property, options)
end

Private Instance Methods

create_observable_property_accessors(property) click to toggle source
# File lib/rugui/observable_property_support.rb, line 184
        def create_observable_property_accessors(property)
          self.class_eval <<-class_eval
            def #{property}
              @#{property}
            end

            def #{property}=(value)
              old_value = clone_if_possible(@#{property})
              if has_changed?(value, old_value)
                @#{property} = ObservablePropertyProxy.new(value, self, '#{property}')
                property_changed('#{property}', value, old_value)
              end
            end
          class_eval
        end
create_observable_property_boolean_readers(property, options) click to toggle source
# File lib/rugui/observable_property_support.rb, line 200
        def create_observable_property_boolean_readers(property, options)
          if options[:boolean]
            self.class_eval <<-class_eval
              def #{property}?
                self.#{property} == true
              end
            class_eval
          end
        end
create_observable_property_options(property, options = {}) click to toggle source
# File lib/rugui/observable_property_support.rb, line 180
def create_observable_property_options(property, options = {})
  self.observable_properties_options[property.to_sym] = prepare_options(options)
end
default_options() click to toggle source
# File lib/rugui/observable_property_support.rb, line 218
def default_options
  { :core => false,
    :initial_value => nil,
    :reset_value => nil }
end
prepare_options(options) click to toggle source
# File lib/rugui/observable_property_support.rb, line 210
def prepare_options(options)
  options = default_options.merge(options)
  if options[:reset_value].nil? and not options[:initial_value].class.include?(ObservablePropertySupport)
    options[:reset_value] = options[:initial_value]
  end
  options
end