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



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

Parameters:

  • value (Object)

    the value

Returns:

  • (Object)

    the coerced value of ‘object` type

Since:

  • 0.7.0



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

#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



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

Parameters:

  • value (Object)

    the value

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.7.0



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