Class: TableSync::Publishing::Message::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/table_sync/publishing/message/base.rb

Direct Known Subclasses

Batch, Single

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
18
19
20
21
# File 'lib/table_sync/publishing/message/base.rb', line 12

def initialize(params = {})
  self.custom_version        = params[:custom_version]
  self.object_class          = params[:object_class]
  self.original_attributes   = params[:original_attributes]
  self.event                 = params[:event].to_sym

  @objects = find_or_init_objects

  raise TableSync::NoObjectsForSyncError if objects.empty? && TableSync.raise_on_empty_message
end

Instance Attribute Details

#custom_versionObject

Returns the value of attribute custom_version.



5
6
7
# File 'lib/table_sync/publishing/message/base.rb', line 5

def custom_version
  @custom_version
end

#eventObject

Returns the value of attribute event.



5
6
7
# File 'lib/table_sync/publishing/message/base.rb', line 5

def event
  @event
end

#object_classObject

Returns the value of attribute object_class.



5
6
7
# File 'lib/table_sync/publishing/message/base.rb', line 5

def object_class
  @object_class
end

#objectsObject (readonly)

Returns the value of attribute objects.



10
11
12
# File 'lib/table_sync/publishing/message/base.rb', line 10

def objects
  @objects
end

#original_attributesObject

Returns the value of attribute original_attributes.



5
6
7
# File 'lib/table_sync/publishing/message/base.rb', line 5

def original_attributes
  @original_attributes
end

Instance Method Details

#dataObject



47
48
49
50
51
52
53
# File 'lib/table_sync/publishing/message/base.rb', line 47

def data
  TableSync::Publishing::Data::Objects.new(
    objects:,
    event:,
    custom_version:,
  ).construct
end

#empty?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/table_sync/publishing/message/base.rb', line 31

def empty?
  objects.empty?
end

#find_or_init_objectsObject



35
36
37
38
39
# File 'lib/table_sync/publishing/message/base.rb', line 35

def find_or_init_objects
  TableSync::Publishing::Helpers::Objects.new(
    object_class:, original_attributes:, event:,
  ).construct_list
end

#message_paramsObject

MESSAGE PARAMS



43
44
45
# File 'lib/table_sync/publishing/message/base.rb', line 43

def message_params
  params.merge(data:)
end

#model_namingObject



73
74
75
# File 'lib/table_sync/publishing/message/base.rb', line 73

def model_naming
  TableSync.publishing_adapter.model_naming(objects.first.object_class)
end

#notify!Object

NOTIFY



63
64
65
66
67
68
69
70
71
# File 'lib/table_sync/publishing/message/base.rb', line 63

def notify!
  TableSync::Instrument.notify(
    table: model_naming.table,
    schema: model_naming.schema,
    event:,
    direction: :publish,
    count: objects.count,
  )
end

#paramsObject

:nocov:

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/table_sync/publishing/message/base.rb', line 56

def params
  raise NotImplementedError
end

#publishObject



23
24
25
26
27
28
29
# File 'lib/table_sync/publishing/message/base.rb', line 23

def publish
  return if original_attributes.blank?

  Rabbit.publish(**message_params)

  notify!
end