Class: AndroidXml::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/android-xml/tag.rb,
lib/android-xml/string.rb

Direct Known Subclasses

XmlFile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, attrs = {}, &block) ⇒ Tag

Returns a new instance of Tag.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/android-xml/tag.rb', line 70

def initialize(tag, attrs={}, &block)
  @buffer = []
  @attrs = {}.merge(Tag.flatten_attrs(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



84
85
86
87
88
# File 'lib/android-xml/tag.rb', line 84

def method_missing(method_name, attrs={}, &block)
  tag = Tag.new(method_name, attrs, &block)
  include tag
  tag
end

Instance Attribute Details

#is_rootObject

Returns the value of attribute is_root.



4
5
6
# File 'lib/android-xml/tag.rb', line 4

def is_root
  @is_root
end

Class Method Details

.flatten_attrs(attrs, prefix = '') ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/android-xml/tag.rb', line 6

def self.flatten_attrs(attrs, prefix='')
  flattened = {}
  attrs.each do |key, value|
    key = "#{prefix}#{key}"
    if value.is_a?(Hash)
      flattened.merge!(flatten_attrs(value, "#{key}_"))
    else
      flattened[key] = value
    end
  end

  flattened
end

.format_attrs(tag, attrs, whitespace) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/android-xml/tag.rb', line 20

def self.format_attrs(tag, attrs, whitespace)
  output = ''
  is_first = true
  attrs.each do |key, value|
    if value.nil?
      next
    end

    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 value =~ /^@string\/(\w+)$/
      Tag.found_strings << $1
    end

    quoted_value = Tag.quote_attr_value(value)
    if is_first
      output << " #{xml_key}=\"#{quoted_value}\""
      is_first = false
    else
      output << "\n#{whitespace}#{xml_key}=\"#{quoted_value}\""
    end
  end

  output
end

.format_string(text) ⇒ Object

isn’t there a method that does this for me!?



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/android-xml/string.rb', line 38

def self.format_string(text)
  text = text.dup
  {
    '\\' => '\\\\',
    "\n" => '\n',
    "\t" => '\t',
    "\b" => '\b',
    '"' => '\"',
    "'" => "\\\\'",
  }.each do |find, replace|
    text.gsub!(find, replace)
  end
  text
end

.found_stringsObject



10
11
12
# File 'lib/android-xml/string.rb', line 10

def found_strings
  @found_strings ||= []
end

.quote_attr_value(value) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/android-xml/tag.rb', line 56

def self.quote_attr_value(value)
  value = value.to_s.dup
  {
    '&' => '&amp;',
    '\'' => '&apos;',
    '"' => '&quot;',
    '<' => '&lt;',
    '>' => '&gt;',
  }.each do |find, replace|
    value.gsub!(find, replace)
  end
  value
end

.registered_stringsObject



14
15
16
# File 'lib/android-xml/string.rb', line 14

def registered_strings
  @registered_strings ||= []
end

Instance Method Details

#clone(attrs = {}, &block) ⇒ Object



90
91
92
93
# File 'lib/android-xml/tag.rb', line 90

def clone(attrs={}, &block)
  block ||= @generate
  Tag.new(@tag, @attrs.merge(Tag.flatten_attrs(attrs)), &block)
end

#generate(tab = '') ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/android-xml/tag.rb', line 120

def generate(tab='')
  whitespace = "#{tab}  #{' ' * @tag.length}"
  output = "#{tab}<#{@tag}#{generate_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_attrs(whitespace) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/android-xml/tag.rb', line 106

def generate_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)

  Tag.format_attrs(@tag, attrs, whitespace)
end

#generate_block(tab = '') ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/android-xml/tag.rb', line 142

def generate_block(tab='')
  return @block_output if @block_output

  output = ''
  if @generate
    text = run_block(&@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



95
96
97
98
99
100
101
102
103
104
# File 'lib/android-xml/tag.rb', line 95

def include(tag, &block)
  if tag.is_a?(Tag)
    if block_given?
      tag = tag.clone(&block)
    end
    @buffer << tag
  else
    super
  end
end

#outObject



175
176
177
# File 'lib/android-xml/tag.rb', line 175

def out
  puts to_s
end

#run_block(&block) ⇒ Object



163
164
165
# File 'lib/android-xml/tag.rb', line 163

def run_block(&block)
  instance_exec(&block)
end

#string(attrs = {}, &block) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/android-xml/string.rb', line 20

def string(attrs={}, &block)
  tag = Tag.new('string', attrs, &block)
  string = attrs['name'] || attrs[:name]
  raise "name is required in a <string> tag" unless string && ! string.empty?
  Tag.registered_strings << (string)
  include tag
  tag
end

#to_aryObject



171
172
173
# File 'lib/android-xml/tag.rb', line 171

def to_ary
  [to_s]
end

#to_sObject



167
168
169
# File 'lib/android-xml/tag.rb', line 167

def to_s
  generate
end