Class: Avromatic::Model::Types::CustomType
- Inherits:
-
AbstractType
- Object
- AbstractType
- Avromatic::Model::Types::CustomType
- Defined in:
- lib/avromatic/model/types/custom_type.rb
Constant Summary collapse
- IDENTITY_PROC =
Proc.new { |value| value }
Instance Attribute Summary collapse
-
#custom_type_configuration ⇒ Object
readonly
Returns the value of attribute custom_type_configuration.
-
#default_type ⇒ Object
readonly
Returns the value of attribute default_type.
-
#value_classes ⇒ Object
readonly
Returns the value of attribute value_classes.
Instance Method Summary collapse
- #coerce(input) ⇒ Object
- #coerced?(value) ⇒ Boolean
- #coercible?(input) ⇒ Boolean
-
#initialize(custom_type_configuration:, default_type:) ⇒ CustomType
constructor
A new instance of CustomType.
- #input_classes ⇒ Object
- #name ⇒ Object
- #serialize(value) ⇒ Object
Constructor Details
#initialize(custom_type_configuration:, default_type:) ⇒ CustomType
Returns a new instance of CustomType.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/avromatic/model/types/custom_type.rb', line 13 def initialize(custom_type_configuration:, default_type:) @custom_type_configuration = custom_type_configuration @default_type = default_type @deserializer = custom_type_configuration.deserializer || IDENTITY_PROC @serializer = custom_type_configuration.serializer || IDENTITY_PROC @value_classes = if custom_type_configuration.value_class [custom_type_configuration.value_class].freeze else default_type.value_classes end end |
Instance Attribute Details
#custom_type_configuration ⇒ Object (readonly)
Returns the value of attribute custom_type_configuration.
11 12 13 |
# File 'lib/avromatic/model/types/custom_type.rb', line 11 def custom_type_configuration @custom_type_configuration end |
#default_type ⇒ Object (readonly)
Returns the value of attribute default_type.
11 12 13 |
# File 'lib/avromatic/model/types/custom_type.rb', line 11 def default_type @default_type end |
#value_classes ⇒ Object (readonly)
Returns the value of attribute value_classes.
11 12 13 |
# File 'lib/avromatic/model/types/custom_type.rb', line 11 def value_classes @value_classes end |
Instance Method Details
#coerce(input) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/avromatic/model/types/custom_type.rb', line 33 def coerce(input) if input.nil? input else @deserializer.call(input) end rescue StandardError => e # TODO: Don't swallow this raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}: #{e.}") end |
#coerced?(value) ⇒ Boolean
51 52 53 54 55 56 |
# File 'lib/avromatic/model/types/custom_type.rb', line 51 def coerced?(value) # TODO: Delegate this to optional configuration coerce(value) == value rescue ArgumentError false end |
#coercible?(input) ⇒ Boolean
44 45 46 47 48 49 |
# File 'lib/avromatic/model/types/custom_type.rb', line 44 def coercible?(input) # TODO: Delegate this to optional configuration input.nil? || !coerce(input).nil? rescue ArgumentError false end |
#input_classes ⇒ Object
25 26 27 |
# File 'lib/avromatic/model/types/custom_type.rb', line 25 def input_classes # We don't know the valid input classes for a custom type end |
#name ⇒ Object
29 30 31 |
# File 'lib/avromatic/model/types/custom_type.rb', line 29 def name custom_type_configuration.value_class ? custom_type_configuration.value_class.name.to_s.freeze : default_type.name end |
#serialize(value) ⇒ Object
58 59 60 |
# File 'lib/avromatic/model/types/custom_type.rb', line 58 def serialize(value, **) @serializer.call(value) end |