Files

Properties

From: www.infoq.com/articles/properties-metaprogramming

Public Class Methods

extended(base) click to toggle source
# File lib/sugar-high/properties.rb, line 4
def self.extended(base)
  base.class_eval %{
    def fire_event_for(sym, arg)
      return if !@listener[sym]
      @listener[sym].each {|l| l.call(arg) }
    end
  }
end

Public Instance Methods

is(test) click to toggle source
# File lib/sugar-high/properties.rb, line 37
def is(test)
  lambda {|val| test === val }
end
property(sym, predicate=nil) click to toggle source

def property(sym, &predicate)

# File lib/sugar-high/properties.rb, line 14
def property(sym, predicate=nil)
  define_method(sym) do
    instance_variable_get("@#{sym}")
  end

  define_method("#{sym}=") do |arg|
    return if !predicate.call(arg) if predicate
    instance_variable_set("@#{sym}", arg)
    fire_event_for(sym, arg)
  end

  define_method("add_#{sym}_listener") do |x|
    @listener ||= {}
    @listener[sym] ||= []
    @listener[sym] << x
  end

  define_method("remove_#{sym}_listener") do |x|
    return if !@listener[sym]
    @listener[sym].delete_at(x)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.