synchronisable 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 96693280363388813f167c19dc3bdcb95e645bd7
4
- data.tar.gz: 7520f5bc6856f053eea801d1ee7b8346b484de1f
3
+ metadata.gz: ef3f05db3248f6b97f08228ce7efb74b4e515272
4
+ data.tar.gz: b973aef1b36c665a89cfcbb350863eea51034ba0
5
5
  SHA512:
6
- metadata.gz: 3cbdabf408ad30d339d7e966501a5534fb375f9f3801a08d7ee0ab00c1d8104588f722257f11bfee4d1a6e7ff6f86d6aef2d58e3988166d0e9050ed4dfa0277a
7
- data.tar.gz: c4399371c437ddf519fba064f85445f34d116b9a08fc6dc9c312dd3b0c9eda19b16412e84d0a0c3a241a2e23b7b5433b2a3216e09b0796606f79779062d8527c
6
+ metadata.gz: 4ef0a3f27605d49e471f170c889ac92a3895406765a770aa86ab907325c55077081cee6c0b12c809303dff7c2c3232ec0a90499c3e99ad8f966d58e09858c07c
7
+ data.tar.gz: 3c0ef536eb6433a0f421ea4d4f1f161aac67a2898e1314e13438857edaa4b9af12e394be6c222b8be465b75ba61d213bffe89bf04f4f146d9a67901532c42ba3
@@ -41,7 +41,7 @@ module Synchronisable
41
41
 
42
42
  # Initiates model synchronization.
43
43
  #
44
- # @param data [Array<Hash>, Array<String>, Array<Integer>, String, Integer]
44
+ # @param data [Hash, Array<Hash>, Array<String>, Array<Integer>, String, Integer]
45
45
  # synchronization data.
46
46
  # If not specified, it will try to get array of hashes to sync with
47
47
  # using defined gateway class or `fetch` lambda/proc
@@ -2,7 +2,7 @@ module Synchronisable
2
2
  class Gateway
3
3
  attr_reader :synchronizer
4
4
 
5
- def fetch
5
+ def fetch(params = {})
6
6
  not_implemented :fetch
7
7
  end
8
8
 
@@ -16,6 +16,10 @@ module Synchronisable
16
16
  @data.blank?
17
17
  end
18
18
 
19
+ def params?
20
+ @data.is_a?(Hash)
21
+ end
22
+
19
23
  def remote_id?
20
24
  @data.is_a?(String)
21
25
  end
@@ -22,17 +22,17 @@ module Synchronisable
22
22
  input = InputDescriptor.new(data)
23
23
 
24
24
  result = case
25
- when input.empty?
26
- @synchronizer.fetch
27
- when input.remote_id?
28
- @synchronizer.find(data)
29
- when input.local_id?
30
- find_by_local_id(data)
31
- when input.array_of_ids?
32
- find_by_array_of_ids(input)
33
- else
34
- result = data.dup
35
- end
25
+ when input.empty? || input.params?
26
+ @synchronizer.fetch(data || {})
27
+ when input.remote_id?
28
+ @synchronizer.find(data)
29
+ when input.local_id?
30
+ find_by_local_id(data)
31
+ when input.array_of_ids?
32
+ find_by_array_of_ids(input)
33
+ else
34
+ result = data.dup
35
+ end
36
36
 
37
37
  [result].flatten.compact
38
38
  end
@@ -11,16 +11,15 @@ module Synchronisable
11
11
  # in your model synchronizer, than it will be used if no data supplied.
12
12
  #
13
13
  # @overload sync(data, options)
14
- # @param data [Array<Hash>, Array<String>, Array<Integer>, String, Integer] synchronization data
14
+ # @param data [Hash, Array<Hash>, Array<String>, Array<Integer>, String, Integer] synchronization data
15
15
  # @param options [Hash] synchronization options
16
- # @option options [Hash] :include assocations to be synchronized.
16
+ # @option options [Hash] :includes assocations to be synchronized.
17
17
  # Use this option to override `has_one` & `has_many` assocations
18
18
  # defined in model synchronizer.
19
19
  # @overload sync(options)
20
20
  # @overload sync(data)
21
21
  # @overload sync
22
22
  #
23
- #
24
23
  # @see Synchronisable::Controller
25
24
  #
26
25
  # @example Supplying array of hashes with remote attributes
@@ -44,17 +44,27 @@ module Synchronisable
44
44
  attribute :gateway
45
45
 
46
46
  # Lambda that returns array of hashes with remote attributes.
47
- method :fetcher, default: -> { [] }
47
+ method :fetcher, default: -> (params = {}) { [] }
48
48
 
49
- # Lambda that returns a hash with remote attributes by id.
49
+ # Lambda that returns a hash with remote attributes by params.
50
50
  #
51
51
  # @example Common use case
52
52
  # class FooSynchronizer < Synchronisable::Synchronizer
53
- # find do |id|
53
+ # finder do |id|
54
54
  # remote_source.find { |h| h[:foo_id] == id } }
55
55
  # end
56
56
  # end
57
- method :finder, default: -> (id) { nil }
57
+ # @example Composite identifier
58
+ # class BarSynchronizer < Synchronisable::Synchronizer
59
+ # finder do |params|
60
+ # remote_source.find do |h|
61
+ # h[:x] == params[:x] &&
62
+ # h[:y] == params[:y] &&
63
+ # h[:z] == params[:z]
64
+ # end
65
+ # end
66
+ # end
67
+ method :finder, default: -> (params) { nil }
58
68
 
59
69
  # Lambda, that will be called before synchronization
60
70
  # of each record and its assocations.
@@ -100,14 +110,14 @@ module Synchronisable
100
110
  method :after_association_sync
101
111
 
102
112
  class << self
103
- def fetch
104
- data = fetcher.()
105
- data.present? ? data : gateway_instance.try(:fetch)
113
+ def fetch(params = {})
114
+ data = fetcher.(params)
115
+ data.present? ? data : gateway_instance.try(:fetch, params)
106
116
  end
107
117
 
108
- def find(id)
109
- data = finder.(id)
110
- data.present? ? data : gateway_instance.try(:find, id)
118
+ def find(params)
119
+ data = finder.(params)
120
+ data.present? ? data : gateway_instance.try(:find, params)
111
121
  end
112
122
 
113
123
  # Extracts remote id from given attribute hash.
@@ -2,7 +2,7 @@ module Synchronisable
2
2
  module VERSION
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- PATCH = 4
5
+ PATCH = 5
6
6
  SUFFIX = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH, SUFFIX].compact.join('.')
@@ -9,7 +9,7 @@ class GatewayBase < Synchronisable::Gateway
9
9
  not_implemented :source
10
10
  end
11
11
 
12
- def fetch
12
+ def fetch(params)
13
13
  source
14
14
  end
15
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synchronisable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasiliy Yorkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-24 00:00:00.000000000 Z
11
+ date: 2014-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord