Sha256: 8ec9b95027ec9e767b5f28eaff3a076fdc65b0524a7617112e81b3689202d017

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

require "nokogiri"

module CfdiProcessor
  class DataExtractorBase
    attr_accessor :xml, :nokogiri_xml

    def initialize(xml)
      @xml = xml
      @nokogiri_xml = ::Nokogiri::XML(xml)
      @nokogiri_xml.remove_namespaces!

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

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

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

    # => 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.
    #
    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
  end
end







Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cfdi_processor-1.0.0 lib/cfdi_processor/data_extractor_base.rb