Sha256: 24006e9d90996b67f8230abf66a24f9389723ec7237388ade95935035b6bf297

Contents?: true

Size: 937 Bytes

Versions: 1

Compression:

Stored size: 937 Bytes

Contents

require_relative 'integer'
require_relative 'float'
require_relative 'datetime'
require_relative 'date'
require_relative 'string'

module Estratto
  module Data
    class InvalidCoercionType < StandardError; end
    class DataCoercionError < StandardError; end
    class Coercer
      attr_reader :data, :index, :type, :formats

      def initialize(data:, index:, type: 'String', formats: {})
        @data = data
        @index = index
        @type = type
        @formats = formats
      end

      def build
        target_coercer.coerce
      rescue StandardError => error
        raise DataCoercionError,
              "Error when coercing #{data} on line #{index}, raising: #{error.message}"
      end

      def target_coercer
        Object.const_get("Estratto::Data::#{type}").new(data, formats)
      rescue NameError
        raise InvalidCoercionType, "Does not exists coercer class for #{type}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
estratto-1.0.3 lib/estratto/data/coercer.rb