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
-
#serialization_method! ⇒ Object
-
#tag!(key, *attrs, &block) ⇒ Object
-
#target! ⇒ Object
-
#text!(text, default_content_key = nil) ⇒ Object
(also: #cdata!)
Methods inherited from Abstract
#comment!, #declare!, #instruct!, #new!, #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
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/builder/hash_structure.rb', line 78
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/builder/hash_structure.rb', line 49
def <<(_target)
if @array_mode
key = @path.pop
eval("#{_current} << _target")
@path.push(key)
else
if _target.is_a?(String)
eval("#{_current} = _target")
else
eval("#{_current} ||= {}")
eval("#{_current}.merge!(_target)")
end
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
|
# File 'lib/builder/hash_structure.rb', line 14
def array_mode(key = nil, &block)
if eval("#{_current}").is_a?(::Hash)
key ||= :entry
eval("#{_current}.merge!(key => [])")
_move_current(key.to_sym) do
_array_mode(&block)
end
else
eval("#{_current} = []")
_array_mode(&block)
end
end
|
#content!(key, default_content_key, *attrs, &block) ⇒ Object
44
45
46
47
|
# File 'lib/builder/hash_structure.rb', line 44
def content!(key, default_content_key, *attrs, &block)
@default_content_key = default_content_key.to_sym
method_missing(key, *attrs, &block)
end
|
#root!(key, *attrs, &block) ⇒ Object
39
40
41
42
|
# File 'lib/builder/hash_structure.rb', line 39
def root!(key, *attrs, &block)
@include_root = true
method_missing(key, *attrs, &block)
end
|
#serialization_method! ⇒ Object
35
36
37
|
# File 'lib/builder/hash_structure.rb', line 35
def serialization_method!
:to_hash
end
|
#tag!(key, *attrs, &block) ⇒ Object
74
75
76
|
# File 'lib/builder/hash_structure.rb', line 74
def tag!(key, *attrs, &block)
method_missing(key, *attrs, &block)
end
|
#target! ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/builder/hash_structure.rb', line 27
def target!
if @include_root
@target
else
@target[@root]
end
end
|
#text!(text, default_content_key = nil) ⇒ Object
Also known as:
cdata!
64
65
66
67
68
69
70
71
|
# File 'lib/builder/hash_structure.rb', line 64
def text!(text, default_content_key = nil)
@default_content_key = default_content_key.to_sym unless default_content_key.nil?
if eval("#{_current}").is_a?(::Hash)
eval("#{_current}.merge!({@default_content_key => text})")
else
eval("#{_current} = text")
end
end
|