Class: Hanami::Model::Types::Schema::CoercibleType Private
- Inherits:
-
Dry::Types::Definition
- Object
- Dry::Types::Definition
- Hanami::Model::Types::Schema::CoercibleType
- Defined in:
- lib/hanami/model/types.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Coercer for objects within custom schema definition
Direct Known Subclasses
Instance Method Summary collapse
-
#call(value) ⇒ Object
private
Coerce given value into the wrapped object type.
-
#coerce(value) ⇒ Object
private
Coerce given value into an instance of ‘object` type.
- #object ⇒ Object private
-
#valid?(value) ⇒ TrueClass, FalseClass
private
Check if value can be coerced.
Instance Method Details
#call(value) ⇒ Object
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.
Coerce given value into the wrapped object type
49 50 51 52 53 54 55 |
# File 'lib/hanami/model/types.rb', line 49 def call(value) if valid?(value) coerce(value) else raise TypeError.new("#{value.inspect} must be coercible into #{object}") end end |
#coerce(value) ⇒ Object
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.
Coerce given value into an instance of ‘object` type
78 79 80 81 82 83 84 85 |
# File 'lib/hanami/model/types.rb', line 78 def coerce(value) case value when object value else object.new(value.to_hash) end end |
#object ⇒ Object
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.
89 90 91 92 93 94 |
# File 'lib/hanami/model/types.rb', line 89 def object result = primitive return result unless result.respond_to?(:primitive) result.primitive end |
#valid?(value) ⇒ TrueClass, FalseClass
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.
Check if value can be coerced
It is true if value is an instance of ‘object` type or if value respond to `#to_hash`.
68 69 70 71 |
# File 'lib/hanami/model/types.rb', line 68 def valid?(value) value.is_a?(object) || value.respond_to?(:to_hash) end |