class Avromatic::Model::CustomTypeConfiguration

Instances of this class contains the configuration for custom handling of a named type (record, enum, fixed).

Attributes

from_avro[RW]
to_avro[RW]
value_class[RW]

Public Class Methods

new(value_class) click to toggle source
# File lib/avromatic/model/custom_type_configuration.rb, line 12
def initialize(value_class)
  @value_class = value_class
end

Public Instance Methods

deserializer() click to toggle source

A deserializer method is used when assigning to the model. It is used both when deserializing a model instance from Avro and when directly instantiating an instance. The deserializer method must accept a single argument and return the value to store in the model for the attribute.

# File lib/avromatic/model/custom_type_configuration.rb, line 20
def deserializer
  proc = from_avro_proc
  wrap_proc(proc) if proc
end
serializer() click to toggle source

A serializer method is used when preparing attributes to be serialized using Avro. The serializer method must accept a single argument of the model value for the attribute and return a value in a form that Avro can serialize for the attribute.

# File lib/avromatic/model/custom_type_configuration.rb, line 29
def serializer
  proc = to_avro_proc
  wrap_proc(proc) if proc
end

Private Instance Methods

from_avro_proc() click to toggle source
# File lib/avromatic/model/custom_type_configuration.rb, line 40
def from_avro_proc
  from_avro || value_class_method(:from_avro)
end
to_avro_proc() click to toggle source
# File lib/avromatic/model/custom_type_configuration.rb, line 36
def to_avro_proc
  to_avro || value_class_method(:to_avro)
end
value_class_method(method_name) click to toggle source
# File lib/avromatic/model/custom_type_configuration.rb, line 44
def value_class_method(method_name)
  value_class && value_class.respond_to?(method_name) &&
    value_class.method(method_name).to_proc
end
wrap_proc(proc) click to toggle source

Wrap the supplied Proc to handle nil.

# File lib/avromatic/model/custom_type_configuration.rb, line 50
def wrap_proc(proc)
  ->(value) { proc.call(value) if value }
end