Class: Builder::HashStructure
Instance Method Summary
collapse
-
#<<(_target) ⇒ Object
-
#array_mode(key = nil, &block) ⇒ Object
NOTICE: you have to call this method to use array in json.
-
#content!(key, default_content_key, *attrs, &block) ⇒ Object
-
#initialize(options = {}) ⇒ HashStructure
constructor
A new instance of HashStructure.
-
#method_missing(key, *args, &block) ⇒ Object
-
#root!(key, *attrs, &block) ⇒ Object
-
#tag!(key, *attrs, &block) ⇒ Object
-
#target! ⇒ Object
-
#text!(text, default_content_key = nil) ⇒ Object
(also: #cdata!)
Methods inherited from Abstract
#comment!, #declare!, #instruct!, #nil?
Constructor Details
#initialize(options = {}) ⇒ HashStructure
Returns a new instance of HashStructure.
4
5
6
7
8
9
10
11
|
# File 'lib/builder/hash_structure.rb', line 4
def initialize(options = {})
@default_content_key = (options[:default_content_key] || :content).to_sym
@include_root = options[:include_root]
@target = {}
@array_mode = false
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, *args, &block) ⇒ Object
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/builder/hash_structure.rb', line 73
def method_missing(key, *args, &block)
key = args.first.is_a?(Symbol) ? "#{key}:#{args.shift}".to_sym : key.to_sym
args[0] = {@default_content_key => args[0]} if args.size > 1 && !args[0].is_a?(::Hash)
if @root
_child(key, args, &block)
else
_root(key, args, &block)
end
target!
end
|
Instance Method Details
#<<(_target) ⇒ Object
48
49
50
51
52
53
54
55
|
# File 'lib/builder/hash_structure.rb', line 48
def <<(_target)
if @array_mode
eval("#{_current} << _target")
else
eval("#{_current} ||= {}")
eval("#{_current}.merge!(_target)")
end
end
|
#array_mode(key = nil, &block) ⇒ Object
NOTICE: you have to call this method to use array in json
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/builder/hash_structure.rb', line 14
def array_mode(key = nil, &block)
raise RuntimeError.new("cannot call inside array_mode block") if @array_mode
@array_mode = true
if eval("#{_current}").is_a?(::Hash)
key ||= :entry
eval("#{_current}.merge!(key => [])")
@path.push(key.to_sym)
yield(self)
@path.pop
else
eval("#{_current} = []")
yield(self)
end
@array_mode = false
end
|
#content!(key, default_content_key, *attrs, &block) ⇒ Object
43
44
45
46
|
# File 'lib/builder/hash_structure.rb', line 43
def content!(key, default_content_key, *attrs, &block)
@default_content_key = default_content_key
method_missing(key, *attrs, &block)
end
|
#root!(key, *attrs, &block) ⇒ Object
38
39
40
41
|
# File 'lib/builder/hash_structure.rb', line 38
def root!(key, *attrs, &block)
@include_root = true
method_missing(key, *attrs, &block)
end
|
#tag!(key, *attrs, &block) ⇒ Object
69
70
71
|
# File 'lib/builder/hash_structure.rb', line 69
def tag!(key, *attrs, &block)
method_missing(key, *args, &block)
end
|
#target! ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/builder/hash_structure.rb', line 30
def target!
if @include_root
@target
else
@target[@root]
end
end
|
#text!(text, default_content_key = nil) ⇒ Object
Also known as:
cdata!
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/builder/hash_structure.rb', line 57
def text!(text, default_content_key = nil)
@default_content_key = default_content_key unless default_content_key.nil?
raise RuntimeError.new("cannot call inside array_mode block") if @array_mode
if eval("#{_current}").is_a?(::Hash)
eval("#{_current}.merge!({@default_content_key => text})")
else
eval("#{_current} = text")
end
end
|