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

#advances_dionysus_state?(event_updated_at) ⇒ Boolean

A purge stamps the cancel column without bumping synced_updated_at, so under a stale payload a tie on the child's timestamp is how a cancelled child gets revived. Demand demonstrably newer. An unstamped child is refused outright: a payload the guard has already rejected proves nothing about a child it carries no timestamp for, and applying one would resurrect a child that a fresher payload had already deleted. Refusing it is exactly what the old blanket next did.

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 33

def advances_dionysus_state?(event_updated_at)
  return false if event_updated_at.nil?

  persist_with_dionysus?(event_updated_at) &&
    !(synced_at && event_updated_at == synced_at)
end

#assign_attributes_from_dionysus(attributes) ⇒ Object



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

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

  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



52
53
54
55
56
57
58
59
60
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 52

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)


66
67
68
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 66

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

#restore_with_dionysusObject



62
63
64
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 62

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