Module: AndroidXml

Defined in:
lib/android-xml/tag.rb,
lib/android-xml/file.rb,
lib/android-xml/main.rb,
lib/android-xml/version.rb,
lib/android-xml/defaults.rb

Defined Under Namespace

Classes: Tag, XmlFile

Constant Summary collapse

ROOT =
'__root_node_with_a_long_special_name__'
ALL =
'__all_node_with_a_long_special_name__'
Version =
'1.1.0'

Class Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, filename, &block) ⇒ Object (private)



115
116
117
118
119
120
# File 'lib/android-xml/main.rb', line 115

def method_missing(method_name, filename, &block)
  xml_file = file(filename) do
    send(method_name, &block)
  end
  xml_file
end

Class Method Details

.all(&block) ⇒ Object



75
76
77
# File 'lib/android-xml/main.rb', line 75

def all(&block)
  tag(ALL, &block)
end

.all_tagObject



71
72
73
# File 'lib/android-xml/main.rb', line 71

def all_tag
  tags[ALL]
end

.defaults(attrs) ⇒ Object



111
112
113
# File 'lib/android-xml/main.rb', line 111

def defaults(attrs)
  tags[@context][:defaults].merge!(attrs)
end

.diff_allObject



34
35
36
37
# File 'lib/android-xml/main.rb', line 34

def diff_all
  files.each do |xml_file|
  end
end

.file(filename, &block) ⇒ Object



11
12
13
# File 'lib/android-xml/main.rb', line 11

def file(filename, &block)
  XmlFile.new(filename, &block)
end

.filesObject



15
16
17
# File 'lib/android-xml/main.rb', line 15

def files
  @files ||= []
end

.method_missing(method_name, filename, &block) ⇒ Object



115
116
117
118
119
120
# File 'lib/android-xml/main.rb', line 115

def method_missing(method_name, filename, &block)
  xml_file = file(filename) do
    send(method_name, &block)
  end
  xml_file
end

.output_allObject



27
28
29
30
31
32
# File 'lib/android-xml/main.rb', line 27

def output_all
  files.each do |xml_file|
    xml_file.out
    puts
  end
end

.rename(attrs) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/android-xml/main.rb', line 101

def rename(attrs)
  if attrs.is_a?(Hash)
    attrs.each do |attr_name, attr_rename|
      tags[@context][:attrs][attr_name.to_s] = attr_rename.to_s
    end
  else
    tags[@context][:attrs][attrs.to_s] = attrs.to_s
  end
end

.resetObject



53
54
55
56
57
# File 'lib/android-xml/main.rb', line 53

def reset
  @tab = nil
  @tags = nil
  @files = nil
end

.root(&block) ⇒ Object



67
68
69
# File 'lib/android-xml/main.rb', line 67

def root(&block)
  tag(ROOT, &block)
end

.root_tagObject



63
64
65
# File 'lib/android-xml/main.rb', line 63

def root_tag
  tags[ROOT]
end

.setup(&block) ⇒ Object



59
60
61
# File 'lib/android-xml/main.rb', line 59

def setup(&block)
  instance_exec(&block)
end

.setup_defaultsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/android-xml/defaults.rb', line 7

def setup_defaults
  setup do
    # assign the xmlns to all root nodes
    root do
      defaults 'xmlns:android' => 'http://schemas.android.com/apk/res/android'
    end

    all do
      # suppress 'android:' prefix to all style attributes
      rename :style
    end

    # disable the xmlns attribute on the resource node
    tag :resources do
      defaults 'xmlns:android' => nil
    end

    # remove the 'android:' prefix
    tag :string, :style, :item, :color do
      rename :name
    end
    tag :style do
      rename :parent
    end
    tag :manifest do
      rename :package
    end

    # creates a couple "tag shortcuts"
    tag :main_action => 'action' do
      defaults name: 'android.intent.action.MAIN'
    end
    tag :launcher_category => 'category' do
      defaults name: 'android.intent.category.LAUNCHER'
    end
  end
end

.tabObject



49
50
51
# File 'lib/android-xml/main.rb', line 49

def tab
  @tab ||= '    '
end

.tab=(value) ⇒ Object



7
8
9
# File 'lib/android-xml/main.rb', line 7

def tab=(value)
  @tab = value
end

.tag(*names, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/android-xml/main.rb', line 79

def tag(*names, &block)
  context_was = @context

  names.each do |name|
    if name.is_a?(Hash)
      @context = nil
      name.each do |shortcut, tag_name|
        raise "There can be only one key-value pair" if @context

        @context = shortcut.to_s
        tags[@context][:rename] = tag_name.to_s
      end
    else
      @context = name.to_s
    end

    instance_exec(&block)
  end

  @context = context_was
end

.tagsObject



39
40
41
42
43
44
45
46
47
# File 'lib/android-xml/main.rb', line 39

def tags
  @tags ||= Hash.new do |hash, key|
    hash[key] = {
      attrs: {},
      defaults: {},
      rename: nil,
    }
  end
end

.write_allObject



19
20
21
22
23
24
25
# File 'lib/android-xml/main.rb', line 19

def write_all
  files.each do |xml_file|
    xml_file.write
  end

  @files = nil
end