synced 1.5.2 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b57fd9809319aece800d98be7cb7d595af42183
4
- data.tar.gz: ceb98bf9015b4877d597f4d89a73c63627ad8353
3
+ metadata.gz: a79202a122c1ac89d94317b7a04ddcc4cbd2f826
4
+ data.tar.gz: 9a24cb9fa5f708ead2375829a1a615588d1dc9e9
5
5
  SHA512:
6
- metadata.gz: 498ac45444e90dac7dba56d8c79bb644c61a89fd5da85c1225612a86dc0d5fae59cb6570d744d2c5c7b4d3176dd0936c36d304f7aadfa97d672bd4b2a4b38125
7
- data.tar.gz: 43493baddef335a7c0602949071ba5602cc0e9519e746fc7f737f86a9fc327a8c6d5dda260bc2ec68249d060e3293dc67a5aac0100cde3527dd4300b49fd0988
6
+ metadata.gz: 9be1f7c897d14a222d99dd2ba3392fa637af8b56432b82078aa26ef59c5914ec412e2b3fb29e3582d03c2881f8a6383d7f0dccdc1d5369b9b044c3aac54f2728
7
+ data.tar.gz: 970a8062422cd80b92d69d7f2d82f82c14ccbabefad4aa8ce9659aace797ae4be06cae49a58458649d1499ff6f08b2ea982d127a732f822afc8dbe043df2d113
data/README.md CHANGED
@@ -462,6 +462,22 @@ keys are delegated attributes' names and values are keys on synced data Hash. Th
462
462
  version of `delegate :name, to: :synced_data` which works with Hash reserved attributes names, like
463
463
  `:zip`, `:map`.
464
464
 
465
+ ## Persisting fetched objects
466
+
467
+ By default all fetched objects are persisted inside one big transaction. You can customize this behaviour by providing `transaction_per_page` option either in model configuration:
468
+
469
+ ```ruby
470
+ class Photo < ActiveRecord::Base
471
+ synced transaction_per_page: true
472
+ end
473
+ ```
474
+
475
+ Or as a param in `synchronize` method:
476
+
477
+ ```
478
+ Photo.synchronize(transaction_per_page: true)
479
+ ```
480
+
465
481
  ## Synced configuration options
466
482
 
467
483
  Option name | Default value | Description | synced | synchronize |
@@ -477,6 +493,7 @@ Option name | Default value | Description
477
493
  `:remote` | `nil` | [Remote objects to be synchronized with local ones](#synchronization-of-given-remote-objects) | NO | YES |
478
494
  `:delegate_attributes`| `[]` | [Define delegators to synced data Hash attributes](#delegate-attributes) | YES | NO |
479
495
  `:auto_paginate` | `true` | [Whether data should be fetched in batches or as one response](#fetching-methods) | YES | YES |
496
+ `:transaction_per_page` | `false` | [Whether transaction should be per page of fetched objects or for all the pages - note that setting this value to `true` will mean always fetching data in batches, even when specifying `auto_paginate` as true](#persisting-fetched-objects) | YES | YES |
480
497
  `:handle_processed_objects_proc` | `nil` | [Custom proc taking persisted remote objects, called after persisting batch of data](#persisted-objects) | YES | NO |
481
498
 
482
499
  ## Documentation
@@ -37,17 +37,20 @@ module Synced
37
37
  # which will be passed to api client to perform search
38
38
  # @option options [Boolean] auto_paginate: If true (default) will fetch and save all
39
39
  # records at once. If false will fetch and save records in batches.
40
+ # @option options transaction_per_page [Boolean]: If false (default) all fetched records
41
+ # will be persisted within single transaction. If true the transaction will be per page
42
+ # of fetched records
40
43
  # @option options [Proc] handle_processed_objects_proc: Proc taking one argument (persisted remote objects).
41
44
  # Called after persisting remote objects (once in case of auto_paginate, after each batch
42
45
  # when paginating with block).
43
46
  def synced(strategy: :updated_since, **options)
44
47
  options.assert_valid_keys(:associations, :data_key, :fields,
45
48
  :globalized_attributes, :id_key, :include, :initial_sync_since,
46
- :local_attributes, :mapper, :only_updated, :remove, :auto_paginate,
49
+ :local_attributes, :mapper, :only_updated, :remove, :auto_paginate, :transaction_per_page,
47
50
  :delegate_attributes, :query_params, :timestamp_strategy, :handle_processed_objects_proc)
48
51
  class_attribute :synced_id_key, :synced_data_key,
49
52
  :synced_local_attributes, :synced_associations, :synced_only_updated,
50
- :synced_mapper, :synced_remove, :synced_include, :synced_fields, :synced_auto_paginate,
53
+ :synced_mapper, :synced_remove, :synced_include, :synced_fields, :synced_auto_paginate, :synced_transaction_per_page,
51
54
  :synced_globalized_attributes, :synced_initial_sync_since, :synced_delegate_attributes,
52
55
  :synced_query_params, :synced_timestamp_strategy, :synced_strategy, :synced_handle_processed_objects_proc
53
56
  self.synced_strategy = strategy
@@ -69,6 +72,7 @@ module Synced
69
72
  self.synced_query_params = options.fetch(:query_params, {})
70
73
  self.synced_timestamp_strategy = options.fetch(:timestamp_strategy, nil)
71
74
  self.synced_auto_paginate = options.fetch(:auto_paginate, true)
75
+ self.synced_transaction_per_page = options.fetch(:transaction_per_page, false)
72
76
  self.synced_handle_processed_objects_proc = options.fetch(:handle_processed_objects_proc, nil)
73
77
  include Synced::DelegateAttributes
74
78
  include Synced::HasSyncedData
@@ -93,6 +97,9 @@ module Synced
93
97
  # and then overwritten in the synchronize method.
94
98
  # @param auto_paginate [Boolean] - If true (default) will fetch and save all
95
99
  # records at once. If false will fetch and save records in batches.
100
+ # @param transaction_per_page [Boolean] - If false (default) all fetched records
101
+ # will be persisted within single transaction. If true the transaction will be per page
102
+ # of fetched records
96
103
  # @param api [BookingSync::API::Client] - API client to be used for fetching
97
104
  # remote objects
98
105
  # @example Synchronizing amenities
@@ -106,12 +113,13 @@ module Synced
106
113
  # website.rentals.synchronize(remote: remote_rentals)
107
114
  #
108
115
  def synchronize(scope: scope_from_relation, strategy: synced_strategy, **options)
109
- options.assert_valid_keys(:api, :fields, :include, :remote, :remove, :query_params, :association_sync, :auto_paginate)
116
+ options.assert_valid_keys(:api, :fields, :include, :remote, :remove, :query_params, :association_sync, :auto_paginate, :transaction_per_page)
110
117
  options[:remove] = synced_remove unless options.has_key?(:remove)
111
118
  options[:include] = Array.wrap(synced_include) unless options.has_key?(:include)
112
119
  options[:fields] = Array.wrap(synced_fields) unless options.has_key?(:fields)
113
120
  options[:query_params] = synced_query_params unless options.has_key?(:query_params)
114
121
  options[:auto_paginate] = synced_auto_paginate unless options.has_key?(:auto_paginate)
122
+ options[:transaction_per_page] = synced_transaction_per_page unless options.has_key?(:transaction_per_page)
115
123
  options.merge!({
116
124
  scope: scope,
117
125
  strategy: strategy,
@@ -44,6 +44,9 @@ module Synced
44
44
  # which will be mapped with their translations.
45
45
  # @option options [Boolean] auto_paginate: If true (default) will fetch and save all
46
46
  # records at once. If false will fetch and save records in batches.
47
+ # @options options [Boolean] transaction_per_page: if false (default) all fetched records
48
+ # will be persisted within single transaction. If true the transaction will be per page
49
+ # of fetched records
47
50
  def initialize(model_class, options = {})
48
51
  @model_class = model_class
49
52
  @scope = options[:scope]
@@ -64,21 +67,22 @@ module Synced
64
67
  @globalized_attributes = synced_attributes_as_hash(options[:globalized_attributes])
65
68
  @query_params = options[:query_params]
66
69
  @auto_paginate = options[:auto_paginate]
70
+ @transaction_per_page = options[:transaction_per_page]
67
71
  @handle_processed_objects_proc = options[:handle_processed_objects_proc]
68
72
  @remote_objects_ids = []
69
73
  end
70
74
 
71
75
  def perform
72
76
  instrument("perform.synced", model: @model_class) do
77
+ processed_objects = instrument("sync_perform.synced", model: @model_class) do
78
+ process_remote_objects(remote_objects_persistor)
79
+ end
73
80
  relation_scope.transaction do
74
- processed_objects = instrument("sync_perform.synced", model: @model_class) do
75
- process_remote_objects(remote_objects_persistor)
76
- end
77
81
  instrument("remove_perform.synced", model: @model_class) do
78
82
  remove_relation.send(remove_strategy) if @remove
79
83
  end
80
- processed_objects
81
84
  end
85
+ processed_objects
82
86
  end
83
87
  end
84
88
 
@@ -193,11 +197,21 @@ module Synced
193
197
 
194
198
  def fetch_and_save_remote_objects(processor)
195
199
  instrument("fetch_remote_objects.synced", model: @model_class) do
196
- if @auto_paginate
197
- processor.call(api.paginate(resource_name, api_request_options))
198
- else
200
+ if @transaction_per_page
199
201
  api.paginate(resource_name, api_request_options) do |batch|
200
- processor.call(batch)
202
+ relation_scope.transaction do
203
+ processor.call(batch)
204
+ end
205
+ end
206
+ elsif @auto_paginate
207
+ relation_scope.transaction do
208
+ processor.call(api.paginate(resource_name, api_request_options))
209
+ end
210
+ else
211
+ relation_scope.transaction do
212
+ api.paginate(resource_name, api_request_options) do |batch|
213
+ processor.call(batch)
214
+ end
201
215
  end
202
216
  end
203
217
  end
@@ -1,3 +1,3 @@
1
1
  module Synced
2
- VERSION = "1.5.2"
2
+ VERSION = "1.6.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synced
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastien Grosjean
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-03-10 00:00:00.000000000 Z
12
+ date: 2017-06-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -238,8 +238,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
238
  version: '0'
239
239
  requirements: []
240
240
  rubyforge_project:
241
- rubygems_version: 2.6.10
241
+ rubygems_version: 2.5.1
242
242
  signing_key:
243
243
  specification_version: 4
244
244
  summary: Keep your BookingSync Application synced with BookingSync.
245
245
  test_files: []
246
+ has_rdoc: