Module: MultiXml::Helpers
- Included in:
- MultiXml
- Defined in:
- lib/multi_xml/helpers.rb
Overview
Methods for transforming parsed XML hash structures
These helper methods handle key transformation and type casting of parsed XML data structures.
Class Method Summary collapse
-
.apply_converter(hash, content, converter) ⇒ Object
private
Apply a type converter to content.
-
.convert_hash(hash, type, disallowed_types) ⇒ Object
private
Convert a hash based on its type and content.
-
.convert_text_content(hash) ⇒ Object
private
Convert text content using type converters.
-
.disallowed_type?(type, disallowed_types) ⇒ Boolean
private
Check if a type is in the disallowed list.
-
.empty_value?(hash, type) ⇒ Boolean
private
Check if a hash represents an empty value.
-
.extract_array_entries(hash, disallowed_types) ⇒ Array
private
Extract array entries from element with type="array".
-
.find_array_entries(hash) ⇒ Array, ...
private
Find array or hash entries in a hash, excluding the type key.
-
.symbolize_keys(data) ⇒ Hash, ...
private
Recursively convert all hash keys to symbols.
-
.transform_keys(data, &block) ⇒ Hash, ...
private
Recursively transform hash keys using a block.
-
.typecast_array(array, disallowed_types) ⇒ Object, Array
private
Typecast array elements and unwrap single-element arrays.
-
.typecast_children(hash, disallowed_types) ⇒ Hash, StringIO
private
Typecast all child values in a hash.
-
.typecast_hash(hash, disallowed_types) ⇒ Object
private
Typecast a hash based on its type attribute.
-
.typecast_xml_value(value, disallowed_types = DISALLOWED_TYPES) ⇒ Object
private
Recursively typecast XML values based on type attributes.
-
.undasherize_keys(data) ⇒ Hash, ...
private
Recursively convert dashes in hash keys to underscores.
-
.unwrap_file_if_present(result) ⇒ Hash, StringIO
private
Unwrap a file object from the result hash if present.
-
.unwrap_if_simple(hash, value) ⇒ Object, Hash
private
Unwrap value if hash has no other significant keys.
-
.wrap_and_typecast(entries, disallowed_types) ⇒ Array
private
Wrap hash in array if needed and typecast all entries.
Class Method Details
.apply_converter(hash, content, converter) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Apply a type converter to content
220 221 222 223 224 225 226 |
# File 'lib/multi_xml/helpers.rb', line 220 def apply_converter(hash, content, converter) # Binary converters need access to entity attributes (e.g., encoding, name) return converter.call(content, hash) if converter.arity == 2 hash.delete("type") unwrap_if_simple(hash, converter.call(content)) end |
.convert_hash(hash, type, disallowed_types) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert a hash based on its type and content
93 94 95 96 97 98 99 100 |
# File 'lib/multi_xml/helpers.rb', line 93 def convert_hash(hash, type, disallowed_types) return extract_array_entries(hash, disallowed_types) if type == "array" return convert_text_content(hash) if hash.key?(TEXT_CONTENT_KEY) return "" if type == "string" && !hash["nil"].eql?("true") return nil if empty_value?(hash, type) typecast_children(hash, disallowed_types) end |
.convert_text_content(hash) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert text content using type converters
155 156 157 158 159 160 161 162 |
# File 'lib/multi_xml/helpers.rb', line 155 def convert_text_content(hash) content = hash.fetch(TEXT_CONTENT_KEY) converter = TYPE_CONVERTERS[hash["type"]] return unwrap_if_simple(hash, content) unless converter apply_converter(hash, content, converter) end |
.disallowed_type?(type, disallowed_types) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if a type is in the disallowed list
82 83 84 |
# File 'lib/multi_xml/helpers.rb', line 82 def disallowed_type?(type, disallowed_types) type && !type.is_a?(Hash) && disallowed_types.include?(type) end |
.empty_value?(hash, type) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if a hash represents an empty value
180 181 182 183 184 |
# File 'lib/multi_xml/helpers.rb', line 180 def empty_value?(hash, type) hash.empty? || hash["nil"] == "true" || (type && hash.size == 1 && !type.is_a?(Hash)) end |
.extract_array_entries(hash, disallowed_types) ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extract array entries from element with type="array"
120 121 122 123 124 125 |
# File 'lib/multi_xml/helpers.rb', line 120 def extract_array_entries(hash, disallowed_types) entries = find_array_entries(hash) return [] unless entries wrap_and_typecast(entries, disallowed_types) end |
.find_array_entries(hash) ⇒ Array, ...
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Find array or hash entries in a hash, excluding the type key
132 133 134 135 136 137 |
# File 'lib/multi_xml/helpers.rb', line 132 def find_array_entries(hash) hash.each do |key, value| return value if !key.eql?("type") && (value.is_a?(Array) || value.is_a?(Hash)) end nil end |
.symbolize_keys(data) ⇒ Hash, ...
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Recursively convert all hash keys to symbols
18 19 20 |
# File 'lib/multi_xml/helpers.rb', line 18 def symbolize_keys(data) transform_keys(data, &:to_sym) end |
.transform_keys(data, &block) ⇒ Hash, ...
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Recursively transform hash keys using a block
193 194 195 196 197 198 199 200 201 |
# File 'lib/multi_xml/helpers.rb', line 193 def transform_keys(data, &block) case data when Hash then data.each_with_object( {} #: Hash[Symbol, MultiXml::xmlValue] # rubocop:disable Layout/LeadingCommentSpace ) { |(key, value), acc| acc[yield(key)] = transform_keys(value, &block) } when Array then data.map { |item| transform_keys(item, &block) } else data end end |
.typecast_array(array, disallowed_types) ⇒ Object, Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Typecast array elements and unwrap single-element arrays
57 58 59 60 |
# File 'lib/multi_xml/helpers.rb', line 57 def typecast_array(array, disallowed_types) array.map! { |item| typecast_xml_value(item, disallowed_types) } array.one? ? array.first : array end |
.typecast_children(hash, disallowed_types) ⇒ Hash, StringIO
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Typecast all child values in a hash
108 109 110 111 |
# File 'lib/multi_xml/helpers.rb', line 108 def typecast_children(hash, disallowed_types) result = hash.transform_values { |v| typecast_xml_value(v, disallowed_types) } unwrap_file_if_present(result) end |
.typecast_hash(hash, disallowed_types) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Typecast a hash based on its type attribute
69 70 71 72 73 74 |
# File 'lib/multi_xml/helpers.rb', line 69 def typecast_hash(hash, disallowed_types) type = hash["type"] raise DisallowedTypeError, type if disallowed_type?(type, disallowed_types) convert_hash(hash, type, disallowed_types) end |
.typecast_xml_value(value, disallowed_types = DISALLOWED_TYPES) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Recursively typecast XML values based on type attributes
43 44 45 46 47 48 49 |
# File 'lib/multi_xml/helpers.rb', line 43 def typecast_xml_value(value, disallowed_types = DISALLOWED_TYPES) case value when Hash then typecast_hash(value, disallowed_types) when Array then typecast_array(value, disallowed_types) else value end end |
.undasherize_keys(data) ⇒ Hash, ...
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Recursively convert dashes in hash keys to underscores
29 30 31 |
# File 'lib/multi_xml/helpers.rb', line 29 def undasherize_keys(data) transform_keys(data) { |key| key.tr("-", "_") } end |
.unwrap_file_if_present(result) ⇒ Hash, StringIO
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Unwrap a file object from the result hash if present
208 209 210 211 |
# File 'lib/multi_xml/helpers.rb', line 208 def unwrap_file_if_present(result) file = result["file"] file.is_a?(StringIO) ? file : result end |
.unwrap_if_simple(hash, value) ⇒ Object, Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Unwrap value if hash has no other significant keys
170 171 172 |
# File 'lib/multi_xml/helpers.rb', line 170 def unwrap_if_simple(hash, value) (hash.size > 1) ? hash.merge(TEXT_CONTENT_KEY => value) : value end |
.wrap_and_typecast(entries, disallowed_types) ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Wrap hash in array if needed and typecast all entries
145 146 147 148 |
# File 'lib/multi_xml/helpers.rb', line 145 def wrap_and_typecast(entries, disallowed_types) entries = [entries] if entries.is_a?(Hash) entries.map { |entry| typecast_xml_value(entry, disallowed_types) } end |