Class: CfdiProcessor::DataExtractorBase

Inherits:
Object
  • Object
show all
Defined in:
lib/cfdi_processor/data_extractor_base.rb

Direct Known Subclasses

StampedExtractor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ DataExtractorBase

Returns a new instance of DataExtractorBase.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cfdi_processor/data_extractor_base.rb', line 7

def initialize(xml)
  @xml = xml
  @nokogiri_xml = ::Nokogiri::XML(xml)
  @nokogiri_xml.remove_namespaces!
  I18n.load_path << File.expand_path("locale/en.yml", __dir__)
  I18n.locale = :en

  # => Hook methods:
  #
  # * Execute data extraction
  # * Translate data extracted
  #
  extract_data_from_xml
  translate_data
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

> Search for the translate pattern at the beginning of the method,

if it is found, look for the translation based on the end of the method name and the argument that is passed to it.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cfdi_processor/data_extractor_base.rb', line 35

def method_missing(name, *args, &block)
  if name.to_s.start_with?('_translate_')
    resource_name = name.to_s.sub('_translate_', '')

    instance_variable_get("@#{resource_name}").inject({}) do |translated, (key,value)|
      if value.kind_of?(Array)
        next if value.empty?
        items = value.each do |item|
          item.transform_keys!{ |k| I18n.t("#{args.first}.#{key}.#{k}") }
        end

        translated.merge!(I18n.t("#{args.first}.#{resource_name}.#{key}") => items)
      else
        translated.merge!(I18n.t("#{args.first}.#{resource_name}.#{key}") => value)
      end

      instance_variable_set("@#{resource_name}", translated)         
    end
  else 
    super
  end
end

Instance Attribute Details

#nokogiri_xmlObject

Returns the value of attribute nokogiri_xml.



5
6
7
# File 'lib/cfdi_processor/data_extractor_base.rb', line 5

def nokogiri_xml
  @nokogiri_xml
end

#xmlObject

Returns the value of attribute xml.



5
6
7
# File 'lib/cfdi_processor/data_extractor_base.rb', line 5

def xml
  @xml
end

Instance Method Details

#extract_data_from_xmlObject



23
24
25
# File 'lib/cfdi_processor/data_extractor_base.rb', line 23

def extract_data_from_xml
  raise 'Undefined abstract method: #extract_data_from_xml'
end

#translate_dataObject



27
28
29
# File 'lib/cfdi_processor/data_extractor_base.rb', line 27

def translate_data
  raise 'Undefined abstract method: #translate_data'
end