Class: Qbxml::Hash
- Inherits:
-
Hash
show all
- Includes:
- Types
- Defined in:
- lib/qbxml/hash.rb
Overview
Constant Summary
collapse
- CONTENT_ROOT =
'__content__'.freeze
- ATTR_ROOT =
'xml_attributes'.freeze
- IGNORED_KEYS =
[ATTR_ROOT]
Constants included
from Types
Types::ACRONYMS, Types::ACRONYM_REGEXP, Types::BIGDECIMAL_CAST, Types::BOOL_CAST, Types::DATE_CAST, Types::FLOAT_CAST, Types::INT_CAST, Types::STR_CAST, Types::TIME_CAST, Types::TYPE_MAP, Types::XML_DIRECTIVES
Class Method Summary
collapse
-
.deep_convert(hash, opts = {}, &block) ⇒ Object
-
.from_hash(hash, opts = {}, &block) ⇒ Object
-
.from_xml(xml, opts = {}) ⇒ Object
-
.hash_to_xml(hash, opts = {}) ⇒ Object
-
.is_repetitive?(schema, xpath, is_repetitive_cache) ⇒ Boolean
Determines if the node is repetitive.
-
.parse_repetitive_from_comment(schema, comment_path) ⇒ Object
-
.to_xml(hash, opts = {}) ⇒ Object
-
.typecast(schema, xpath, value, typecast_cache) ⇒ Object
-
.xml_to_hash(node, hash = {}, opts = {}) ⇒ Object
Instance Method Summary
collapse
Class Method Details
.deep_convert(hash, opts = {}, &block) ⇒ Object
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/qbxml/hash.rb', line 169
def self.deep_convert(hash, opts = {}, &block)
hash.inject(self.new) do |h, (k,v)|
k = k.to_s
ignored = IGNORED_KEYS.include?(k)
if ignored
h[k] = v
else
key = block_given? ? yield(k) : k
h[key] = \
case v
when Hash
deep_convert(v, &block)
when Array
v.map { |i| i.is_a?(Hash) ? deep_convert(i, &block) : i }
else v
end
end; h
end
end
|
.from_hash(hash, opts = {}, &block) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/qbxml/hash.rb', line 16
def self.from_hash(hash, opts = {}, &block)
key_proc = \
if opts[:camelize]
lambda { |k|
k.camelize.gsub(Qbxml::Types::ACRONYM_REGEXP) { "#{$1}#{$2.upcase}#{$3}" }
}
elsif opts[:underscore]
lambda { |k| k.underscore }
end
deep_convert(hash, opts, &key_proc)
end
|
.from_xml(xml, opts = {}) ⇒ Object
41
42
43
44
|
# File 'lib/qbxml/hash.rb', line 41
def self.from_xml(xml, opts = {})
from_hash(
xml_to_hash(Nokogiri::XML(xml).root, {}, opts), opts)
end
|
.hash_to_xml(hash, opts = {}) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/qbxml/hash.rb', line 48
def self.hash_to_xml(hash, opts = {})
opts = opts.dup
opts[:indent] ||= 2
opts[:root] ||= :hash
opts[:attributes] ||= (hash.delete(ATTR_ROOT) || {})
opts[:builder] ||= Builder::XmlMarkup.new(indent: opts[:indent])
opts[:skip_types] = true unless opts.key?(:skip_types)
opts[:skip_instruct] = false unless opts.key?(:skip_instruct)
builder = opts[:builder]
unless opts.delete(:skip_instruct)
builder.instruct!(:xml, :encoding => "ISO-8859-1")
builder.instruct!(opts[:schema], version: opts[:version])
end
builder.tag!(opts[:root], opts.delete(:attributes)) do
hash.each do |key, val|
case val
when Hash
self.hash_to_xml(val, opts.merge({root: key, skip_instruct: true}))
when Array
val.map { |i|
if i.is_a?(String)
next builder.tag!(key, i, {})
end
next self.hash_to_xml(i, opts.merge({root: key, skip_instruct: true}))
}
else
builder.tag!(key, val, {})
end
end
yield builder if block_given?
end
end
|
.is_repetitive?(schema, xpath, is_repetitive_cache) ⇒ Boolean
Determines if the node is repetitive. Just because something is repetitive doesn’t mean it always repeats. For example, a customer query could return 1 result or 100, but in both cases, we should be returning an Array.
157
158
159
160
161
|
# File 'lib/qbxml/hash.rb', line 157
def self.is_repetitive?(schema, xpath, is_repetitive_cache)
= xpath.gsub(/\[\d+\]/,'') + "/comment()"
return is_repetitive_cache[] || (schema, )
end
|
163
164
165
166
167
|
# File 'lib/qbxml/hash.rb', line 163
def self.(schema, )
= schema.xpath().first
return false if .nil?
return .text.include?('may rep')
end
|
.to_xml(hash, opts = {}) ⇒ Object
35
36
37
38
39
|
# File 'lib/qbxml/hash.rb', line 35
def self.to_xml(hash, opts = {})
opts[:root], hash = hash.first
opts[:attributes] = hash.delete(ATTR_ROOT)
hash_to_xml(hash, opts)
end
|
.typecast(schema, xpath, value, typecast_cache) ⇒ Object
146
147
148
149
150
151
152
|
# File 'lib/qbxml/hash.rb', line 146
def self.typecast(schema, xpath, value, typecast_cache)
type_path = xpath.gsub(/\[\d+\]/,'')
type_proc = typecast_cache[type_path] ||= Qbxml::TYPE_MAP[schema.xpath(type_path).first.try(:text)]
raise "#{xpath} is not a valid type" unless type_proc
type_proc[value]
end
|
.xml_to_hash(node, hash = {}, opts = {}) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/qbxml/hash.rb', line 84
def self.xml_to_hash(node, hash = {}, opts = {})
node_hash = {CONTENT_ROOT => '', ATTR_ROOT => {}}
name = node.name
schema = opts[:schema]
opts[:typecast_cache] ||= {}
opts[:is_repetitive_cache] ||= {}
case hash[name]
when Array
hash[name] << node_hash
when Hash, String
hash[name] = [hash[name], node_hash]
else
if is_repetitive?(schema, node.path, opts[:is_repetitive_cache])
hash[name] = [node_hash]
else
hash[name] = node_hash
end
end
node.children.each do |c|
if c.element?
xml_to_hash(c, node_hash, opts)
elsif c.text? || c.cdata?
node_hash[CONTENT_ROOT] << c.content
end
end
node.attribute_nodes.each { |a| node_hash[ATTR_ROOT][a.node_name] = a.value }
if node_hash.size > 2 || node_hash[ATTR_ROOT].present?
node_hash.delete(CONTENT_ROOT)
elsif node_hash[CONTENT_ROOT].present?
node_hash.delete(ATTR_ROOT)
v = schema ? typecast(schema, node.path, node_hash[CONTENT_ROOT], opts[:typecast_cache]) : node_hash[CONTENT_ROOT]
if hash[name].is_a?(Array)
hash[name].pop
hash[name] << v
else
hash[name] = v
end
else
hash[name] = node_hash[CONTENT_ROOT]
end
hash
end
|
Instance Method Details
#to_xml(opts = {}) ⇒ Object
31
32
33
|
# File 'lib/qbxml/hash.rb', line 31
def to_xml(opts = {})
self.class.to_xml(self, opts)
end
|