Module | LibXML::Conversions::Node |
In: |
vendor/rails/activesupport/lib/active_support/xml_mini/libxml.rb
|
CONTENT_ROOT | = | '__content__' |
LIB_XML_LIMIT | = | 30000000 |
Convert XML document to hash
hash: | Hash to merge the converted element into. |
# File vendor/rails/activesupport/lib/active_support/xml_mini/libxml.rb, line 40 40: def to_hash(hash={}) 41: if text? 42: raise LibXML::XML::Error if content.length >= LIB_XML_LIMIT 43: hash[CONTENT_ROOT] = content 44: else 45: sub_hash = insert_name_into_hash(hash, name) 46: attributes_to_hash(sub_hash) 47: if array? 48: children_array_to_hash(sub_hash) 49: elsif yaml? 50: children_yaml_to_hash(sub_hash) 51: else 52: children_to_hash(sub_hash) 53: end 54: end 55: hash 56: end
Check if child is of type array
# File vendor/rails/activesupport/lib/active_support/xml_mini/libxml.rb, line 119 119: def array? 120: child? && child.next? && child.name == child.next.name 121: end
Convert xml attributes to hash
hash: | Hash to merge the attributes into |
# File vendor/rails/activesupport/lib/active_support/xml_mini/libxml.rb, line 93 93: def attributes_to_hash(hash={}) 94: each_attr { |attr| hash[attr.name] = attr.value } 95: hash 96: end
Convert array into hash
hash: | Hash to merge the array into |
# File vendor/rails/activesupport/lib/active_support/xml_mini/libxml.rb, line 102 102: def children_array_to_hash(hash={}) 103: hash[child.name] = map do |child| 104: returning({}) { |sub_hash| child.children_to_hash(sub_hash) } 105: end 106: hash 107: end
Insert children into hash
hash: | Hash to merge the children into. |
# File vendor/rails/activesupport/lib/active_support/xml_mini/libxml.rb, line 83 83: def children_to_hash(hash={}) 84: each { |child| child.to_hash(hash) } 85: attributes_to_hash(hash) 86: hash 87: end
Convert yaml into hash
hash: | Hash to merge the yaml into |
# File vendor/rails/activesupport/lib/active_support/xml_mini/libxml.rb, line 113 113: def children_yaml_to_hash(hash = {}) 114: hash[CONTENT_ROOT] = content unless content.blank? 115: hash 116: end
Insert name into hash
hash: | Hash to merge the converted element into. |
name: | name to to merge into hash |
# File vendor/rails/activesupport/lib/active_support/xml_mini/libxml.rb, line 66 66: def insert_name_into_hash(hash, name) 67: sub_hash = {} 68: if hash[name] 69: if !hash[name].kind_of? Array 70: hash[name] = [hash[name]] 71: end 72: hash[name] << sub_hash 73: else 74: hash[name] = sub_hash 75: end 76: sub_hash 77: end