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.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, #[]=, #as_json, #initialize, #to_s

Constructor Details

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

Class Method Details

.initialize_from_value(value) ⇒ Object

:nodoc:



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

def self.initialize_from_value(value)
  return nil if value.nil?

  value.is_a?(self) ? value : self.new(**value.symbolize_keys)
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



31
32
33
# File 'lib/deimos/schema_class/record.rb', line 31

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

#full_schemaString

Returns the full schema name of the inheriting class.

Returns:

  • (String)


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

def full_schema
  "#{namespace}.#{schema}"
end

#merge(other_hash) ⇒ SchemaClasses::Base

Merge a hash or an identical schema object with this one and return a new object.

Parameters:

  • other_hash (Hash|SchemaClasses::Base)

Returns:

  • (SchemaClasses::Base)


20
21
22
23
24
25
26
# File 'lib/deimos/schema_class/record.rb', line 20

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:

  • (NotImplementedError)


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

def namespace
  raise NotImplementedError
end

#schemaString

Returns the schema name of the inheriting class.

Returns:

  • (String)

Raises:

  • (NotImplementedError)


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

def schema
  raise NotImplementedError
end

#schema_fieldsArray<String>

Returns an array of fields names in the schema.

Returns:

  • (Array<String>)

    an array of fields names in the schema.



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

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.



13
14
15
# File 'lib/deimos/schema_class/record.rb', line 13

def to_h
  self.as_json
end

#validatorDeimos::SchemaBackends::Base

Returns the schema validator from the schema backend



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

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

#with_indifferent_accessObject

:nodoc



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

def with_indifferent_access
  self
end