Class: Synced::Synchronizer
- Inherits:
-
Object
- Object
- Synced::Synchronizer
- Defined in:
- lib/synced/synchronizer.rb
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(model_class, options = {}) ⇒ Synchronizer
constructor
Initializes a new Synchronizer.
- #perform ⇒ Object
Constructor Details
#initialize(model_class, options = {}) ⇒ Synchronizer
Initializes a new Synchronizer
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/synced/synchronizer.rb', line 42 def initialize(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] @include = [:include] @local_attributes = attributes_as_hash([:local_attributes]) @api = [:api] @mapper = [:mapper].respond_to?(:call) ? [:mapper].call : [:mapper] @fields = [:fields] @remove = [:remove] @associations = Array([:associations]) @remote_objects = Array([:remote]) unless [:remote].nil? @request_performed = false @globalized_attributes = attributes_as_hash([:globalized_attributes]) end |
Instance Attribute Details
#id_key ⇒ Object (readonly)
Returns the value of attribute id_key.
5 6 7 |
# File 'lib/synced/synchronizer.rb', line 5 def id_key @id_key end |
Instance Method Details
#perform ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/synced/synchronizer.rb', line 63 def perform instrument("perform.synced", model: @model_class) do relation_scope.transaction do instrument("remove_perform.synced", model: @model_class) do remove_relation.send(remove_strategy) if @remove end instrument("sync_perform.synced", model: @model_class) do remote_objects.map do |remote| remote.extend(@mapper) if @mapper 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) if @globalized_attributes.present? local_object.attributes = globalized_attributes_mapping(remote, local_object.translations.translated_locales) end 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 end.tap do |local_objects| if updated_since_enabled? && @request_performed instrument("update_synced_all_at_perform.synced", model: @model_class) do relation_scope.update_all(@synced_all_at_key => Time.now) end end end end end end |