Class: Taro::Types::EnumType

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

Overview

Abstract class.

Constant Summary

Constants included from Shared::OpenAPIFormat

Shared::OpenAPIFormat::OPENAPI_INTEGER_FORMATS, Shared::OpenAPIFormat::OPENAPI_NUMBER_FORMATS, Shared::OpenAPIFormat::OPENAPI_STRING_FORMATS

Constants included from Shared::OpenAPIType

Shared::OpenAPIType::OPENAPI_TYPES

Instance Attribute Summary

Attributes included from Shared::ItemType

#item_type

Attributes inherited from BaseType

#object

Attributes included from Shared::AdditionalProperties

#additional_properties

Attributes included from Shared::Deprecation

#deprecated

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared::ItemType

raise_mixed_types

Methods included from Shared::AdditionalProperties

#additional_properties?, #inherited

Methods included from Shared::DerivedTypes

#define_derived_type, #derived_types, map

Methods included from Shared::Description

#desc, #desc=

Methods included from Shared::Equivalence

#equal_properties?, #equivalent?

Methods included from Shared::Name

#define_name

Methods included from Shared::OpenAPIFormat

#inherited, #openapi_format, #openapi_format=

Methods included from Shared::OpenAPIName

#default_openapi_name, #openapi_name, #openapi_name=

Methods included from Shared::OpenAPIType

#inherited, #openapi_type, #openapi_type=

Methods included from Shared::Rendering

#last_render, #last_render=, #render, #used_in_response

Methods included from Shared::TypeClass

included

Methods included from Shared::Errors

#coerce_error_message, #input_error, #response_error

Methods included from Shared::Caching

#cached_coerce_response, included

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