Sha256: cf7e51191be53853bdd9323f7ab9b33d0a5737a259f9378cd511c49eece30062

Contents?: true

Size: 1.09 KB

Versions: 13

Compression:

Stored size: 1.09 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 { |k, v| [k, v.is_a?(Hash) || v.is_a?(Array) ? convert(v, types, convert_fn) : convert_fn[types[k]].call(v)] }]
      else 
        object
      end
    end
    
    def parsing_fn
      @parsing ||= Hash.new(Proc.new { |v| v }).tap { |p|
        p[:date] = Proc.new { |v| ::Date.parse(v) rescue nil }
        p[:integer] = Proc.new { |v| v.to_i }
        p[:float] = Proc.new { |v| v.to_f }
        p[:boolean] = Proc.new { |v| v == "S" }
      }
    end

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
afipws-1.0.3 lib/afipws/type_conversions.rb
afipws-1.0.2 lib/afipws/type_conversions.rb
afipws-1.0.1 lib/afipws/type_conversions.rb
afipws-1.0.0 lib/afipws/type_conversions.rb
afipws-0.2.0 lib/afipws/type_conversions.rb
afipws-0.1.8 lib/afipws/type_conversions.rb
afipws-0.1.7 lib/afipws/type_conversions.rb
afipws-0.1.6 lib/afipws/type_conversions.rb
afipws-0.1.4 lib/afipws/type_conversions.rb
afipws-0.1.3 lib/afipws/type_conversions.rb
afipws-0.1.2 lib/afipws/type_conversions.rb
afipws-0.1.1 lib/afipws/type_conversions.rb
afipws-0.1.0 lib/afipws/type_conversions.rb