Are you tired of ugly, cumbersome hashes cluttering up your Rails models and controllers. Well, Hash Builder is a simple tool for building hashes that allows you define hashes separate from your code, keeping your code clean and focused on its core responsibilities. The hashes themselves are then easy to organize and manipulate.
-
Chris Wyckoff – cwyckoff@leadmediapartners.com
Let’s say you want to build a hash of user contact information. In a separate file, set up a hash definition:
HashBuilder.define(:contact_info) do |b| b.build_hash do b[:title] = "Dr." b[:f_name] = "Hawkeye" b[:l_name] = "Pierce" b[:street] = "123 Main St." b[:city] = "Crabapple Cove" b[:state] = "ME" b[:zip] = "04656" b[:gender] = "male" b[:age] = "50" b[:married] = false end end
Each definition requires a key – in this case :contact_info. Our contact_info hash is now registered to that key and we are free to buid away within the block passed to the #build_hash method.
For complex hash construction, it helps to organize your data with namespaces; in the example below I’ve set up three separate namespaces, :name, :address, and :personal, which are keys registered with the #buid_hash method.
HashBuilder.define(:contact_info) do |b| b.build_hash(:name) do b[:title] = "Dr." b[:f_name] = "Hawkeye" b[:l_name] = "Pierce" end b.build_hash(:address) do b[:street] = "123 Main St." b[:city] = "Crabapple Cove" b[:state] = "ME" b[:zip] = "04656" end b.build_hash(:personal) do b[:gender] = "male" b[:age] = "50" b[:married] = false end end
In order to build your hash, simply call
HashBuilder.build(:contact_info)
which will return
{ :title => "Dr.", :f_name => "Hawkeye", :l_name => "Pierce", :street => "123 Main St.", :city => "Crabapple Cove", :state => "ME", :zip => "04656", :gender => "mail", :age => "50", :married => false, }
If you have set up namespaces and you only want to build an individual namespace, use the #build_namespace method
HashBuilder.build_namespace(:contact_info, :address)
which will return only the :address section of the hash
{ :street => "123 Main St.", :city => "Crabapple Cove", :state => "ME", :zip => "04656", }
To select multiple namespaces, set them in a block:
HashBuilder.build(:contact_info) do |b| b.using :name b.using :address end
which returns only the :name and :address sections of the hash
{ :title => "Dr.", :f_name => "Hawkeye", :l_name => "Pierce", :street => "123 Main St.", :city => "Crabapple Cove", :state => "ME", :zip => "04656", }
You may pass additional variables to your hash definition when you build it. Let’s assume you want to use an ActiveRecord ‘user’ model to dynamically populate your hash instead of hard-coding the values as we did above. Simply pass the ‘user’ object to the .build method after the definition key.
HashBuilder.build(:contact_info, user)
For namespaces, pass it after the namespace key
HashBuilder.build_namespace(:contact_info, :address, user)
This requires, however, that you now yield the ‘user’ model in the hash definition:
HashBuilder.define(:contact_info) do |b| b.build_hash(:name) do |user| b[:title] = user.title b[:f_name] = user.first_name b[:l_name] = user.last_name end b.build_hash(:address) do |user| b[:street] = user.street b[:city] = user.city b[:state] = user.state b[:zip] = user.zip end b.build_hash(:personal) do |user| b[:gender] = user.gender b[:age] = user.age b[:married] = user.married end end
You may pass as many additional variables as you like. Just remember to yield them to your hash block.