I am trying to add a method to Observable, so that for a class that includes it, it can call the method observe_attribute :attribute
that would generate a method attribute=
with the logic to check to see if the value has changed:
module Observable
def observe_attribute(attribute)
raise NameError, "#{self.class} does not contain #{attribute}" unless instance_variables.include? "@#{attribute}"
eval %" def #{attribute}=(new_value)
unless @#{attribute} == new_value
changed
notify_observers
puts 'ok'
end
end
";
end
end
However, for the following class, the last call to observe_attribute :att
results in a NoMethodError, while the commented one does not:
class Test
include Observable
def initialize
@att = 3
#observe_attribute :att
end
observe_attribute :att
end
What would I need to do in order for the last call to observe_attribute
to work correctly?