Class: Synced::Synchronizer

Inherits:
Object
  • Object
show all
Defined in:
lib/synced/synchronizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, strategy:, **options) ⇒ Synchronizer

Initializes a new Synchronizer

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)
  • 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.

  • strategy: (Symbol)

    Strategy to be used for synchronization process, possible values are :full, :updated_since, :check.



48
49
50
51
52
53
# File 'lib/synced/synchronizer.rb', line 48

def initialize(model_class, strategy:, **options)
  @model_class       = model_class
  @only_updated      = options[:only_updated]
  @remote            = options[:remote]
  @strategy          = strategy_class(strategy).new(model_class, options)
end

Instance Attribute Details

#strategyObject (readonly)

Returns the value of attribute strategy.



11
12
13
# File 'lib/synced/synchronizer.rb', line 11

def strategy
  @strategy
end

Instance Method Details

#performObject



55
56
57
# File 'lib/synced/synchronizer.rb', line 55

def perform
  @strategy.perform
end

#reset_syncedObject



59
60
61
# File 'lib/synced/synchronizer.rb', line 59

def reset_synced
  @strategy.reset_synced
end