class Hash::Builder

Public Class Methods

build(&block) click to toggle source
# File lib/hash_ext/builder.rb, line 25
def self.build(&block)
  new.build(&block)
end
new() click to toggle source
# File lib/hash_ext/builder.rb, line 4
def initialize
  @hash = {}
  @scopes = []
end

Public Instance Methods

build() { |self| ... } click to toggle source
# File lib/hash_ext/builder.rb, line 9
def build
  yield self
  @hash
end
method_missing(method, *args, &block) click to toggle source
# File lib/hash_ext/builder.rb, line 14
def method_missing(method, *args, &block)
  if block
    @scopes.push method
    block.call
    @scopes.pop
  else
    current_scope = @scopes.inject(@hash) { |h,s| h[s] ||= {} }
    current_scope[method] = args.first
  end
end