class HashBuilder

This class simplifies the creation of potentially deeply nested hashes It turns code like this:

hash = {
  one: {
    two: {
      three: {
        four: :foo
      }
    }
  }
}

Into this:

hash = HashBuilder.new
hash.one.two.three.four = :foo

The underlaying hash can be retrieved with `#as_json`.

NOTE: Its good practice to also implement respond_to_missing? when overriding method_missing, but for some reason that makes the tests fail.

Public Class Methods

new() click to toggle source
# File lib/hash_builder.rb, line 24
def initialize
  @hash = {}
end

Public Instance Methods

as_json() click to toggle source
# File lib/hash_builder.rb, line 28
def as_json
  @hash.as_json
end
method_missing(name, *args) click to toggle source
# File lib/hash_builder.rb, line 32
def method_missing(name, *args)
  match = name.to_s.match(/(?<key>.*?)=$/)

  if match.present?
    @hash[match[:key].to_sym] = args.first
  else
    @hash[name] = HashBuilder.new if @hash[name].blank?
    @hash[name]
  end
end