Class: OXM::Object
- Inherits:
-
Object
show all
- Defined in:
- lib/oxm/object.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(tag, attrs = {}) ⇒ OXM::Object
125
126
127
128
129
130
131
|
# File 'lib/oxm/object.rb', line 125
def initialize tag, attrs = {}
@tag = tag
@attrs = attrs
@nodes = {}
@cdata = false
@data = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symb, *args) ⇒ Object
139
140
141
142
143
144
145
|
# File 'lib/oxm/object.rb', line 139
def method_missing(symb, *args)
if @nodes.has_key?(symb.to_s)
return @nodes[symb.to_s]
else
raise NoMethodError.new("undefined method or attribute `#{symb}'")
end
end
|
Instance Attribute Details
Returns the value of attribute tag.
3
4
5
|
# File 'lib/oxm/object.rb', line 3
def tag
@tag
end
|
Instance Method Details
#[](attr) ⇒ String
70
71
72
|
# File 'lib/oxm/object.rb', line 70
def [] attr
@attrs[attr.to_s]
end
|
#[]=(attr, val) ⇒ String
77
78
79
|
# File 'lib/oxm/object.rb', line 77
def []= attr, val
@attrs[attr.to_s] = val
end
|
#add_node(tag, object) ⇒ OXM::Object
95
96
97
98
99
|
# File 'lib/oxm/object.rb', line 95
def add_node tag, object
@nodes[tag] ||= []
@nodes[tag] << object
self
end
|
#attributes ⇒ Hash
82
83
84
|
# File 'lib/oxm/object.rb', line 82
def attributes
@attrs
end
|
#cdata=(val) ⇒ String
20
21
22
23
|
# File 'lib/oxm/object.rb', line 20
def cdata= val
@cdata = true
@data = val
end
|
#cdata? ⇒ Boolean
26
27
28
|
# File 'lib/oxm/object.rb', line 26
def cdata?
@cdata
end
|
Compacts the object by removing empty child elements and then collapsing element Arrays. After compaction, single-element arrays are collapsed, and empty arrays become nil.
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/oxm/object.rb', line 104
def compact!
@nodes.each do |key, value|
if value.is_a?(Array)
value = value.reject(&:empty?)
@nodes[key] =
case value.length
when 0
nil
when 1
value.first
else
value
end
end
end
self
end
|
#content ⇒ String/NilClass
6
7
8
|
# File 'lib/oxm/object.rb', line 6
def content
@data
end
|
#content=(val) ⇒ String
13
14
15
16
|
# File 'lib/oxm/object.rb', line 13
def content= val
@cdata = false
@data = val
end
|
#elements ⇒ Hash
Also known as:
children
87
88
89
|
# File 'lib/oxm/object.rb', line 87
def elements
@nodes
end
|
#empty? ⇒ Boolean
Return if the element is empty (no text value, no attributes and no child elements)
134
135
136
|
# File 'lib/oxm/object.rb', line 134
def empty?
self.to_s.empty? && self.attributes.empty? && self.elements.empty?
end
|
#inspect ⇒ String
64
65
66
|
# File 'lib/oxm/object.rb', line 64
def inspect
to_xml
end
|
#to_s ⇒ String
59
60
61
|
# File 'lib/oxm/object.rb', line 59
def to_s
@data.to_s
end
|
#to_xml ⇒ String
Returns XML expression for this object.
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/oxm/object.rb', line 31
def to_xml
io = StringIO.new
builder = Builder::XmlMarkup.new(:target => io)
build_node = lambda do |node|
builder.tag!(node.tag, node.attributes) do
node.elements.each do |tag, elements|
next if elements.nil?
elements = [elements] unless elements.is_a? Array
elements.each do |child|
build_node.call child
end
end
if node.content
if node.cdata?
builder.cdata! node.to_s
else
builder.text! node.to_s
end
end
end
end
build_node.call self
io.string
end
|