Class: Dionysus::Consumer::SynchronizableModel

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/dionysus/consumer/synchronizable_model.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, model) ⇒ SynchronizableModel

Returns a new instance of SynchronizableModel.



7
8
9
10
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 7

def initialize(config, model)
  @config = config
  super(model)
end

Instance Method Details

#assign_attributes_from_dionysus(attributes) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 28

def assign_attributes_from_dionysus(attributes)
  public_send("#{synced_data_attribute}=", attributes)
  reverse_mapping = config.attributes_mapping_for_model(model.model_name).to_a.map(&:reverse).to_h

  assignable_attributes = extract_assignable_attributes(attributes)
    .map { |key, value| apply_mapping(reverse_mapping, key, value) }
    .select { |attribute, _| respond_to?("#{attribute}=") }
    .to_h

  assign_attributes(assignable_attributes)
end

#modelObject



12
13
14
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 12

def model
  __getobj__
end

#persist_with_dionysus?(event_updated_at) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 24

def persist_with_dionysus?(event_updated_at)
  (synced_at && event_updated_at && event_updated_at >= synced_at) || synced_at.nil? || event_updated_at.nil?
end

#remove_with_dionysus(deseralized_record) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 40

def remove_with_dionysus(deseralized_record)
  if soft_deleteable_but_cannot_be_soft_deleted_via_attribute_assignment?(deseralized_record)
    public_send(soft_delete_strategy)
  elsif (deseralized_record.has_synced_canceled_at? && !respond_to?("#{soft_deleted_at_timestamp_attribute}=")) ||
      !deseralized_record.has_synced_canceled_at?

    destroy
  end
end

#restorable?(deseralized_record) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 54

def restorable?(deseralized_record)
  respond_to?("#{soft_deleted_at_timestamp_attribute}=") && !deseralized_record.has_synced_canceled_at?
end

#restore_with_dionysusObject



50
51
52
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 50

def restore_with_dionysus
  model.public_send("#{soft_deleted_at_timestamp_attribute}=", nil)
end

#synced_atObject



16
17
18
19
20
21
22
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 16

def synced_at
  if respond_to?(synced_updated_at_timestamp_attribute)
    public_send(synced_updated_at_timestamp_attribute)
  else
    public_send(synced_created_at_timestamp_attribute)
  end
end