Class: REXMLUtilityNode

Inherits:
Object
  • Object
show all
Defined in:
lib/formatted_string/formats/xml.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attributesObject

Returns the value of attribute attributes.



40
41
42
# File 'lib/formatted_string/formats/xml.rb', line 40

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



40
41
42
# File 'lib/formatted_string/formats/xml.rb', line 40

def children
  @children
end

#nameObject

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_htmlObject



105
106
107
# File 'lib/formatted_string/formats/xml.rb', line 105

def inner_html
  @children.join
end

#to_hashObject



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_htmlObject



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_sObject



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(/&lt;/,   "<").
        gsub(/&gt;/,   ">").
        gsub(/&quot;/, '"').
        gsub(/&apos;/, "'").
        gsub(/&amp;/,  "&")
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