Class: AndroidXml::XmlFile

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

Instance Attribute Summary collapse

Attributes inherited from Tag

#is_root

Instance Method Summary collapse

Methods inherited from Tag

#attrs, #generate_block, #to_ary, #to_s

Constructor Details

#initialize(filename, &block) ⇒ XmlFile

Returns a new instance of XmlFile.



7
8
9
10
11
# File 'lib/android-xml/file.rb', line 7

def initialize(filename, &block)
  @filename = filename
  super(nil, &block)
  AndroidXml.files << self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



13
14
15
16
17
18
19
# File 'lib/android-xml/file.rb', line 13

def method_missing(method_name, *args, &block)
  raise "There can be only one (new: #{method_name}, old: #{@root})" if @root

  @root = super
  @root.is_root = true
  @root
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/android-xml/file.rb', line 5

def filename
  @filename
end

Instance Method Details

#generateObject



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

def generate
  output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  output << "<!-- Do not edit this file. It was generated by AndroidXml -->\n"
  output << generate_block
end

#outObject



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

def out
  puts "---- filename: #{@filename} ----"
  super
  puts "---------------#{'-' * @filename.to_s.length}-----"
end

#writeObject



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

def write
  File.open(self.filename, 'w') do |f|
    f.write(self.to_s)
  end
  puts "\033[32m✓\033[0m #{self.filename}"
end