Class: TableSync::Receiving::Handler

Inherits:
Rabbit::EventHandler
  • Object
show all
Extended by:
DSL
Defined in:
lib/table_sync/receiving/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL

inherited, receive

Constructor Details

#initialize(message) ⇒ Handler

Returns a new instance of Handler.



9
10
11
12
13
14
15
# File 'lib/table_sync/receiving/handler.rb', line 9

def initialize(message)
  super

  self.event   = message.data[:event]
  self.model   = message.data[:model]
  self.version = message.data[:version]
end

Instance Attribute Details

#eventObject

Returns the value of attribute event.



7
8
9
# File 'lib/table_sync/receiving/handler.rb', line 7

def event
  @event
end

#modelObject

Returns the value of attribute model.



7
8
9
# File 'lib/table_sync/receiving/handler.rb', line 7

def model
  @model
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/table_sync/receiving/handler.rb', line 6

def version
  @version
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/table_sync/receiving/handler.rb', line 17

def call
  configs.each do |config|
    next unless config.allow_event?(event)

    data = processed_data(config)

    next if data.empty?

    version_key = config.version_key(data:)
    data.each { |row| row[version_key] = version }

    target_keys = config.target_keys(data:)

    validate_data(data, target_keys:)

    data.sort_by! { |row| row.values_at(*target_keys).map { |value| sort_key(value) } }

    params = { data:, target_keys:, version_key: }

    if event == :update
      params[:default_values] = config.default_values(data:)
    end

    config.wrap_receiving(event:, **params) do
      perform(config, params)
    end
  end
end