Attribute objects handle coercion and provide interface to hook into an attribute set instance that‘s included into a class or object
@example
# non-strict mode
attr = Virtus::Attribute.build(Integer)
attr.coerce('1')
# => 1
# strict mode
attr = Virtus::Attribute.build(Integer, :strict => true)
attr.coerce('not really coercible')
# => Virtus::CoercionError: Failed to coerce "fsafa" into Integer
| coercer | [R] | @api private |
| default_value | [R] | @api private |
| options | [R] | @api private |
| primitive | [R] | @api private |
| type | [R] |
Return type of this attribute
@return [Axiom::Types::Type] @api public |
Coerce the input into the expected type
@example
attr = Virtus::Attribute.build(String) attr.coerce(:one) # => 'one'
@param [Object] input
@api public
Return if the attribute is coercible
@example
attr = Virtus::Attribute.build(String, :coerce => true) attr.coercible? # => true attr = Virtus::Attribute.build(String, :coerce => false) attr.coercible? # => false
@return [Boolean]
@api public
Return if the attribute was already finalized
@example
attr = Virtus::Attribute.build(String, :finalize => true) attr.finalized? # => true attr = Virtus::Attribute.build(String, :finalize => false) attr.finalized? # => false
@return [Boolean]
@api public
Return if the attribute has lazy default value evaluation
@example
attr = Virtus::Attribute.build(String, :lazy => true) attr.lazy? # => true attr = Virtus::Attribute.build(String, :lazy => false) attr.lazy? # => false
@return [Boolean]
@api public
Return if the attribute is in the nullify blank coercion mode
@example
attr = Virtus::Attribute.build(String, :nullify_blank => true) attr.nullify_blank? # => true attr = Virtus::Attribute.build(String, :nullify_blank => false) attr.nullify_blank? # => false
@return [Boolean]
@api public
Return if the attribute is accepts nil values as valid coercion output
@example
attr = Virtus::Attribute.build(String, :required => true) attr.required? # => true attr = Virtus::Attribute.build(String, :required => false) attr.required? # => false
@return [Boolean]
@api public
Return if the attribute is in the strict coercion mode
@example
attr = Virtus::Attribute.build(String, :strict => true) attr.strict? # => true attr = Virtus::Attribute.build(String, :strict => false) attr.strict? # => false
@return [Boolean]
@api public