Class: Amanzi::XML::Element
- Inherits:
-
Object
- Object
- Amanzi::XML::Element
show all
- Defined in:
- lib/amanzi/xml.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#<<(child) ⇒ Object
-
#[]=(key, value) ⇒ Object
-
#add_child(name, options = {}, &block) ⇒ Object
-
#attribute_text ⇒ Object
-
#comment(text) ⇒ Object
-
#contents ⇒ Object
-
#indent(offset = 0) ⇒ Object
-
#initialize(name, depth = 0) ⇒ Element
constructor
A new instance of Element.
-
#insert(index, child) ⇒ Object
-
#insert_child(index, name, options = {}, &block) ⇒ Object
-
#make_child(name, options = {}, &block) ⇒ Object
-
#method_missing(symbol, *args, &block) ⇒ Object
-
#ns(ns) ⇒ Object
Utility for setting the namepace in a method-chaining DSL.
-
#tag ⇒ Object
-
#to_s ⇒ Object
-
#to_xml(options = {}) ⇒ Object
Constructor Details
#initialize(name, depth = 0) ⇒ Element
Returns a new instance of Element.
8
9
10
11
12
13
|
# File 'lib/amanzi/xml.rb', line 8
def initialize(name,depth=0)
@name = name
@depth = depth
@children = []
@attributes = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
49
50
51
|
# File 'lib/amanzi/xml.rb', line 49
def method_missing(symbol,*args,&block)
add_child(Element.camelize(symbol), *args, &block)
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
7
8
9
|
# File 'lib/amanzi/xml.rb', line 7
def attributes
@attributes
end
|
#children ⇒ Object
Returns the value of attribute children.
6
7
8
|
# File 'lib/amanzi/xml.rb', line 6
def children
@children
end
|
#depth ⇒ Object
Returns the value of attribute depth.
6
7
8
|
# File 'lib/amanzi/xml.rb', line 6
def depth
@depth
end
|
#name ⇒ Object
Returns the value of attribute name.
6
7
8
|
# File 'lib/amanzi/xml.rb', line 6
def name
@name
end
|
#namespace ⇒ Object
Returns the value of attribute namespace.
7
8
9
|
# File 'lib/amanzi/xml.rb', line 7
def namespace
@namespace
end
|
Class Method Details
.camelize(symbol) ⇒ Object
46
47
48
|
# File 'lib/amanzi/xml.rb', line 46
def self.camelize(symbol)
symbol.to_s.gsub(/__/,':_').gsub(/(^|_)(\w)/){"#{$2.upcase}"}.gsub(/^(\w+):/){"#{$1.downcase}:"}
end
|
Instance Method Details
#<<(child) ⇒ Object
25
26
27
|
# File 'lib/amanzi/xml.rb', line 25
def << child
@children << child
end
|
#[]=(key, value) ⇒ Object
19
20
21
|
# File 'lib/amanzi/xml.rb', line 19
def []= (key,value)
@attributes[key] = value
end
|
#add_child(name, options = {}, &block) ⇒ Object
39
40
41
42
|
# File 'lib/amanzi/xml.rb', line 39
def add_child(name,options={},&block)
self << child = make_child(name,options,&block)
child
end
|
#attribute_text ⇒ Object
56
57
58
|
# File 'lib/amanzi/xml.rb', line 56
def attribute_text
attributes.empty? ? "" : " #{attributes.to_a.map{|x|"#{x[0]}=\"#{x[1]}\""}.join(' ')}"
end
|
43
44
45
|
# File 'lib/amanzi/xml.rb', line 43
def (text)
@children << Comment.new(text)
end
|
#contents ⇒ Object
59
60
61
62
|
# File 'lib/amanzi/xml.rb', line 59
def contents
children.length==1 && !children[0].is_a?(Element) && children[0].to_s ||
"\n#{indent(1)}#{children.map{|x|x.to_xml(@xml_options)}.join("\n#{indent(1)}")}\n#{indent}"
end
|
#indent(offset = 0) ⇒ Object
52
53
54
55
|
# File 'lib/amanzi/xml.rb', line 52
def indent(offset=0)
@indent ||= []
@indent[offset] ||= (0...(depth+offset)).inject(''){|a,v|a << (@xml_options[:tab] || "\t")}
end
|
#insert(index, child) ⇒ Object
22
23
24
|
# File 'lib/amanzi/xml.rb', line 22
def insert index, child
@children.insert index, child
end
|
#insert_child(index, name, options = {}, &block) ⇒ Object
35
36
37
38
|
# File 'lib/amanzi/xml.rb', line 35
def insert_child(index,name,options={},&block)
insert index, child = make_child(name,options,&block)
child
end
|
#make_child(name, options = {}, &block) ⇒ Object
28
29
30
31
32
33
34
|
# File 'lib/amanzi/xml.rb', line 28
def make_child(name,options={},&block)
child = Element.new(name,depth+1)
namespace && (child.namespace = namespace)
options.each{|k,v| child[k]=v}
block && block.call(child)
child
end
|
#ns(ns) ⇒ Object
Utility for setting the namepace in a method-chaining DSL
15
16
17
18
|
# File 'lib/amanzi/xml.rb', line 15
def ns(ns)
self.namespace = ns
self
end
|
#tag ⇒ Object
63
64
65
|
# File 'lib/amanzi/xml.rb', line 63
def tag
namespace ? "#{namespace}:#{name}" : name
end
|
#to_s ⇒ Object
70
71
72
|
# File 'lib/amanzi/xml.rb', line 70
def to_s
tag
end
|
#to_xml(options = {}) ⇒ Object
66
67
68
69
|
# File 'lib/amanzi/xml.rb', line 66
def to_xml(options={})
@xml_options = options
children.empty? ? "<#{tag}#{attribute_text}/>" : "<#{tag}#{attribute_text}>#{contents}</#{tag}>"
end
|