Module: Mixture::Extensions::Coercable

Defined in:
lib/mixture/extensions/coercable.rb

Overview

Extends the attribute definition to allow coercion.

Class Method Summary collapse

Class Method Details

.coerce_attribute(attribute, value) ⇒ Object

Performs the coercion for the attribute and the value. It is used in a :update callback.

Parameters:

  • attribute (Attribute)

    The attribute

  • value (Object)

    The new value.

Returns:

  • (Object)

    The new new value.

Raises:

  • (CoercionError)

    If an error occurs while a coercion is running.

See Also:



17
18
19
20
# File 'lib/mixture/extensions/coercable.rb', line 17

def self.coerce_attribute(attribute, value)
  return value unless attribute.options[:type]
  Coerce.perform(attribute.options[:type], value)
end

.included(base) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Called by Ruby when the module is included.

Parameters:

  • base (Object)


27
28
29
# File 'lib/mixture/extensions/coercable.rb', line 27

def self.included(base)
  base.attributes.callbacks[:update] << method(:coerce_attribute)
end