<commit_before>= Hash Builder

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.


== Author

* <NAME> -- <EMAIL>


== Examples


=== Defining

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(: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 


=== Building

Each definition requires a key -- in this case :contact_info.  Moreover, within the contact definition, you may set up namespaces (:name, :address, and :personal) to further organize your hash content.

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 want to build an individual namespace, call

  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",
  }


=== Variables

You may pass additional variables to your hash definition when you build it.  Simply pass them after the definition key.

  HashBuilder.build(:contact_info, user)

For namespaces, pass them after the namespace key

  HashBuilder.build(:contact_info, :address, user)

All of this assumes that you will now yield the additional variables in the hash definition:
<commit_msg>updated README again!<commit_after>= Hash Builder

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.


== Author

* <NAME> -- <EMAIL>


== Examples


=== Defining

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(: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 


=== Building

Each definition requires a key -- in this case :contact_info.  Moreover, within the contact definition, you may set up namespaces (:name, :address, and :personal) to further organize your hash content.

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 want to build an individual namespace, call

  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",
  }


=== Variables

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(:contact_info, :address, user)

All of this assumes that you will now yield the 'user' model in the hash definition:

{'fraction_non_alphanumeric': 0.11516279069767442, 'fraction_numerical': 0.013395348837209302, 'mean_word_length': 3.0913242009132422, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2296444', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1633, 'n_words': 652}