Class: Dionysus::Consumer::SynchronizableModel
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Dionysus::Consumer::SynchronizableModel
- Defined in:
- lib/dionysus/consumer/synchronizable_model.rb
Instance Method Summary collapse
- #assign_attributes_from_dionysus(attributes) ⇒ Object
-
#initialize(config, model) ⇒ SynchronizableModel
constructor
A new instance of SynchronizableModel.
- #model ⇒ Object
- #persist_with_dionysus?(event_updated_at) ⇒ Boolean
- #remove_with_dionysus(deseralized_record) ⇒ Object
- #restorable?(deseralized_record) ⇒ Boolean
- #restore_with_dionysus ⇒ Object
- #synced_at ⇒ Object
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 |
#model ⇒ Object
12 13 14 |
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 12 def model __getobj__ end |
#persist_with_dionysus?(event_updated_at) ⇒ 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?("#{}=")) || !deseralized_record.has_synced_canceled_at? destroy end end |
#restorable?(deseralized_record) ⇒ Boolean
54 55 56 |
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 54 def restorable?(deseralized_record) respond_to?("#{}=") && !deseralized_record.has_synced_canceled_at? end |
#restore_with_dionysus ⇒ Object
50 51 52 |
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 50 def restore_with_dionysus model.public_send("#{}=", nil) end |
#synced_at ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/dionysus/consumer/synchronizable_model.rb', line 16 def synced_at if respond_to?() public_send() else public_send() end end |