Module: ActiveAvro::Model::ClassMethods

Defined in:
lib/active_avro/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#avro_schemaObject (readonly)

The Avro::Schema that defines de/serialization of model instances.


36
37
38
# File 'lib/active_avro/model.rb', line 36

def avro_schema
  @avro_schema
end

Instance Method Details

#schema(schema) ⇒ Object

Declare the Avro schema for records represented by this model class. Object instances are (de)serialized from/to records with this schema, and attributes will be defined for the field names and types specified therein.


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_avro/model.rb', line 48

def schema(schema)
  @avro_schema ||= case schema
                   when Avro::Schema
                     schema
                   when String, Pathname, File
                     Avro::Schema.parse(open(schema).read)
                   # when Symbol
                     # look for #{schema}.avsc in a configured path?
                   else raise ArgumentError,
                     "Unsupported type for Avro schema <#{schema.class}>"
                   end

  # Declare a (Virtus) attribute for each field.
  #
  # TODO: this is a quick and dirty start--we'll want to handle default
  # values and other features, like complex types, as best we can.
  @avro_schema.fields.each do |field|
    type = AVRO_TYPE_MAP[field.type.type_sym]
    attribute field.name, type, default: field.default
  end
end