Sha256: 090fd6e99159161b7984dd60a160363981bc24afbfeb63f97ec30446c06c1982

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

module Vedeu

  module Coercers

    # Provides the mechanism to convert a value into another value.
    #
    # @api private
    #
    class Coercer

      include Vedeu::Common

      # @param (see #initialize)
      # @return (see #coerce)
      def self.coerce(value)
        new(value).coerce
      end

      # Returns a new instance of the Vedeu::Coercers::Coercer
      # subclass.
      #
      # @param value [void]
      # @return [Vedeu::Coercers::Coercer]
      def initialize(value)
        @value = value
      end

      # @macro raise_not_implemented
      # @return [void]
      def coerce
        raise Vedeu::Error::NotImplemented, 'Subclasses implement this.'
      end

      protected

      # @!attribute [r] value
      # @return [void]
      attr_reader :value

      private

      # @return [Boolean]
      def coerced?
        value.is_a?(klass)
      end

      # @macro raise_fatal
      # @return [void]
      def incoercible!
        raise Vedeu::Error::Fatal,
              "Vedeu cannot coerce a '#{value.class.name}' into a '#{klass}'."
      end

      # @macro raise_not_implemented
      # @return [Class]
      def klass
        raise Vedeu::Error::NotImplemented, 'Subclasses implement this.'
      end

    end # Coercer

  end # Coercers

end # Vedeu

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.8.15 lib/vedeu/coercers/coercer.rb
vedeu-0.8.14 lib/vedeu/coercers/coercer.rb
vedeu-0.8.13 lib/vedeu/coercers/coercer.rb