I have a person object that gets a big hash of information about the person back from a webservice call (around 400 key value pairs). Each of the items in the hash is somewhat distinct, and has to be handled differently before displaying to the user (e.g. timestamp value converted, nils handled, or text otherwise transformed), etc, and the keys themselves are also important elsewhere and must be 'translated' from a fixed mapping list.
Currently the hash of attributes is accessed directly in a Rails view, and then the return value is cleaned up in a helper, as in
<%= clean_up(person.attributes["some_strangly_formatted_name"]) %>
or
<%= show_timestamp(person.attributes["some_nonhuman_time"]) %>
I'm trying to figure out how to assign each of these values to the person object itself dynamically, while also creating each of the attributes as its own object of a different class (e.g. Attribute) to move related behavior there, as the amount and complexity of helper methods is getting out of control.
Ideally in the view, I could call person.some_timestamp
and it would return the human readable time or person.name
and return a string of the person's name
I've tried assigning variables as shown here, but it seems like the attribute has to be pre-defined on the object, and I'm trying to avoid 400 attr_accessors and also trying to accommodate the possibility that new attributes could be added Set Attribute Dynamically of Ruby Object Having a hash, set object properties in Ruby DRY way to assign hash values to an object
the non-working method looks like
def process_attributes(attributes)
attributes.each {|k,v| public_send("#{k}=", v)}
end
How can I accomplish this goal? More generally, does this strategy even make sense?