Sha256: 39acbd524481bf01d51fe3219219377de7dcec70510ad5a3665f784e5021287f

Contents?: true

Size: 1013 Bytes

Versions: 8

Compression:

Stored size: 1013 Bytes

Contents

module Virtus
  class Attribute

    # Coercer accessor wrapper
    #
    # @api private
    class Coercer
      attr_reader :method

      # Initialize a new coercer object
      #
      # @param [Object] coercers accessor
      # @param [Symbol] coercion method
      #
      # @return [undefined]
      #
      # @api private
      def initialize(coercers, method)
        @coercers = coercers
        @method   = method
      end

      # Coerce given value
      #
      # @return [Object]
      #
      # @api private
      def call(value)
        self[value.class].public_send(@method, value)
      rescue ::Coercible::UnsupportedCoercion
        value
      end

      # @api public
      def success?(primitive, value)
        self[primitive].coerced?(value)
      end

      # Return coercer object for the given type
      #
      # @return [Object]
      #
      # @api private
      def [](type)
        @coercers[type]
      end

    end # class Coercer

  end # class Attribute
end # module Virtus

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
virtus-1.0.0.rc2 lib/virtus/attribute/coercer.rb
virtus-1.0.0.rc1 lib/virtus/attribute/coercer.rb
virtus-1.0.0.beta8 lib/virtus/attribute/coercer.rb
virtus-1.0.0.beta7 lib/virtus/attribute/coercer.rb
virtus-1.0.0.beta6 lib/virtus/attribute/coercer.rb
virtus-1.0.0.beta5 lib/virtus/attribute/coercer.rb
virtus-1.0.0.beta4 lib/virtus/attribute/coercer.rb
virtus-1.0.0.beta3 lib/virtus/attribute/coercer.rb