Class: Hash::Builder
- Inherits:
-
Object
show all
- Defined in:
- lib/hash_ext/builder.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Builder.
4
5
6
7
|
# File 'lib/hash_ext/builder.rb', line 4
def initialize
@hash = {}
@scopes = []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
14
15
16
17
18
19
20
21
22
23
|
# 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
|
Class Method Details
.build(&block) ⇒ Object
25
26
27
|
# File 'lib/hash_ext/builder.rb', line 25
def self.build(&block)
new.build(&block)
end
|
Instance Method Details
#build {|_self| ... } ⇒ Object
9
10
11
12
|
# File 'lib/hash_ext/builder.rb', line 9
def build
yield self
@hash
end
|