Class: Xout
- Inherits:
-
Object
- Object
- Xout
- Defined in:
- lib/xout.rb
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Instance Method Summary collapse
- #add_child(node) ⇒ Object
- #child(name, *args, &block) ⇒ Object
-
#initialize(node_name, *args) {|_self| ... } ⇒ Xout
constructor
A new instance of Xout.
- #to_xml ⇒ Object (also: #to_s)
- #to_xml_doc ⇒ Object
Constructor Details
#initialize(node_name, *args) {|_self| ... } ⇒ Xout
Returns a new instance of Xout.
5 6 7 8 9 10 11 |
# File 'lib/xout.rb', line 5 def initialize node_name, *args, &block @children = [] attrs = args.last.is_a?(Hash) ? args.pop : {} text = args.empty? ? '' : args.pop.to_s @name, @text, @attrs = node_name, text, attrs yield self if block_given? end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
3 4 5 |
# File 'lib/xout.rb', line 3 def attrs @attrs end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
3 4 5 |
# File 'lib/xout.rb', line 3 def children @children end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/xout.rb', line 3 def name @name end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
3 4 5 |
# File 'lib/xout.rb', line 3 def text @text end |
Instance Method Details
#add_child(node) ⇒ Object
17 18 19 |
# File 'lib/xout.rb', line 17 def add_child node children << node end |
#child(name, *args, &block) ⇒ Object
13 14 15 |
# File 'lib/xout.rb', line 13 def child name, *args, &block add_child self.class.new(name, *args, &block) end |
#to_xml ⇒ Object Also known as: to_s
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/xout.rb', line 21 def to_xml xml = ["<#{name}#{create_attrs(attrs)}"] if not text.empty? or not children.empty? xml << ">#{escape_text text.to_s}" xml += children.map{|child|child.to_xml} xml << "</#{name}>" else xml << '/>' end xml.join end |
#to_xml_doc ⇒ Object
35 36 37 |
# File 'lib/xout.rb', line 35 def to_xml_doc '<?xml version="1.0" encoding="UTF-8"?>' + to_xml end |