Sha256: 9573fad91319e3f55248cf9ce7f237716353a8bec68d245ff92c502f6f1da02a

Contents?: true

Size: 1.08 KB

Versions: 13

Compression:

Stored size: 1.08 KB

Contents

module Afipws
  module TypeConversions
    def r2x x, types
      convert x, types, marshall_fn
    end

    def x2r x, types
      convert x, types, parsing_fn
    end

    private

    # Hace una conversión recursiva de tipo de todos los values según los tipos de las keys indicados en types
    def convert object, types, convert_fn
      case object
      when Array then
        object.map { |e| convert e, types, convert_fn }
      when Hash then
        Hash[object.map do |k, v|
          [k, v.is_a?(Hash) || v.is_a?(Array) ? convert(v, types, convert_fn) : convert_fn[types[k]].call(v)]
        end]
      else
        object
      end
    end

    def parsing_fn
      @parsing_fn ||= Hash.new(proc { |v| v }).tap { |p|
        p[:date] = proc { |v| ::Date.parse(v) rescue nil }
        p[:integer] = proc { |v| v.to_i }
        p[:float] = proc { |v| v.to_f }
        p[:boolean] = proc { |v| v == 'S' }
      }
    end

    def marshall_fn
      @marshall_fn ||= Hash.new(proc { |other| other }).tap do |p|
        p[:date] = proc { |date| date.strftime('%Y%m%d') }
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
afipws-2.3.0 lib/afipws/type_conversions.rb
afipws-2.2.0 lib/afipws/type_conversions.rb
afipws-2.1.0 lib/afipws/type_conversions.rb
afipws-2.0.0 lib/afipws/type_conversions.rb
afipws-1.3.2 lib/afipws/type_conversions.rb
afipws-1.3.1 lib/afipws/type_conversions.rb
afipws-1.3.0 lib/afipws/type_conversions.rb
afipws-1.2.3 lib/afipws/type_conversions.rb
afipws-1.2.2 lib/afipws/type_conversions.rb
afipws-1.2.1 lib/afipws/type_conversions.rb
afipws-1.2.0 lib/afipws/type_conversions.rb
afipws-1.1.0 lib/afipws/type_conversions.rb
afipws-1.0.4 lib/afipws/type_conversions.rb