Class: PubSubModelSync::PayloadBuilder

Inherits:
Base
  • Object
show all
Defined in:
lib/pub_sub_model_sync/payload_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

config, debug?, log

Constructor Details

#initialize(model, action, settings = {}) ⇒ PayloadBuilder

Returns a new instance of PayloadBuilder.

Parameters:

  • model (ActiveRecord::Base, PubSubModelSync::PublisherConcern)
  • action (@see PublishConcern::ps_publish)
  • settings (@see PublishConcern::ps_publish) (defaults to: {})

    : { data:, mapping:, headers:, as_klass: }



10
11
12
13
14
15
16
17
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 10

def initialize(model, action, settings = {})
  @model = model
  @action = action
  @data = settings[:data] || {}
  @mapping = settings[:mapping] || []
  @headers = settings[:headers] || {}
  @as_klass = settings[:as_klass] || model.class.name
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 5

def action
  @action
end

#as_klassObject

Returns the value of attribute as_klass.



5
6
7
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 5

def as_klass
  @as_klass
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 5

def data
  @data
end

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 5

def headers
  @headers
end

#mappingObject

Returns the value of attribute mapping.



5
6
7
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 5

def mapping
  @mapping
end

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 5

def model
  @model
end

Class Method Details

.ordering_key_for(model) ⇒ Object



26
27
28
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 26

def self.ordering_key_for(model)
  [model.class.name, model.id || SecureRandom.uuid].join('/')
end

.parse_mapping_for(model, mapping) ⇒ Hash

Sample: parse_mapping_for(my_model, %w[id name:full_name])

==> { id: 10, full_name: 'model.name value' }

Parameters:

  • model (ActiveRecord::Base)
  • mapping (@see PublishConcern::ps_publish -)

    mapping)

Returns:

  • (Hash)

    Hash with the corresponding values for each attribute



35
36
37
38
39
40
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 35

def self.parse_mapping_for(model, mapping)
  mapping.map do |prop|
    source, target = prop.to_s.split(':')
    [target || source, model.send(source.to_sym)]
  end.to_h.symbolize_keys
end

Instance Method Details

#callPayload

Returns:



20
21
22
23
24
# File 'lib/pub_sub_model_sync/payload_builder.rb', line 20

def call
  values = compute_value(data)
  values = self.class.parse_mapping_for(model, mapping).merge(values)
  PubSubModelSync::Payload.new(values, settings_data, headers_data)
end