Class: REXMLUtilityNode
- Inherits:
-
Object
- Object
- REXMLUtilityNode
- Defined in:
- lib/formatted_string/formats/xml.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#children ⇒ Object
Returns the value of attribute children.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #add_node(node) ⇒ Object
-
#initialize(name, attributes = {}) ⇒ REXMLUtilityNode
constructor
A new instance of REXMLUtilityNode.
- #inner_html ⇒ Object
- #to_hash ⇒ Object
- #to_html ⇒ Object
- #to_s ⇒ Object
- #translate_xml_entities(value) ⇒ Object
- #typecast_value(value) ⇒ Object
- #undasherize_keys(params) ⇒ Object
Constructor Details
#initialize(name, attributes = {}) ⇒ REXMLUtilityNode
Returns a new instance of REXMLUtilityNode.
42 43 44 45 46 47 |
# File 'lib/formatted_string/formats/xml.rb', line 42 def initialize(name, attributes = {}) @name = name.tr("-", "_") @attributes = undasherize_keys(attributes) @children = [] @text = false end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
40 41 42 |
# File 'lib/formatted_string/formats/xml.rb', line 40 def attributes @attributes end |
#children ⇒ Object
Returns the value of attribute children.
40 41 42 |
# File 'lib/formatted_string/formats/xml.rb', line 40 def children @children end |
#name ⇒ Object
Returns the value of attribute name.
40 41 42 |
# File 'lib/formatted_string/formats/xml.rb', line 40 def name @name end |
Instance Method Details
#add_node(node) ⇒ Object
49 50 51 52 |
# File 'lib/formatted_string/formats/xml.rb', line 49 def add_node(node) @text = true if node.is_a? String @children << node end |
#inner_html ⇒ Object
105 106 107 |
# File 'lib/formatted_string/formats/xml.rb', line 105 def inner_html @children.join end |
#to_hash ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/formatted_string/formats/xml.rb', line 54 def to_hash if @text return { name => typecast_value( translate_xml_entities( inner_html ) ) } else #change repeating groups into an array # group by the first key of each element of the array to find repeating groups groups = @children.group_by{ |c| c.name } hash = {} groups.each do |key, values| if values.size == 1 hash.merge! values.first else hash.merge! key => values.map { |element| element.to_hash[key] } end end # merge the arrays, including attributes hash.merge! attributes unless attributes.empty? { name => hash } end end |
#to_html ⇒ Object
109 110 111 |
# File 'lib/formatted_string/formats/xml.rb', line 109 def to_html "<#{name}#{attributes.to_xml_attributes}>#{inner_html}</#{name}>" end |
#to_s ⇒ Object
113 114 115 |
# File 'lib/formatted_string/formats/xml.rb', line 113 def to_s to_html end |
#translate_xml_entities(value) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/formatted_string/formats/xml.rb', line 90 def translate_xml_entities(value) value.gsub(/</, "<"). gsub(/>/, ">"). gsub(/"/, '"'). gsub(/'/, "'"). gsub(/&/, "&") end |
#typecast_value(value) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/formatted_string/formats/xml.rb', line 78 def typecast_value(value) return value unless attributes["type"] case attributes["type"] when "integer" then value.to_i when "boolean" then value.strip == "true" when "datetime" then ::Time.parse(value).utc when "date" then ::Date.parse(value) else value end end |
#undasherize_keys(params) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/formatted_string/formats/xml.rb', line 98 def undasherize_keys(params) params.keys.each do |key, vvalue| params[key.tr("-", "_")] = params.delete(key) end params end |