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

#clone, #flatten_attrs, #format_attrs, format_string, found_strings, #generate_attrs, #generate_block, #include, quote_attr_value, registered_strings, #run_block, #string, #to_ary, #to_s

Constructor Details

#initialize(filename, &block) ⇒ XmlFile

Returns a new instance of XmlFile.



10
11
12
13
14
# File 'lib/android-xml/file.rb', line 10

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, attrs = {}, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/android-xml/file.rb', line 16

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

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

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

Instance Method Details

#generateObject



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

def generate
  output = "<!-- Do not edit this file. It was generated by AndroidXml. -->\n"
  output << generate_block
end

#outObject



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

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

#writeObject



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

def write
  dirname = File.dirname(self.filename)

  unless File.exists?(dirname)
    warn "\033[33m⚑\033[0m mkdir -p #{dirname}"
    FileUtils.mkdir_p(dirname)
  end

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