Class: AndroidXml::Tag
- Inherits:
-
Object
show all
- Defined in:
- lib/android-xml/tag.rb,
lib/android-xml/string.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(tag, attrs = {}, &block) ⇒ Tag
Returns a new instance of Tag.
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/android-xml/tag.rb', line 6
def initialize(tag, attrs={}, &block)
@buffer = []
@attrs = {}.merge(attrs)
@raw_tag = tag.to_s
if rename = Setup.tags[tag.to_s][:rename]
@tag = rename
else
@tag = tag.to_s.gsub('_', '-')
end
@generate = block
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, attrs = {}, &block) ⇒ Object
20
21
22
23
24
|
# File 'lib/android-xml/tag.rb', line 20
def method_missing(method_name, attrs={}, &block)
tag = Tag.new(method_name, attrs, &block)
include tag
tag
end
|
Instance Attribute Details
#is_root ⇒ Object
Returns the value of attribute is_root.
4
5
6
|
# File 'lib/android-xml/tag.rb', line 4
def is_root
@is_root
end
|
Instance Method Details
#attrs(whitespace) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/android-xml/tag.rb', line 42
def attrs(whitespace)
attrs = {}
attrs.merge!(Setup.all_tag[:defaults])
if is_root
attrs.merge!(Setup.root_tag[:defaults])
end
attrs.merge!(Setup.tags[@raw_tag][:defaults])
attrs.merge!(@attrs)
output = ''
is_first = true
attrs.each do |key, value|
next if value.nil?
key = key.to_s
if Setup.tags[@tag][:attrs].key?(key)
xml_key = Setup.tags[@tag][:attrs][key]
elsif Setup.all_tag[:attrs].key?(key)
xml_key = Setup.all_tag[:attrs][key]
elsif key.to_s.include?(':')
xml_key = key.to_s
else
xml_key = "android:#{key}"
end
if is_first
output << " #{xml_key}=\"#{value}\""
is_first = false
else
output << "\n#{whitespace}#{xml_key}=\"#{value}\""
end
end
output
end
|
#clone(attrs = {}, &block) ⇒ Object
26
27
28
29
|
# File 'lib/android-xml/tag.rb', line 26
def clone(attrs={}, &block)
block ||= @generate
Tag.new(@tag, @attrs.merge(attrs), &block)
end
|
#generate(tab = '') ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/android-xml/tag.rb', line 80
def generate(tab='')
whitespace = "#{tab} #{' ' * @tag.length}"
output = "#{tab}<#{@tag}#{attrs(whitespace)}"
if @generate
inside = generate_block(tab + Setup.tab)
if !inside || inside.strip.empty?
output << " />\n"
else
output << ">"
if inside.lstrip.start_with?('<')
output << "\n" << inside << "#{tab}</#{@tag}>\n"
else
output << inside << "</#{@tag}>\n"
end
end
else
output << " />\n"
end
output
end
|
#generate_block(tab = '') ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/android-xml/tag.rb', line 102
def generate_block(tab='')
return @block_output if @block_output
output = ''
if @generate
text = instance_exec(&@generate)
@buffer.each do |tag|
output << tag.generate(tab)
end
if text.is_a?(String)
if output.empty?
output << text
else
output << tab << text << "\n"
end
end
end
@block_output = output
end
|
#include(tag, &block) ⇒ Object
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/android-xml/tag.rb', line 31
def include(tag, &block)
if tag.is_a?(Tag)
if block_given?
tag = tag.clone(&block)
end
@buffer << tag
else
super
end
end
|
#out ⇒ Object
131
132
133
|
# File 'lib/android-xml/tag.rb', line 131
def out
puts to_s
end
|
#string(attrs = {}, &block) ⇒ Object
5
6
7
8
9
|
# File 'lib/android-xml/string.rb', line 5
def string(attrs={}, &block)
tag = Tag.new('string', attrs, &block)
include tag
tag
end
|
#to_ary ⇒ Object
127
128
129
|
# File 'lib/android-xml/tag.rb', line 127
def to_ary
[to_s]
end
|
#to_s ⇒ Object
123
124
125
|
# File 'lib/android-xml/tag.rb', line 123
def to_s
generate
end
|