Sha256: b97aa90fa4dc36036fb2e483c4341d9f8dab3ef77ca4823507ef5f73b86f1db5

Contents?: true

Size: 654 Bytes

Versions: 2

Compression:

Stored size: 654 Bytes

Contents

# frozen_string_literal: true

module Format
  class Mapper
    def initialize(yml)
      @mappers = yml.fetch(:mappers, {})
    end

    def apply(raw_value, mapper_name)
      return raw_value unless mapper_name

      mappers.each do |name, fields|
        return mapper_value(fields, raw_value) if mapper_name.to_sym == name
      end

      raise "Mapper not found #{mapper_name}"
    end

    private

    attr_reader :mappers

    def mapper_value(fields, value)
      (fields[:options] || []).each do |option, values|
        return option.to_s if [values].flatten.include?(value.to_s)
      end
      fields[:default] || value
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xml_data_extractor-0.7.0 lib/src/format/mapper.rb
xml_data_extractor-0.6.0 lib/src/format/mapper.rb