The attribute
class method from Ohm::Model
defines accessor and mutator methods for the named attribute:
def self.attribute(name)
define_method(name) do
read_local(name)
end
define_method(:"#{name}=") do |value|
write_local(name, value)
end
attributes << name unless attributes.include?(name)
end
So when you say attribute :foo
, you get these methods for free:
def foo # Returns the value of foo.
def foo=(value) # Assigns a value to foo.
You could use send
to call the mutator method like this:
@ohm_obj.send((att + '=').to_sym, val)
If you really want to say @ohm_obj[att] = val
then you could add something like the following to your OhmObj
class:
def []=(att, value)
send((att + '=').to_sym, val)
end
And you'd probably want the accessor version as well to maintain symmetry:
def [](att)
send(att.to_sym)
end