I'm not sure if this can even be achieved, but here goes... :)
Lets assume two models, a Page
model and a Field
model. A Page has_many :fields
and the Field
model has two attributes: :name, :value
What I want to achieve is in the Page
model to dynamically define instance methods for each Field#name
returning the Field#value
.
So if my page had a field with a name of "foobar", I would dynamically create a method like so:
def foobar
fields.find_by_name("foobar").value
end
Can this be achieved? If so, please help?
Over to the rubyists...