Class: XMLBuilder

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

Overview

XMLBuilder is a library that allows you to easily create XML.

Licensed under MIT

Written by Coderz

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.



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

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.



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
114
# File 'lib/xmlbuilder.rb', line 89

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.



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

def separator
  @separator
end

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

separator set to two spaces by default, used in nesting



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

def str
  @str
end

Instance Method Details

#add(*strs) ⇒ Object

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



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

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.



33
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
# File 'lib/xmlbuilder.rb', line 33

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.



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

def clear
  initialize(@separator)
  self
end