Sha256: ea6d10ac8ea5a649d9feeaec9315d3207778eeec5984debc502cd26954874416

Contents?: true

Size: 968 Bytes

Versions: 2

Compression:

Stored size: 968 Bytes

Contents

# encoding: utf-8
# frozen_string_literal: true

module Mixture
  module Extensions
    # Extends the attribute definition to allow coercion.
    module Coercable
      # Performs the coercion for the attribute and the value.
      # It is used in a `:update` callback.
      #
      # @see Coerce.perform
      # @param attribute [Attribute] The attribute
      # @param value [Object] The new value.
      # @return [Object] The new new value.
      # @raise [CoercionError] If an error occurs while a coercion is
      #   running.
      def self.coerce_attribute(attribute, value)
        return value unless attribute.options[:type]
        Coerce.perform(attribute.options[:type], value)
      end

      # Called by Ruby when the module is included.
      #
      # @param base [Object]
      # @return [void]
      # @api private
      def self.included(base)
        base.attributes.callbacks[:update] << method(:coerce_attribute)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mixture-0.7.1 lib/mixture/extensions/coercable.rb
mixture-0.7.0 lib/mixture/extensions/coercable.rb