Sha256: f6ed55de580bef2934693f0043b5d51ffb91b4a97a944c39ecb83e8be41a32d4

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require_relative "configuration"

module Necromancer
  # Abstract converter used internally as a base for other converters
  #
  # @api private
  class Converter
    # Create an abstract converter
    #
    # @param [Object] source
    #   the source object type
    #
    # @param [Object] target
    #   the target object type
    #
    # @api public
    def initialize(source = nil, target = nil)
      @source = source if source
      @target = target if target
      @config ||= Configuration.new
    end

    # Run converter
    #
    # @api private
    def call(*)
      raise NotImplementedError
    end

    # Creates anonymous converter
    #
    # @api private
    def self.create(&block)
      Class.new(self) do
        define_method(:initialize) { |*a| block.(self, *a) }

        define_method(:call) { |value| convert.(value) }
      end.new
    end

    # Fail with conversion type error
    #
    # @param [Object] value
    #   the value that cannot be converted
    #
    # @api private
    def raise_conversion_type(value)
      raise ConversionTypeError, "'#{value}' could not be converted " \
                                 "from `#{source}` into `#{target}`"
    end

    attr_accessor :source

    attr_accessor :target

    attr_accessor :convert

   # protected

    attr_reader :config
  end # Converter
end # Necromancer

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
necromancer-0.7.0 lib/necromancer/converter.rb