Class: Fede::XMLNode
- Inherits:
-
Object
- Object
- Fede::XMLNode
- Defined in:
- lib/fede/xml_node.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#propperties ⇒ Object
readonly
Returns the value of attribute propperties.
Instance Method Summary collapse
- #add_child(child) ⇒ Object
- #add_children(children) ⇒ Object
- #children_string(indent_level) ⇒ Object
-
#initialize(tag_name, content: nil, cdata: false, propperties: {}) ⇒ XMLNode
constructor
A new instance of XMLNode.
- #open_tag(indent_level) ⇒ Object
- #parent? ⇒ Boolean
- #prop_string ⇒ Object
- #set_propperty(name, value) ⇒ Object
- #tag_close ⇒ Object
- #tag_middle(indent_level) ⇒ Object
- #tag_open ⇒ Object
- #to_s(indent_level = 0) ⇒ Object
Constructor Details
#initialize(tag_name, content: nil, cdata: false, propperties: {}) ⇒ XMLNode
Returns a new instance of XMLNode.
5 6 7 8 9 10 11 |
# File 'lib/fede/xml_node.rb', line 5 def initialize(tag_name, content: nil, cdata: false, propperties: {}) @tag_name = tag_name @content = content @cdata = cdata @propperties = propperties @children = [] end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
3 4 5 |
# File 'lib/fede/xml_node.rb', line 3 def children @children end |
#propperties ⇒ Object (readonly)
Returns the value of attribute propperties.
3 4 5 |
# File 'lib/fede/xml_node.rb', line 3 def propperties @propperties end |
Instance Method Details
#add_child(child) ⇒ Object
21 22 23 |
# File 'lib/fede/xml_node.rb', line 21 def add_child(child) @children << child end |
#add_children(children) ⇒ Object
25 26 27 28 29 |
# File 'lib/fede/xml_node.rb', line 25 def add_children(children) raise TypeError 'Children must be an Array of nodes' unless children.is_a? Array @children += children end |
#children_string(indent_level) ⇒ Object
48 49 50 |
# File 'lib/fede/xml_node.rb', line 48 def children_string(indent_level) @children.reduce('') { |prev, value| "#{prev}#{value.to_s indent_level}" } end |
#open_tag(indent_level) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/fede/xml_node.rb', line 31 def open_tag(indent_level) if @cdata "<#{@tag_name}>\n#{"\t" * indent_level}<![CDATA[" elsif @propperties.empty? "<#{@tag_name}>" else prop_string = @propperties.reduce('') { |prev, values| "#{prev} #{values[0]}='#{values[1]}'" } "<#{@tag_name} #{prop_string.strip}#{@content ? '' : ' /'}>" end end |
#parent? ⇒ Boolean
13 14 15 |
# File 'lib/fede/xml_node.rb', line 13 def parent? !@children.empty? end |
#prop_string ⇒ Object
42 43 44 45 46 |
# File 'lib/fede/xml_node.rb', line 42 def prop_string return '' if @propperties.empty? @propperties.reduce('') { |prev, values| "#{prev} #{values[0]}='#{values[1]}'" } end |
#set_propperty(name, value) ⇒ Object
17 18 19 |
# File 'lib/fede/xml_node.rb', line 17 def set_propperty(name, value) @propperties[name] = value end |
#tag_close ⇒ Object
68 69 70 |
# File 'lib/fede/xml_node.rb', line 68 def tag_close parent? || @content ? "</#{@tag_name}>\n" : " />\n" end |
#tag_middle(indent_level) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fede/xml_node.rb', line 56 def tag_middle(indent_level) return '' unless parent? || @content if @cdata ">\n#{"\t" * (indent_level + 1)}<![CDATA[#{@content}]]>\n#{"\t" * indent_level}" elsif parent? ">\n#{children_string(indent_level + 1)}#{"\t" * indent_level}" else ">#{@content}" end end |
#tag_open ⇒ Object
52 53 54 |
# File 'lib/fede/xml_node.rb', line 52 def tag_open "<#{@tag_name}#{prop_string}" end |
#to_s(indent_level = 0) ⇒ Object
72 73 74 |
# File 'lib/fede/xml_node.rb', line 72 def to_s(indent_level = 0) "#{"\t" * indent_level}#{tag_open}#{tag_middle(indent_level)}#{tag_close}" end |