Class: Avromatic::Model::Types::EnumType
- Inherits:
-
AbstractType
- Object
- AbstractType
- Avromatic::Model::Types::EnumType
- Defined in:
- lib/avromatic/model/types/enum_type.rb
Constant Summary collapse
- VALUE_CLASSES =
[::String].freeze
- INPUT_CLASSES =
[::String, ::Symbol].freeze
Instance Attribute Summary collapse
-
#allowed_values ⇒ Object
readonly
Returns the value of attribute allowed_values.
Instance Method Summary collapse
- #coerce(input) ⇒ Object
- #coerced?(value) ⇒ Boolean
- #coercible?(input) ⇒ Boolean
-
#initialize(allowed_values) ⇒ EnumType
constructor
A new instance of EnumType.
- #input_classes ⇒ Object
- #name ⇒ Object
- #referenced_model_classes ⇒ Object
- #serialize(value, _strict) ⇒ Object
- #value_classes ⇒ Object
Constructor Details
#initialize(allowed_values) ⇒ EnumType
Returns a new instance of EnumType.
14 15 16 17 |
# File 'lib/avromatic/model/types/enum_type.rb', line 14 def initialize(allowed_values) super() @allowed_values = allowed_values.to_set end |
Instance Attribute Details
#allowed_values ⇒ Object (readonly)
Returns the value of attribute allowed_values.
12 13 14 |
# File 'lib/avromatic/model/types/enum_type.rb', line 12 def allowed_values @allowed_values end |
Instance Method Details
#coerce(input) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/avromatic/model/types/enum_type.rb', line 31 def coerce(input) if input.nil? input elsif coercible?(input) input.to_s else raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}") end end |
#coerced?(value) ⇒ Boolean
41 42 43 |
# File 'lib/avromatic/model/types/enum_type.rb', line 41 def coerced?(value) value.nil? || value.is_a?(::String) && allowed_values.include?(value) end |
#coercible?(input) ⇒ Boolean
45 46 47 48 49 |
# File 'lib/avromatic/model/types/enum_type.rb', line 45 def coercible?(input) input.nil? || (input.is_a?(::String) && allowed_values.include?(input)) || (input.is_a?(::Symbol) && allowed_values.include?(input.to_s)) end |
#input_classes ⇒ Object
27 28 29 |
# File 'lib/avromatic/model/types/enum_type.rb', line 27 def input_classes INPUT_CLASSES end |
#name ⇒ Object
19 20 21 |
# File 'lib/avromatic/model/types/enum_type.rb', line 19 def name "enum#{allowed_values.to_a}" end |
#referenced_model_classes ⇒ Object
55 56 57 |
# File 'lib/avromatic/model/types/enum_type.rb', line 55 def referenced_model_classes EMPTY_ARRAY end |
#serialize(value, _strict) ⇒ Object
51 52 53 |
# File 'lib/avromatic/model/types/enum_type.rb', line 51 def serialize(value, _strict) value end |
#value_classes ⇒ Object
23 24 25 |
# File 'lib/avromatic/model/types/enum_type.rb', line 23 def value_classes VALUE_CLASSES end |