Parent

HashBuilder

HashBuilder

Build a hash programmatically via a fluent DSL-like syntax. In other words, this uses # to buld the hash.

Public Class Methods

load( blockstr, &block ) click to toggle source
    # File lib/more/facets/hashbuilder.rb, line 47
47:   def self.load( blockstr, &block )
48:     new( blockstr, &block ).to_h
49:   end
new( blockstr=nil, &block ) click to toggle source

def self.build( blockstr=nil, &block )

  self.new.build( blockstr, &block ).to_h

end

    # File lib/more/facets/hashbuilder.rb, line 55
55:   def initialize( blockstr=nil, &block )
56:     @hash = {}
57:     @flag = {}
58:     build!( blockstr, &block )
59:   end

Public Instance Methods

build!( blockstr=nil, &block ) click to toggle source
    # File lib/more/facets/hashbuilder.rb, line 61
61:   def build!( blockstr=nil, &block )
62:     raise "both string and block given" if blockstr and block_given?
63:     if blockstr
64:       instance_eval blockstr
65:     else
66:       instance_eval &block
67:     end
68:     self  # or to_h ?
69:   end
method_missing( sym, *args, &block ) click to toggle source
    # File lib/more/facets/hashbuilder.rb, line 73
73:   def method_missing( sym, *args, &block )
74:     sym = sym.to_s.downcase.chomp('=')
75: 
76:     if @hash.key?(sym)
77:       unless @flag[sym]
78:         @hash[sym] = [ @hash[sym] ]
79:         @flag[sym] = true
80:       end
81:       if block_given?
82:         @hash[sym] << self.__class__.new( &block ).to_h
83:       else
84:         @hash[sym] << args[0]
85:       end
86:     else
87:       if block_given?
88:         @hash[sym] = self.__class__.new( &block ).to_h
89:       else
90:         @hash[sym] = args[0]
91:       end
92:     end
93: 
94:   end
to_h() click to toggle source
    # File lib/more/facets/hashbuilder.rb, line 71
71:   def to_h ; @hash ; end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.