Class: Deimos::ActiveRecordConsume::SchemaModelConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/deimos/active_record_consume/schema_model_converter.rb

Overview

Convert a message with a schema to an ActiveRecord model

Instance Method Summary collapse

Constructor Details

#initialize(decoder, klass) ⇒ SchemaModelConverter

Create new converter

Parameters:

  • decoder (SchemaBackends::Base)

    Incoming message schema.

  • klass (ActiveRecord::Base)

    Model to map to.



10
11
12
13
# File 'lib/deimos/active_record_consume/schema_model_converter.rb', line 10

def initialize(decoder, klass)
  @decoder = decoder
  @klass = klass
end

Instance Method Details

#convert(payload) ⇒ Hash

Convert a message from a decoded hash to a set of ActiveRecord attributes. Attributes that don’t exist in the model will be ignored.

Parameters:

  • payload (Hash)

    Decoded message payload.

Returns:

  • (Hash)

    Model attributes.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/deimos/active_record_consume/schema_model_converter.rb', line 19

def convert(payload)
  attributes = {}
  @decoder.schema_fields.each do |field|
    column = @klass.columns.find { |c| c.name == field.name }
    next if column.nil?
    next if %w(updated_at created_at).include?(field.name)

    attributes[field.name] = _coerce_field(column, payload[field.name])
  end
  attributes
end