Hash Builder

Build a hash programmatically.

Build a hash from missing method calls.

Methods
Public Class methods
build( blockstr=nil, &block )
# File lib/facets/more/hashbuilder.rb, line 32
  def self.build( blockstr=nil, &block )
    self.new.build( blockstr, &block ).to_h
  end
new( blockstr=nil, &block )
# File lib/facets/more/hashbuilder.rb, line 36
  def initialize( blockstr=nil, &block )
    @hash = {}
    @flag = {}
  end
Public Instance methods
build( blockstr=nil, &block )
# File lib/facets/more/hashbuilder.rb, line 41
  def build( blockstr=nil, &block )
    raise "both string and block given" if blockstr and block_given?
    if blockstr
      instance_eval blockstr
    else
      instance_eval &block
    end
    self  # or to_h ?
  end
method_missing( sym, *args, &block )
# File lib/facets/more/hashbuilder.rb, line 53
  def method_missing( sym, *args, &block )
    sym = sym.to_s.downcase.chomp('=')

    if @hash.key?(sym)
      unless @flag[sym]
        @hash[sym] = [ @hash[sym] ]
        @flag[sym] = true
      end
      if block_given?
        @hash[sym] << self.__class__.new( &block ).to_h
      else
        @hash[sym] << args[0]
      end
    else
      if block_given?
        @hash[sym] = self.__class__.new( &block ).to_h
      else
        @hash[sym] = args[0]
      end
    end

  end
to_h()
# File lib/facets/more/hashbuilder.rb, line 51
  def to_h ; @hash ; end