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.



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_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

.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



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/android-xml/tag.rb', line 89

def self.quote_attr_value(value)
  value = value.to_s.dup
  {
    '&' => '&',
    '\'' => ''',
    '"' => '"',
    '<' => '&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

#attrs(whitespace) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# 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)

  format_attrs(@tag, attrs, whitespace)
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

#format_attrs(tag, attrs, whitespace, is_first = true) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/android-xml/tag.rb', line 56

def format_attrs(tag, attrs, whitespace, is_first=true)
  output = ''
  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 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

#generate(tab = '') ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/android-xml/tag.rb', line 103

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/android-xml/tag.rb', line 125

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



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

#outObject



158
159
160
# File 'lib/android-xml/tag.rb', line 158

def out
  puts to_s
end

#run_block(&block) ⇒ Object



146
147
148
# File 'lib/android-xml/tag.rb', line 146

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



154
155
156
# File 'lib/android-xml/tag.rb', line 154

def to_ary
  [to_s]
end

#to_sObject



150
151
152
# File 'lib/android-xml/tag.rb', line 150

def to_s
  generate
end