Class: Omakase::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/omakase/type.rb

Overview

A return type that is a Ruby class: the model builds the object in code and hands the object itself back, so nothing goes through JSON.

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Type

Returns a new instance of Type.



7
8
9
# File 'lib/omakase/type.rb', line 7

def initialize(klass)
  @klass = klass
end

Instance Method Details

#definitionObject Also known as: json

Raises:



19
20
21
# File 'lib/omakase/type.rb', line 19

def definition
  raise Error, "returns: #{@klass} needs the :code_act strategy — the model has to build the object in code"
end

#describeObject



11
# File 'lib/omakase/type.rb', line 11

def describe = "a #{@klass}"

#take(value) ⇒ Object

Raises:



13
14
15
16
17
# File 'lib/omakase/type.rb', line 13

def take(value)
  return value if value.is_a?(@klass)

  raise ContractError, "expected #{describe}, got #{value.class}"
end