module Woyo::Attributes
Public Instance Methods
attribute(*attrs, &block)
click to toggle source
# File lib/woyo/world/attributes.rb, line 73 def attribute *attrs, &block attributes *attrs, &block end
attributes(*attrs, &block)
click to toggle source
# File lib/woyo/world/attributes.rb, line 77 def attributes *attrs, &block @attributes ||= Woyo::Attributes::AttributesHash.new return @attributes if attrs.empty? attrs.each do |attr| case when attr.kind_of?( Hash ) attr.each do |attr_sym,default| define_attr_methods attr_sym, default @attributes[attr_sym] = send "#{attr_sym}_default" end when block define_attr_methods attr, block @attributes[attr] = send "#{attr}_default" else unless @attributes.include? attr define_attr_methods attr @attributes[attr] = nil end end end end
changes()
click to toggle source
# File lib/woyo/world/attributes.rb, line 49 def changes @changes ? @changes.merge!( dependent_changes ) : nil end
clear_changes()
click to toggle source
# File lib/woyo/world/attributes.rb, line 69 def clear_changes @changes.clear end
define_attr(attr)
click to toggle source
# File lib/woyo/world/attributes.rb, line 121 def define_attr attr define_singleton_method attr do |arg = nil| return @attributes[attr] = arg unless arg.nil? case when @attributes[attr].kind_of?( Hash ) truthy_matches = @attributes[attr].collect do |name,value| truthy = if @attributes[name].respond_to?( :call ) @attributes[name].arity == 0 ? @attributes[name].call : @attributes[name].call(self) else @attributes[name] end truthy ? value : nil end.compact truthy_matches = truthy_matches.count == 1 ? truthy_matches[0] : truthy_matches return truthy_matches when @attributes[attr].respond_to?( :call ) return @attributes[attr].arity == 0 ? @attributes[attr].call : @attributes[attr].call(self) else @attributes[attr] end end end
define_attr!(attr)
click to toggle source
# File lib/woyo/world/attributes.rb, line 150 def define_attr! attr define_singleton_method "#{attr}!" do send "#{attr}=", true end end
define_attr?(attr)
click to toggle source
# File lib/woyo/world/attributes.rb, line 144 def define_attr? attr define_singleton_method "#{attr}?" do ( send attr ) ? true : false end end
define_attr_default(attr, default)
click to toggle source
# File lib/woyo/world/attributes.rb, line 109 def define_attr_default attr, default define_singleton_method "#{attr}_default" do default end end
define_attr_equals(attr)
click to toggle source
# File lib/woyo/world/attributes.rb, line 115 def define_attr_equals attr define_singleton_method "#{attr}=" do |arg| @attributes[attr] = arg end end
define_attr_methods(attr, default = nil)
click to toggle source
# File lib/woyo/world/attributes.rb, line 99 def define_attr_methods attr, default = nil define_attr_default attr, default define_attr_equals attr define_attr attr if default == true || default == false # boolean convenience methods define_attr? attr define_attr! attr end end
dependent_changes()
click to toggle source
# File lib/woyo/world/attributes.rb, line 53 def dependent_changes remaining_attrs = @attributes.names - @changes.names dependent_attrs = remaining_attrs.select do |attr| @attributes[attr].kind_of?( Hash ) && ! ( @attributes[attr].keys & @changes.names ).empty? end dependent_attrs.each_with_object({}) do |attr,hash| hash[attr] = send attr end end
exclusion(sym, *attrs)
click to toggle source
# File lib/woyo/world/group.rb, line 116 def exclusion sym, *attrs @exclusions ||= {} exc = @exclusions[sym] ? @exclusions[sym] : ( @exclusions[sym] = Woyo::Attributes::Exclusion.new attributes ) attributes *attrs attrs.each do |attr| define_attr? attr define_attr! attr if attr.kind_of? Hash attr.each do |attr_sym,default_value| exc << attr_sym end else exc << attr end end define_singleton_method sym do @exclusions[sym] end exc end
exclusions()
click to toggle source
# File lib/woyo/world/group.rb, line 112 def exclusions @exclusions end
group(sym, *attrs)
click to toggle source
# File lib/woyo/world/group.rb, line 93 def group sym, *attrs @groups ||= {} grp = @groups[sym] ? @groups[sym] : ( @groups[sym] = Woyo::Attributes::Group.new attributes ) attributes *attrs attrs.each do |attr| if attr.kind_of? Hash attr.each do |attr_sym,default_value| grp << attr_sym end else grp << attr end end define_singleton_method sym do @groups[sym] end grp end
groups()
click to toggle source
# File lib/woyo/world/group.rb, line 89 def groups @groups end
is(attr)
click to toggle source
# File lib/woyo/world/attributes.rb, line 160 def is attr send "#{attr}=", true end
is?(attr)
click to toggle source
# File lib/woyo/world/attributes.rb, line 156 def is? attr send "#{attr}?" end
track_changes()
click to toggle source
# File lib/woyo/world/attributes.rb, line 63 def track_changes @changes = ChangesHash.new @attributes ||= Woyo::Attributes::AttributesHash.new @attributes.add_listener :*, @changes # :* indicates listener for changes to all attributes end