Module: Properties
- Defined in:
- lib/sugar-high/properties.rb
Overview
Class Method Summary collapse
Instance Method Summary collapse
- #is(test) ⇒ Object
-
#property(sym, predicate = nil) ⇒ Object
def property(sym, &predicate).
Class Method Details
.extended(base) ⇒ Object
4 5 6 7 8 9 10 11 |
# 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 |
Instance Method Details
#is(test) ⇒ Object
37 38 39 |
# File 'lib/sugar-high/properties.rb', line 37 def is(test) lambda {|val| test === val } end |
#property(sym, predicate = nil) ⇒ Object
def property(sym, &predicate)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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 |