Class: Synced::Strategies::Full

Inherits:
Object
  • Object
show all
Includes:
AttributesAsHash
Defined in:
lib/synced/strategies/full.rb

Overview

This strategy performs full synchronization. It takes all the objects from the API and

- creates missing in the local database
- removes local objects which are missing the API
- updates local objects which are changed in the API

This is the base synchronization strategy.

Direct Known Subclasses

Check, UpdatedSince

Defined Under Namespace

Classes: MissingAPIClient

Instance Method Summary collapse

Methods included from AttributesAsHash

#synced_attributes_as_hash

Constructor Details

#initialize(model_class, options = {}) ⇒ Full

Initializes new Full sync strategy

Parameters:

  • remote_objects (Array|NilClass)

    Array of objects to be synchronized with local database. Objects need to respond to at least :id message. If it’s nil, then synchronizer will fetch the remote objects on it’s own from the API.

  • model_class (Class)

    ActiveRecord model class from which local objects will be created.

  • options (Hash) (defaults to: {})
  • api (BookingSync::API::Client)
    • API client to be used for fetching

    remote objects

Options Hash (options):

  • scope: (Symbol)

    Within this object scope local objects will be synchronized. By default it’s model_class.

  • id_key: (Symbol)

    attribute name under which remote object’s ID is stored, default is :synced_id.

  • data_key: (Symbol)

    attribute name under which remote object’s data is stored.

  • local_attributes: (Array)

    Array of attributes in the remote object which will be mapped to local object attributes.

  • remove: (Boolean)

    If it’s true all local objects within current scope which are not present in the remote array will be destroyed. If only_updated is enabled, ids of objects to be deleted will be taken from the meta part. By default if cancel_at column is present, all missing local objects will be canceled with cancel_all, if it’s missing, all will be destroyed with destroy_all. You can also force method to remove local objects by passing it to remove: :mark_as_missing.

  • only_updated: (Boolean)

    If true requests to API will take advantage of updated_since param and fetch only created/changed/deleted remote objects

  • mapper: (Module)

    Module class which will be used for mapping remote objects attributes into local object attributes

  • globalized_attributes: (Array|Hash)

    A list of attributes which will be mapped with their translations.

  • auto_paginate: (Boolean)

    If true (default) will fetch and save all records at once. If false will fetch and save records in batches.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/synced/strategies/full.rb', line 50

def initialize(model_class, options = {})
  @model_class           = model_class
  @synced_endpoint       = options[:synced_endpoint]
  @scope                 = options[:scope]
  @id_key                = options[:id_key]
  @data_key              = options[:data_key]
  @only_updated          = options[:only_updated]
  @include               = options[:include]
  @local_attributes      = synced_attributes_as_hash(options[:local_attributes])
  @api                   = options[:api]
  @mapper                = options[:mapper].respond_to?(:call) ?
                             options[:mapper].call : options[:mapper]
  @fields                = options[:fields]
  @remove                = options[:remove]
  @associations          = Array.wrap(options[:associations])
  @association_sync      = options[:association_sync]
  @perform_request       = options[:remote].nil? && !@association_sync
  @remote_objects        = Array.wrap(options[:remote]) unless @perform_request
  @globalized_attributes = synced_attributes_as_hash(options[:globalized_attributes])
  @query_params         = options[:query_params]
  @auto_paginate         = options[:auto_paginate]
  @transaction_per_page  = options[:transaction_per_page]
  @handle_processed_objects_proc = options[:handle_processed_objects_proc]
  @remote_objects_ids = []
end

Instance Method Details

#performObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/synced/strategies/full.rb', line 76

def perform
  instrument("perform.synced", model: @model_class) do
    processed_objects = instrument("sync_perform.synced", model: @model_class) do
      process_remote_objects(remote_objects_persistor)
    end
    relation_scope.transaction do
      instrument("remove_perform.synced", model: @model_class) do
        remove_relation.send(remove_strategy) if @remove
      end
    end
    processed_objects
  end
end

#reset_syncedObject



90
91
92
# File 'lib/synced/strategies/full.rb', line 90

def reset_synced
  RuntimeError.new("Full strategy does not support reset_synced functionality")
end