Class: C11n::Importer::Android

Inherits:
Object
  • Object
show all
Defined in:
lib/c11n/importer/android.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Android

Returns a new instance of Android.



9
10
11
12
13
14
# File 'lib/c11n/importer/android.rb', line 9

def initialize(options = {})
  @path = options[:path]
  @locale = options[:locale]
  @types = {}
  @categories = {}
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



7
8
9
# File 'lib/c11n/importer/android.rb', line 7

def categories
  @categories
end

#typesObject (readonly)

Returns the value of attribute types.



7
8
9
# File 'lib/c11n/importer/android.rb', line 7

def types
  @types
end

Instance Method Details

#documentObject



20
21
22
# File 'lib/c11n/importer/android.rb', line 20

def document
  Nokogiri::XML(file)
end

#fileObject



16
17
18
# File 'lib/c11n/importer/android.rb', line 16

def file
  File.open(@path, 'r')
end

#importObject



24
25
26
# File 'lib/c11n/importer/android.rb', line 24

def import
  { @locale.to_sym => to_hash }
end

#to_hashObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/c11n/importer/android.rb', line 28

def to_hash
  document.at_xpath('resources').elements.inject({}) do |result, element|
    if element.name == 'string'
      @types[element['name']] = C11n::Types::CDATA if element.children.any? { |child| child.cdata? }
      result.merge(element['name'] => element.text.strip)
    elsif element.name == 'string-array'
      @types[element['name']] = C11n::Types::ARRAY
      result.merge(element['name'] => element.elements.map(&:text))
    end
  end
end