Class: Headdesk::Apk::Resources::XmlCollection
- Inherits:
-
Object
- Object
- Headdesk::Apk::Resources::XmlCollection
- Defined in:
- lib/headdesk/apk/resources.rb
Overview
Collection of XML values for specific locale/api/etc
Class Method Summary collapse
- .api_versions(modifiers) ⇒ Object
-
.item_elements(xml) ⇒ Object
:reek:TooManyStatements.
-
.named_elements(xml) ⇒ Object
:reek:TooManyStatements.
Instance Method Summary collapse
-
#initialize(path, type, modifiers = {}) ⇒ XmlCollection
constructor
:reek:NestedIterators and :reek:TooManyStatements.
- #method_missing(method_name, *arguments, &block) ⇒ Object
- #respond_to_missing?(method_name, include_all) ⇒ Boolean
Constructor Details
#initialize(path, type, modifiers = {}) ⇒ XmlCollection
:reek:NestedIterators and :reek:TooManyStatements
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/headdesk/apk/resources.rb', line 32 def initialize(path, type, modifiers = {}) @resources = {} globspec = File.join(path, 'res', "#{type}{#{XmlCollection.api_versions(modifiers).join(',')}}", '*.xml') Dir.glob(globspec).each do |file_name| xml = File.open(file_name) { |file| Nokogiri::XML(file) } @resources.merge! XmlCollection.named_elements(xml) @resources.merge! XmlCollection.item_elements(xml) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
47 48 49 50 51 |
# File 'lib/headdesk/apk/resources.rb', line 47 def method_missing(method_name, *arguments, &block) super unless @resources.include?(method_name.to_s) OpenStruct.new(@resources[method_name.to_s]) end |
Class Method Details
.api_versions(modifiers) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/headdesk/apk/resources.rb', line 53 def self.api_versions(modifiers) mods = [nil] if modifiers.key?(:v) (1..modifiers[:v].to_i).each do |api_version| mods << "-v#{api_version}" end end mods end |
.item_elements(xml) ⇒ Object
:reek:TooManyStatements
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/headdesk/apk/resources.rb', line 86 def self.item_elements(xml) item_elements = %i[drawable] resources = {} xml.xpath("//item[#{item_elements.map { |elem| "contains(@type, '#{elem}')" }.join('or')}]").each do |elem| type = elem.attributes['type'].to_s name = elem.attributes['name'].to_s resources[type] ||= {} resources[type][name] = elem.text end resources end |
.named_elements(xml) ⇒ Object
:reek:TooManyStatements
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/headdesk/apk/resources.rb', line 64 def self.named_elements(xml) named_elements = %i[string integer color bool] resources = {} xml.xpath("//#{named_elements.join('|//')}").each do |elem| type = elem.name.to_s name = elem.attributes['name'].to_s resources[type] ||= {} resources[type][name] = case type when 'bool' elem.text == true.to_s when 'integer' elem.text.to_i else elem.text end end resources end |
Instance Method Details
#respond_to_missing?(method_name, include_all) ⇒ Boolean
43 44 45 |
# File 'lib/headdesk/apk/resources.rb', line 43 def respond_to_missing?(method_name, include_all) @resources.include?(method_name.to_s) || super end |