Class: Fede::XMLNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#proppertiesObject (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

Returns:

  • (Boolean)


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

def parent?
  !@children.empty?
end

#prop_stringObject



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_closeObject



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_openObject



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