Class: Deimos::SchemaClass::Record

Inherits:
Base
  • Object
show all
Defined in:
lib/deimos/schema_class/record.rb

Overview

Base Class of Record Classes generated from Avro.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, #[]=, #as_json, #initialize, #inspect

Constructor Details

This class inherits a constructor from Deimos::SchemaClass::Base

Instance Attribute Details

#_from_messageObject

Returns the value of attribute _from_message.



11
12
13
# File 'lib/deimos/schema_class/record.rb', line 11

def _from_message
  @_from_message
end

#tombstone_keyObject

Returns the value of attribute tombstone_key.



11
12
13
# File 'lib/deimos/schema_class/record.rb', line 11

def tombstone_key
  @tombstone_key
end

Class Method Details

.initialize_from_value(value, from_message: false) ⇒ SchemaClass::Record

Parameters:

  • from_message (Boolean) (defaults to: false)

    whether it’s being initialized from a real Avro message.

Returns:



88
89
90
91
92
93
94
95
96
97
# File 'lib/deimos/schema_class/record.rb', line 88

def self.initialize_from_value(value, from_message: false)
  return nil if value.nil?

  return value if value.is_a?(self)
  if from_message
    self.new_from_message(**value.symbolize_keys)
  else
    self.new(**value.symbolize_keys)
  end
end

.new_from_message(**kwargs) ⇒ SchemaClass::Record

Used internally within Deimos so that we don’t crash on unknown fields that come from a backwards compatible schema.

Parameters:

  • kwargs (Hash)

    the attributes to set on the new object.

Returns:



79
80
81
82
83
84
# File 'lib/deimos/schema_class/record.rb', line 79

def self.new_from_message(**kwargs)
  record = self.new
  attrs = kwargs.select { |k, v| record.respond_to?("#{k}=") }
  record = self.new(_from_message: true, **attrs)
  record
end

Instance Method Details

#[](key) ⇒ Object

Element access method as if this Object were a hash

Parameters:

  • key (String, Symbol)

Returns:

  • (Object)

    The value of the attribute if exists, nil otherwise



37
38
39
# File 'lib/deimos/schema_class/record.rb', line 37

def [](key)
  self.try(key.to_sym)
end

#full_schemaString

Returns the full schema name of the inheriting class.

Returns:

  • (String)


60
61
62
# File 'lib/deimos/schema_class/record.rb', line 60

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.

Parameters:

Returns:



26
27
28
29
30
31
32
# File 'lib/deimos/schema_class/record.rb', line 26

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

#namespaceString

Returns the namespace for the schema of the inheriting class.

Returns:

  • (String)

Raises:



54
55
56
# File 'lib/deimos/schema_class/record.rb', line 54

def namespace
  raise MissingImplementationError
end

#schemaString

Returns the schema name of the inheriting class.

Returns:

  • (String)

Raises:



48
49
50
# File 'lib/deimos/schema_class/record.rb', line 48

def schema
  raise MissingImplementationError
end

#schema_fieldsArray<String>

Returns an array of fields names in the schema.

Returns:

  • (Array<String>)

    an array of fields names in the schema.



71
72
73
# File 'lib/deimos/schema_class/record.rb', line 71

def schema_fields
  validator.schema_fields.map(&:name)
end

#to_hHash

Converts the object attributes to a hash which can be used for Kafka

Returns:

  • (Hash)

    the payload as a hash.



15
16
17
18
19
20
21
# File 'lib/deimos/schema_class/record.rb', line 15

def to_h
  if self.tombstone_key
    { payload_key: self.tombstone_key&.as_json }
  else
    self.as_json
  end
end

#validatorDeimos::SchemaBackends::Base

Returns the schema validator from the schema backend



66
67
68
# File 'lib/deimos/schema_class/record.rb', line 66

def validator
  Deimos.schema_backend(schema: schema, namespace: namespace)
end

#with_indifferent_accessSchemaClass::Record

Returns:



42
43
44
# File 'lib/deimos/schema_class/record.rb', line 42

def with_indifferent_access
  self
end