How do I set a field dynamically for a Ohm object?
class OhmObj < Ohm::Model
attribute :foo
attribute :bar
attribute :baz
def add att, val
self[att] = val
end
end
class OtherObj
def initialize
@ohm_obj = OhmObj.create
end
def set att, val
@ohm_obj[att] = val #doesn't work
@ohm_obj.add(att, val) #doesn't work
end
end
The
attributeclass method fromOhm::Modeldefines accessor and mutator methods for the named attribute:So when you say
attribute :foo, you get these methods for free:You could use
sendto call the mutator method like this:If you really want to say
@ohm_obj[att] = valthen you could add something like the following to yourOhmObjclass:And you’d probably want the accessor version as well to maintain symmetry: