Module: Woyo::Attributes
- Included in:
- WorldObject
- Defined in:
- lib/woyo/world/group.rb,
lib/woyo/world/attributes.rb
Defined Under Namespace
Classes: AttributesHash, ChangesHash, Exclusion, Group
Instance Method Summary
collapse
Instance Method Details
#attribute(*attrs, &block) ⇒ Object
73
74
75
|
# File 'lib/woyo/world/attributes.rb', line 73
def attribute *attrs, &block
attributes *attrs, &block
end
|
#attributes(*attrs, &block) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# 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 ⇒ Object
49
50
51
|
# File 'lib/woyo/world/attributes.rb', line 49
def changes
@changes ? @changes.merge!( dependent_changes ) : nil
end
|
#clear_changes ⇒ Object
69
70
71
|
# File 'lib/woyo/world/attributes.rb', line 69
def clear_changes
@changes.clear
end
|
#define_attr(attr) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# 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) ⇒ Object
150
151
152
153
154
|
# 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) ⇒ Boolean
144
145
146
147
148
|
# 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) ⇒ Object
109
110
111
112
113
|
# 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) ⇒ Object
115
116
117
118
119
|
# 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) ⇒ Object
99
100
101
102
103
104
105
106
107
|
# 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 define_attr? attr
define_attr! attr
end
end
|
#dependent_changes ⇒ Object
53
54
55
56
57
58
59
60
61
|
# 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) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# 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 ⇒ Object
112
113
114
|
# File 'lib/woyo/world/group.rb', line 112
def exclusions
@exclusions
end
|
#group(sym, *attrs) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# 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 ⇒ Object
89
90
91
|
# File 'lib/woyo/world/group.rb', line 89
def groups
@groups
end
|
#is(attr) ⇒ Object
160
161
162
|
# File 'lib/woyo/world/attributes.rb', line 160
def is attr
send "#{attr}=", true
end
|
#is?(attr) ⇒ Boolean
156
157
158
|
# File 'lib/woyo/world/attributes.rb', line 156
def is? attr
send "#{attr}?"
end
|