145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/maintain/maintainer.rb', line 145
def state(name, value = nil, options = {})
if value.is_a?(Hash)
options = value
value = nil
end
@increment ||= 0
if bitmask?
unless value.is_a?(Integer)
value = @increment
end
value = 2 ** value.to_i
elsif value.is_a?(Integer)
integer(true)
end
value ||= name
states[name] = {compare_value: !bitmask? && value.is_a?(Integer) ? value : @increment, value: value}
@increment += 1
if back_end
back_end.state maintainee, name, @attribute, value.is_a?(Symbol) ? value.to_s : value, force: options[:force]
end
if options.has_key?(:default)
default(name)
end
if options.has_key?(:enter)
on :enter, name.to_sym, options.delete(:enter)
end
if options.has_key?(:exit)
on :exit, name.to_sym, options.delete(:exit)
end
boolean_method = "#{name}?"
shortcut = options[:force] || method_free?(boolean_method)
maintainee.class_eval " def \#{@attribute}_\#{boolean_method}\n \#{@attribute} == \#{value.inspect}\n end\n \#{\"alias :\#{boolean_method} :\#{@attribute}_\#{boolean_method}\" if shortcut}\n EOC\n\n # Last but not least, add bang methods to automatically convert to state.\n # Like boolean methods above, these only get added if they're not already\n # things that are things.\n bang_method = \"\#{name}!\"\n shortcut = options[:force] || method_free?(bang_method)\n maintainee.class_eval <<-EOC\n def \#{@attribute}_\#{bang_method}\n self.\#{@attribute} = \#{value.inspect}\n end\n \#{\"alias :\#{bang_method} :\#{@attribute}_\#{bang_method}\" if shortcut}\n EOC\nend\n"
|