Class: Xout

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_name, *args) {|_self| ... } ⇒ Xout

Returns a new instance of Xout.

Yields:

  • (_self)

Yield Parameters:

  • _self (Xout)

    the object that the method was called on



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

#attrsObject (readonly)

Returns the value of attribute attrs.



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

def attrs
  @attrs
end

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#textObject (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_xmlObject 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_docObject



35
36
37
# File 'lib/xout.rb', line 35

def to_xml_doc
  '<?xml version="1.0" encoding="UTF-8"?>' + to_xml
end