Class: Definition::Types::Type

Inherits:
Base
  • Object
show all
Defined in:
lib/definition/types/type.rb

Instance Attribute Summary

Attributes inherited from Base

#context, #name

Instance Method Summary collapse

Methods inherited from Base

#error_renderer, #explain

Constructor Details

#initialize(name, klass, &coerce) ⇒ Type

Returns a new instance of Type.



8
9
10
11
12
13
14
# File 'lib/definition/types/type.rb', line 8

def initialize(name, klass, &coerce)
  raise "#{klass.inspect} is not a class" unless klass.is_a?(Class)

  self.klass = klass
  self.coerce = coerce
  super(name, context: { class: klass })
end

Instance Method Details

#conform(value) ⇒ Object



16
17
18
19
20
# File 'lib/definition/types/type.rb', line 16

def conform(value)
  value = coerce.call(value) if value && coerce && !valid?(value)

  try_conform(value)
end