Module: Mixture::Extensions::Coercable
- Defined in:
- lib/mixture/extensions/coercable.rb
Overview
Extends the attribute definition to allow coercion.
Class Method Summary collapse
- .coerce_attribute(attribute, value) ⇒ Object
-
.included(base) ⇒ void
private
Called by Ruby when the module is included.
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.[:type] attr_type = Type.infer(attribute.[: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.
26 27 28 |
# File 'lib/mixture/extensions/coercable.rb', line 26 def self.included(base) base.attributes.callbacks[:update] << method(:coerce_attribute) end |