Class: Saxerator::XmlNode
- Inherits:
-
Object
- Object
- Saxerator::XmlNode
- Defined in:
- lib/saxerator/xml_node.rb
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.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #add_node(node) ⇒ Object
-
#initialize(config, name, attributes) ⇒ XmlNode
constructor
A new instance of XmlNode.
- #to_hash ⇒ Object
- #to_s ⇒ Object
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
#attributes ⇒ Object
Returns the value of attribute attributes.
3 4 5 |
# File 'lib/saxerator/xml_node.rb', line 3 def attributes @attributes end |
#children ⇒ Object
Returns the value of attribute children.
3 4 5 |
# File 'lib/saxerator/xml_node.rb', line 3 def children @children end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/saxerator/xml_node.rb', line 3 def name @name end |
#type ⇒ Object
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_hash ⇒ Object
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_s ⇒ Object
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 |