Class: Hanami::Model::Types::Schema::CoercibleType Private

Inherits:
Dry::Types::Definition
  • Object
show all
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

Since:

  • 0.7.0

Direct Known Subclasses

Sql::Types::Schema::AssociationType

Instance Method Summary collapse

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

Parameters:

  • value (Object)

    the value

Returns:

  • (Object)

    the coerced value of ‘object` type

Raises:

  • (TypeError)

    if value can’t be coerced

Since:

  • 0.7.0



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

Parameters:

  • value (Object)

    the value

Returns:

  • (Object)

    the coerced value of ‘object` type

Since:

  • 0.7.0



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

#objectObject

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.

Since:

  • 0.7.0



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`.

Parameters:

  • value (Object)

    the value

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.7.0



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