Class: Saxerator::XmlNode

Inherits:
Object
  • Object
show all
Defined in:
lib/saxerator/xml_node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, name, attributes) ⇒ XmlNode

Returns a new instance of XmlNode.



5
6
7
8
9
10
11
# File 'lib/saxerator/xml_node.rb', line 5

def initialize(config, name, attributes)
  @config = config
  self.name = name
  self.attributes = attributes
  self.children = []
  @text = false
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/saxerator/xml_node.rb', line 3

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



3
4
5
# File 'lib/saxerator/xml_node.rb', line 3

def children
  @children
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/saxerator/xml_node.rb', line 3

def name
  @name
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/saxerator/xml_node.rb', line 3

def type
  @type
end

Instance Method Details

#add_node(node) ⇒ Object



13
14
15
16
# File 'lib/saxerator/xml_node.rb', line 13

def add_node(node)
  @text = true if node.is_a? String
  children << node
end

#to_hashObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/saxerator/xml_node.rb', line 24

def to_hash
  if @text
    to_s
  else
    out = HashWithAttributes.new
    out.attributes = attributes

    @children.each do |child|
      name = child.name
      element = child.to_hash
      if out[name]
        if !out[name].is_a?(Array)
          out[name] = [out[name]]
        end
        out[name] << element
      else
        out[name] = element
      end
    end
    out
  end
end

#to_sObject



18
19
20
21
22
# File 'lib/saxerator/xml_node.rb', line 18

def to_s
  string = StringWithAttributes.new(@text ? children.join : children.to_s)
  string.attributes = attributes
  string
end