Class: XMLBuilder

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

Constant Summary collapse

@@default_separator =
"  "

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(separator = @@default_separator) ⇒ XMLBuilder

Returns a new instance of XMLBuilder.



16
17
18
19
20
# File 'lib/xmlbuilder.rb', line 16

def initialize(separator=@@default_separator)
  @str = ""
  @depth = 0
  @separator = separator
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject

Takes the name of the tag to add, an optional string to put in the tag, an optional boolean parameter which signifies whether to make it a single tag or not, any options to put in the tag, and a block to evaluate between the opening and closing tags. Aliased to #method_missing to allow dynamic tag creation.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/xmlbuilder.rb', line 88

def add_element(name, *args)
  one_tag, internal, attrs = process_args args

  # logic time
  add indentation, ?<, name
  attrs.each do |attr, value|
	add " #{attr}=\"#{value}\""
  end
  if one_tag
	add " />\n"
	return self
  else
	add ?>
  end
  if internal
	add internal
  elsif block_given?
	@depth += 1
	add "\n"
	yield
	@depth -= 1
  end
  add indentation unless internal
  add "</#{name}>\n"
  return self
end

Instance Attribute Details

#separatorObject

Returns the value of attribute separator.



14
15
16
# File 'lib/xmlbuilder.rb', line 14

def separator
  @separator
end

#strObject (readonly) Also known as: to_s, to_str, inspect

separator set to two spaces by default, used in nesting



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

def str
  @str
end

Instance Method Details

#add(*strs) ⇒ Object

Adds a string (with no preprocessing) to the object’s string.



29
30
31
# File 'lib/xmlbuilder.rb', line 29

def add(*strs)
  @str << strs.flatten.join('')
end

#add_element(name, *args) ⇒ Object Also known as: method_missing

Takes the name of the tag to add, an optional string to put in the tag, an optional boolean parameter which signifies whether to make it a single tag or not, any options to put in the tag, and a block to evaluate between the opening and closing tags. Aliased to #method_missing to allow dynamic tag creation.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/xmlbuilder.rb', line 34

def add_element(name, *args)
  one_tag, internal, attrs = process_args args

  # logic time
  add indentation, ?<, name
  attrs.each do |attr, value|
	add " #{attr}=\"#{value}\""
  end
  if one_tag
	add " />\n"
	return self
  else
	add ?>
  end
  if internal
	add internal
  elsif block_given?
	@depth += 1
	add "\n"
	yield
	@depth -= 1
  end
  add indentation unless internal
  add "</#{name}>\n"
  return self
end

#clearObject

Sets the stored string to “” and the depth to 0.



23
24
25
26
# File 'lib/xmlbuilder.rb', line 23

def clear
  initialize(@separator)
  self
end