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
101 102 103 104 105 106 107 108 109 |
# File 'lib/hanami/model/types.rb', line 101 def call(value) return if value.nil? 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
132 133 134 135 136 137 138 139 |
# File 'lib/hanami/model/types.rb', line 132 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.
143 144 145 146 147 148 |
# File 'lib/hanami/model/types.rb', line 143 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 responds to `#to_hash`.
122 123 124 125 |
# File 'lib/hanami/model/types.rb', line 122 def valid?(value) value.is_a?(object) || value.respond_to?(:to_hash) end |