Class: Taro::Types::EnumType

Inherits:
BaseType
  • Object
show all
Extended by:
Shared::ItemType
Defined in:
lib/taro/types/enum_type.rb

Overview

Abstract class.

Instance Attribute Summary

Attributes included from Shared::ItemType

#item_type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared::ItemType

raise_mixed_types

Class Method Details

.inherited(subclass) ⇒ Object



39
40
41
42
# File 'lib/taro/types/enum_type.rb', line 39

def self.inherited(subclass)
  subclass.instance_variable_set(:@values, values.dup)
  super
end

.raise_if_empty_enumObject



35
36
37
# File 'lib/taro/types/enum_type.rb', line 35

def self.raise_if_empty_enum
  values.empty? && raise(Taro::RuntimeError, "Enum #{self} has no values")
end

.value(value) ⇒ Object



5
6
7
8
9
# File 'lib/taro/types/enum_type.rb', line 5

def self.value(value)
  self.item_type = Taro::Types::Coercion.call(type: value.class.name)
  @openapi_type ||= item_type.openapi_type
  values << value
end

.valuesObject



11
12
13
# File 'lib/taro/types/enum_type.rb', line 11

def self.values
  @values ||= []
end

Instance Method Details

#coerce_inputObject



15
16
17
18
19
20
21
22
23
# File 'lib/taro/types/enum_type.rb', line 15

def coerce_input
  self.class.raise_if_empty_enum
  value = self.class.item_type.new(object).coerce_input
  if self.class.values.include?(value)
    value
  else
    input_error("must be #{self.class.values.map(&:inspect).join(' or ')}")
  end
end

#coerce_responseObject



25
26
27
28
29
30
31
32
33
# File 'lib/taro/types/enum_type.rb', line 25

def coerce_response
  self.class.raise_if_empty_enum
  value = self.class.item_type.new(object).cached_coerce_response
  if self.class.values.include?(value)
    value
  else
    response_error("must be #{self.class.values.map(&:inspect).join(' or ')}")
  end
end