Class: Synced::Engine::Synchronizer
- Inherits:
-
Object
- Object
- Synced::Engine::Synchronizer
- Defined in:
- lib/synced/engine/synchronizer.rb
Overview
Synchronizer class which performs actual synchronization between local database and given array of remote objects
Defined Under Namespace
Classes: MissingAPIClient
Instance Attribute Summary collapse
-
#id_key ⇒ Object
readonly
Returns the value of attribute id_key.
Instance Method Summary collapse
-
#initialize(remote_objects, model_class, options = {}) ⇒ Synchronizer
constructor
Initializes a new Synchronizer.
- #perform ⇒ Object
Constructor Details
#initialize(remote_objects, model_class, options = {}) ⇒ Synchronizer
Initializes a new Synchronizer
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/synced/engine/synchronizer.rb', line 35 def initialize(remote_objects, model_class, = {}) @model_class = model_class @scope = [:scope] @id_key = [:id_key] @synced_all_at_key = [:synced_all_at_key] @data_key = [:data_key] @remove = [:remove] @only_updated = [:only_updated] @local_attributes = Array([:local_attributes]) @associations = Array([:associations]) @remote_objects = Array(remote_objects) if remote_objects @request_performed = false end |
Instance Attribute Details
#id_key ⇒ Object (readonly)
Returns the value of attribute id_key.
4 5 6 |
# File 'lib/synced/engine/synchronizer.rb', line 4 def id_key @id_key end |
Instance Method Details
#perform ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/synced/engine/synchronizer.rb', line 49 def perform relation_scope.transaction do remove_relation.send(remove_strategy) if @remove remote_objects.map do |remote| local_object = local_object_by_remote_id(remote.id) || relation_scope.new local_object.attributes = default_attributes_mapping(remote) local_object.attributes = local_attributes_mapping(remote) local_object.save! if local_object.changed? local_object.tap do |local_object| @associations.each do |association| klass = association.to_s.classify.constantize klass.synchronize(remote: remote[association], scope: local_object, remove: @remove) end end end.tap do |local_objects| if updated_since_enabled? && @request_performed relation_scope.update_all(@synced_all_at_key => Time.now) end end end end |