I am using this array of hashes to do a batch insert into a mongo DB. Each hash was populated by parsing a text file so the formatting of fields are in an unpredictable format. It might look something like:
{date => "March 5", time => "05:22:21", first_name = "John", middle_initial = "JJ", ...}
And I would have a series of formatting functions. So maybe:
def format_date
..convert if needed..
end
def format_time
...
end
How would I go about calling the formatting functions on various records? I could see doing some kind of lambda call where I iterate through the hash and call a format_record_name function, but not all records will have formatting functions. For instance above the first_name record wouldn't need one. Any ideas?
Make use of Ruby's Singleton (or Eigen) class and then the following one liner solves your problem:
Here's one idea, pretty similar to what you stated. You might just have an identity function for the fields you don't want to format
Just keep a list of the keys that you do want to handle. You could even tie it to the transformation functions with a Hash: