Method: Synced::Synchronizer#initialize

Defined in:
lib/synced/synchronizer.rb

#initialize(model_class, 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.

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

  • synced_all_at_key: (Symbol)

    attribute name under which remote object’s sync time is stored, default is :synced_all_at

  • 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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/synced/synchronizer.rb', line 40

def initialize(model_class, options = {})
  @model_class       = model_class
  @scope             = options[:scope]
  @id_key            = options[:id_key]
  @synced_all_at_key = options[:synced_all_at_key]
  @data_key          = options[:data_key]
  @remove            = options[:remove]
  @only_updated      = options[:only_updated]
  @include           = options[:include]
  @local_attributes  = 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(options[:associations])
  @remote_objects    = Array(options[:remote]) unless options[:remote].nil?
  @request_performed = false
end