Sha256: 3bc077ebf93ad245c5e69672e2a9003fb72ffde4d51eaa69bec152bbc2f86452

Contents?: true

Size: 775 Bytes

Versions: 1

Compression:

Stored size: 775 Bytes

Contents

module MapFields
  class Mapping
    def initialize(mapping, fields)
      @mapping = mapping
      @fields = fields

      @mapping = mapping.each_with_object({}){ |arr, hash|
        key = arr[1].blank? ? nil : arr[1].to_i
        next if key.nil?
        value = arr[0].blank? ? nil : arr[0].to_i

        hash[key] = value
        hash[@fields[key]] = value
        hash[field_to_symbol(@fields[key])] = value
      }
    end

    def [](index)
      @mapping[index]
    end

    def is_mapped?(key)
      @mapping.has_key?(key)
    end

    def selected_mapping(column)
      @mapping.find{|k, v| return k if v == column && k.is_a?(Numeric) }
    end

    private
    def field_to_symbol(field)
      field.to_s.downcase.gsub(/[^a-z0-9]+/, '_').to_sym
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
map_fields-2.0.0.beta2 lib/map_fields/mapping.rb