Class: AvroGen::SchemaClass::Record
- Defined in:
- lib/avro_gen/schema_class/record.rb
Overview
Base Class of Record Classes generated from Avro.
Instance Attribute Summary collapse
-
#_from_message ⇒ Object
Returns the value of attribute _from_message.
-
#tombstone_key ⇒ Object
Returns the value of attribute tombstone_key.
Class Method Summary collapse
- .initialize_from_value(value, from_message: false) ⇒ SchemaClass::Record
-
.new_from_message(**kwargs) ⇒ SchemaClass::Record
Used internally so that we don't crash on unknown fields that come from a backwards compatible schema.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Element access method as if this Object were a hash.
-
#full_schema ⇒ String
Returns the full schema name of the inheriting class.
-
#merge(other_hash) ⇒ SchemaClass::Base
Merge a hash or an identical schema object with this one and return a new object.
-
#namespace ⇒ String
Returns the namespace for the schema of the inheriting class.
-
#schema ⇒ String
Returns the schema name of the inheriting class.
-
#schema_fields ⇒ Array<String>
An array of fields names in the schema.
-
#to_h ⇒ Hash
Converts the object attributes to a hash which can be used for Kafka.
-
#validator ⇒ AvroGen::SchemaValidator
Returns a validator that can load the schema and its fields.
- #with_indifferent_access ⇒ SchemaClass::Record
Methods inherited from Base
#==, #[]=, #as_json, #initialize, #inspect
Constructor Details
This class inherits a constructor from AvroGen::SchemaClass::Base
Instance Attribute Details
#_from_message ⇒ Object
Returns the value of attribute _from_message.
13 14 15 |
# File 'lib/avro_gen/schema_class/record.rb', line 13 def @_from_message end |
#tombstone_key ⇒ Object
Returns the value of attribute tombstone_key.
13 14 15 |
# File 'lib/avro_gen/schema_class/record.rb', line 13 def tombstone_key @tombstone_key end |
Class Method Details
.initialize_from_value(value, from_message: false) ⇒ SchemaClass::Record
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/avro_gen/schema_class/record.rb', line 95 def self.initialize_from_value(value, from_message: false) return nil if value.nil? return value if value.is_a?(self) if self.(**value&.symbolize_keys || {}) else self.new(**value&.symbolize_keys || {}) end end |
.new_from_message(**kwargs) ⇒ SchemaClass::Record
Used internally so that we don't crash on unknown fields that come from a backwards compatible schema.
87 88 89 90 91 |
# File 'lib/avro_gen/schema_class/record.rb', line 87 def self.(**kwargs) record = self.new attrs = kwargs.select { |k, _v| record.respond_to?("#{k}=") } self.new(_from_message: true, **attrs) end |
Instance Method Details
#[](key) ⇒ Object
Element access method as if this Object were a hash
39 40 41 |
# File 'lib/avro_gen/schema_class/record.rb', line 39 def [](key) self.try(key.to_sym) end |
#full_schema ⇒ String
Returns the full schema name of the inheriting class.
62 63 64 |
# File 'lib/avro_gen/schema_class/record.rb', line 62 def full_schema "#{namespace}.#{schema}" end |
#merge(other_hash) ⇒ SchemaClass::Base
Merge a hash or an identical schema object with this one and return a new object.
28 29 30 31 32 33 34 |
# File 'lib/avro_gen/schema_class/record.rb', line 28 def merge(other_hash) obj = self.class.new(**self.to_h.symbolize_keys) other_hash.to_h.each do |k, v| obj.send("#{k}=", v) end obj end |
#namespace ⇒ String
Returns the namespace for the schema of the inheriting class.
56 57 58 |
# File 'lib/avro_gen/schema_class/record.rb', line 56 def namespace raise MissingImplementationError end |
#schema ⇒ String
Returns the schema name of the inheriting class.
50 51 52 |
# File 'lib/avro_gen/schema_class/record.rb', line 50 def schema raise MissingImplementationError end |
#schema_fields ⇒ Array<String>
Returns an array of fields names in the schema.
79 80 81 |
# File 'lib/avro_gen/schema_class/record.rb', line 79 def schema_fields validator.schema_fields.map(&:name) end |
#to_h ⇒ Hash
Converts the object attributes to a hash which can be used for Kafka
17 18 19 20 21 22 23 |
# File 'lib/avro_gen/schema_class/record.rb', line 17 def to_h if self.tombstone_key { payload_key: self.tombstone_key&.as_json } else self.as_json end end |
#validator ⇒ AvroGen::SchemaValidator
Returns a validator that can load the schema and its fields. Reuses a shared, path-scoped schema store so repeated lookups don't re-parse the .avsc files.
70 71 72 73 74 75 76 |
# File 'lib/avro_gen/schema_class/record.rb', line 70 def validator @validator ||= AvroGen::SchemaValidator.new( schema: schema, namespace: namespace, store: AvroGen::SchemaValidator.store_for(AvroGen.config.schema_path) ) end |
#with_indifferent_access ⇒ SchemaClass::Record
44 45 46 |
# File 'lib/avro_gen/schema_class/record.rb', line 44 def with_indifferent_access self end |