Class: AndroidXml::Tag

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

Direct Known Subclasses

XmlFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag, *args, &block) ⇒ Tag

Returns a new instance of Tag.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/android-xml/tag.rb', line 6

def initialize(tag, *args, &block)
  @buffer = []
  @attrs = {}
  @raw_tag = tag.to_s
  @text = nil

  if rename = AndroidXml.tags[tag.to_s][:rename]
    @tag = rename
  else
    @tag = tag.to_s.gsub('_', '-')
  end

  args.each do |arg|
    if arg.is_a?(Hash)
      @attrs.merge!(arg)
    elsif arg.is_a?(String)
      @text = arg
    else
      raise ArgumentError.new("Unknown argument #{arg.inspect} in #{self.class}#new")
    end
  end

  @generate = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



31
32
33
34
35
# File 'lib/android-xml/tag.rb', line 31

def method_missing(method_name, *args, &block)
  tag = Tag.new(method_name, *args, &block)
  @buffer << 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

Instance Method Details

#attrs(whitespace) ⇒ Object



37
38
39
40
41
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
# File 'lib/android-xml/tag.rb', line 37

def attrs(whitespace)
  attrs = {}

  attrs.merge!(AndroidXml.all_tag[:defaults])
  if is_root
    attrs.merge!(AndroidXml.root_tag[:defaults])
  end

  attrs.merge!(AndroidXml.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 AndroidXml.tags[@tag][:attrs].key?(key)
      xml_key = AndroidXml.tags[@tag][:attrs][key]
    elsif AndroidXml.all_tag[:attrs].key?(key)
      xml_key = AndroidXml.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

#generate(tab = '') ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/android-xml/tag.rb', line 75

def generate(tab='')
  whitespace = "#{tab}  #{' ' * @tag.length}"
  output = "#{tab}<#{@tag}#{attrs(whitespace)}"
  if @generate
    inside = generate_block(tab + AndroidXml.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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/android-xml/tag.rb', line 97

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

#outObject



126
127
128
# File 'lib/android-xml/tag.rb', line 126

def out
  puts to_s
end

#to_aryObject



122
123
124
# File 'lib/android-xml/tag.rb', line 122

def to_ary
  [to_s]
end

#to_sObject



118
119
120
# File 'lib/android-xml/tag.rb', line 118

def to_s
  generate
end