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



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mixture/extensions/coercable.rb', line 7

def self.coerce_attribute(attribute, value)
  return value unless attribute.options[:type]
  attr_type = Type.infer(attribute.options[:type])
  value_type = Type.infer(value)

  block = Coerce.coerce(value_type, attr_type)

  begin
    block.call(value)
  rescue StandardError => e
    raise CoercionError, "#{e.class}: #{e.message}", e.backtrace
  end
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)


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

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