synchronisable 0.0.4 → 0.0.5

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: 373108e0dab22db4d8c9ff814efc6df6c875a012
4
- data.tar.gz: 01d16982d0a8085ed46b69a117b4189ef8d39be1
3
+ metadata.gz: 670f61c0f12b85b365a456735ad8a8c9d67ba37b
4
+ data.tar.gz: 2ab99ead8df5badb9fef3ee54d3343d5748826f2
5
5
  SHA512:
6
- metadata.gz: ecefc4f286f2227d86193e6aa5c16861ca1d2feb668ba874e28dbc0815cae88ba080c41978a57352ac4935f0c08db7bbe7b54b5bc943de60ddaf45263dde2465
7
- data.tar.gz: 1a67ad8603495c3b6e41d0a6a95f512ad31f8b65213711f784a671504ffffc242bda2befed97e8ac2b0855f5af7e9dc3842ecd79f9aa1a71411b6c5904400c13
6
+ metadata.gz: f929b28fb870bc097bf8c3e28895ed16bebd2f2403cbb4570e6d1b8afcaf729f518116411ecc2f38a13864329510c8b1ab3f09d7ae491fbcc4e44fbe94a880e1
7
+ data.tar.gz: 03c0b36a7b6a56022faa4b0adff9b5d6003315a5148778541ff6ba90f65bb57f4826213eaf328ea2974894ccea3b3f2feae543a1680f297323b42c69e722d215
@@ -18,9 +18,9 @@ module Synchronisable
18
18
 
19
19
  result = case input
20
20
  when ->(i) { i.empty? }
21
- @synchronizer.fetch.()
21
+ @synchronizer.fetch
22
22
  when ->(i) { i.remote_id? }
23
- @synchronizer.find.(data)
23
+ @synchronizer.find(data)
24
24
  when ->(i) { i.local_id? }
25
25
  find_by_local_id(data)
26
26
  when ->(i) { i.array_of_ids? }
@@ -29,19 +29,19 @@ module Synchronisable
29
29
  result = data.dup
30
30
  end
31
31
 
32
- [result].flatten
32
+ [result].flatten.compact
33
33
  end
34
34
 
35
35
  private
36
36
 
37
37
  def find_by_array_of_ids(input)
38
38
  records = find_imports(input.element_class.name, input.data)
39
- records.map { |r| @synchronizer.find.(r.remote_id) }
39
+ records.map { |r| @synchronizer.find(r.remote_id) }
40
40
  end
41
41
 
42
42
  def find_by_local_id(id)
43
43
  import = @model.find_by(id: id).try(:import)
44
- import ? @synchronizer.find.(import.remote_id) : nil
44
+ import ? @synchronizer.find(import.remote_id) : nil
45
45
  end
46
46
 
47
47
  private
@@ -0,0 +1,24 @@
1
+ module Synchronisable
2
+ class Gateway
3
+ attr_reader :synchronizer
4
+
5
+ def initialize(synchronizer)
6
+ @synchronizer = synchronizer
7
+ end
8
+
9
+ def fetch
10
+ not_implemented :fetch
11
+ end
12
+
13
+ def find(id)
14
+ not_implemented :find
15
+ end
16
+
17
+ protected
18
+
19
+ def not_implemented(method)
20
+ raise NotImplementedError,
21
+ I18n.t('errors.gateway_method_missing', method: method)
22
+ end
23
+ end
24
+ end
@@ -38,8 +38,13 @@ module Synchronisable
38
38
  # of this particular model.
39
39
  attribute :logger, default: -> { Synchronisable.logging[:logger] }
40
40
 
41
+ # Gateway to be used to get the remote data
42
+ #
43
+ # @see Synchronizable::Gateway
44
+ attribute :gateway
45
+
41
46
  # Lambda that returns array of hashes with remote attributes.
42
- method :fetch, default: -> { [] }
47
+ method :fetcher, default: -> { [] }
43
48
 
44
49
  # Lambda that returns a hash with remote attributes by id.
45
50
  #
@@ -49,7 +54,7 @@ module Synchronisable
49
54
  # remote_source.find { |h| h[:foo_id] == id } }
50
55
  # end
51
56
  # end
52
- method :find
57
+ method :finder, default: -> (id) { nil }
53
58
 
54
59
  # Lambda, that will be called before synchronization
55
60
  # of each record and its assocations.
@@ -95,6 +100,20 @@ module Synchronisable
95
100
  method :after_association_sync
96
101
 
97
102
  class << self
103
+ def fetch
104
+ data = fetcher.()
105
+ data.present? ? data : gateway_instance.try(:fetch)
106
+ end
107
+
108
+ def find(id)
109
+ data = finder.(id)
110
+ data.present? ? data : gateway_instance.try(:find, id)
111
+ end
112
+
113
+ def gateway_instance
114
+ @gateway_instance ||= gateway.try(:new, self)
115
+ end
116
+
98
117
  # Extracts remote id from given attribute hash.
99
118
  #
100
119
  # @param attrs [Hash] remote attributes
@@ -2,7 +2,7 @@ module Synchronisable
2
2
  module VERSION
3
3
  MAJOR = 0
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('.')
@@ -36,7 +36,8 @@ module Synchronisable
36
36
  #
37
37
  # @param data [Array<Hash>] array of hashes with remote attriutes.
38
38
  # If not specified worker will try to get the data
39
- # using `fetch` lambda/proc defined in corresponding synchronizer
39
+ # using defined gateway class or `fetch` lambda/proc
40
+ # defined in corresponding synchronizer
40
41
  #
41
42
  # @return [Synchronisable::Context] synchronization context
42
43
  def run(data)
@@ -133,7 +134,7 @@ module Synchronisable
133
134
  log_info("synchronizing association with id: #{id}", :blue)
134
135
 
135
136
  @synchronizer.with_association_sync_callbacks(source, id, association) do
136
- attrs = association.model.synchronizer.find.(id)
137
+ attrs = association.model.synchronizer.find(id)
137
138
  Worker.run(association.model, [attrs], { :parent => source })
138
139
  end
139
140
  end
@@ -1,12 +1,12 @@
1
- class GatewayBase
1
+ require 'synchronisable/gateway'
2
+
3
+ class GatewayBase < Synchronisable::Gateway
2
4
  def id_key
3
- raise NotImplementedError,
4
- I18n.t('errors.gateway_method_missing', method: 'id_key')
5
+ not_implemented :id_key
5
6
  end
6
7
 
7
8
  def source
8
- raise NotImplementedError,
9
- I18n.t('errors.gateway_method_missing', method: 'source')
9
+ not_implemented :source
10
10
  end
11
11
 
12
12
  def fetch
@@ -1,6 +1,4 @@
1
1
  class BreakConventionTeamSynchronizer < Synchronisable::Synchronizer
2
- @gateway = TeamGateway.new
3
-
4
2
  has_many :players
5
3
 
6
4
  remote_id :maet_id
@@ -11,6 +9,5 @@ class BreakConventionTeamSynchronizer < Synchronisable::Synchronizer
11
9
  )
12
10
  except :ignored_1, :ignored_2
13
11
 
14
- find { |id| @gateway.find(id) }
15
- fetch { @gateway.fetch }
12
+ gateway TeamGateway
16
13
  end
@@ -1,6 +1,4 @@
1
1
  class MatchSynchronizer < Synchronisable::Synchronizer
2
- @gateway = MatchGateway.new
3
-
4
2
  has_one :team, key: 'home_team_id'
5
3
  has_one :team, key: 'away_team_id'
6
4
 
@@ -23,8 +21,7 @@ class MatchSynchronizer < Synchronisable::Synchronizer
23
21
 
24
22
  destroy_missed true
25
23
 
26
- find { |id| @gateway.find(id) }
27
- fetch { @gateway.fetch }
24
+ gateway MatchGateway
28
25
 
29
26
  after_sync do |source|
30
27
  MatchPlayer::REF_TYPES.each do |ref_type|
@@ -1,6 +1,4 @@
1
1
  class PlayerSynchronizer < Synchronisable::Synchronizer
2
- @gateway = PlayerGateway.new
3
-
4
2
  remote_id :player_id
5
3
 
6
4
  mappings(
@@ -14,6 +12,5 @@ class PlayerSynchronizer < Synchronisable::Synchronizer
14
12
  )
15
13
  only :team_id, :first_name, :last_name
16
14
 
17
- find { |id| @gateway.find(id) }
18
- fetch { @gateway.fetch }
15
+ gateway PlayerGateway
19
16
  end
@@ -1,6 +1,4 @@
1
1
  class StageSynchronizer < Synchronisable::Synchronizer
2
- @gateway = StageGateway.new
3
-
4
2
  has_many :matches
5
3
 
6
4
  remote_id :stage_id
@@ -15,6 +13,5 @@ class StageSynchronizer < Synchronisable::Synchronizer
15
13
 
16
14
  except :ignored_1, :ignored_2
17
15
 
18
- find { |id| @gateway.find(id) }
19
- fetch { @gateway.fetch }
16
+ gateway StageGateway
20
17
  end
@@ -1,6 +1,4 @@
1
1
  class TournamentSynchronizer < Synchronisable::Synchronizer
2
- @gateway = TournamentGateway.new
3
-
4
2
  has_many :stages
5
3
 
6
4
  remote_id :tour_id
@@ -15,6 +13,5 @@ class TournamentSynchronizer < Synchronisable::Synchronizer
15
13
 
16
14
  only :name, :beginning, :ending
17
15
 
18
- find { |id| @gateway.find(id) }
19
- fetch { @gateway.fetch }
16
+ gateway TournamentGateway
20
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synchronisable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasiliy Yorkin
@@ -319,6 +319,7 @@ files:
319
319
  - lib/synchronisable/dsl/macro/method.rb
320
320
  - lib/synchronisable/error_handler.rb
321
321
  - lib/synchronisable/exceptions.rb
322
+ - lib/synchronisable/gateway.rb
322
323
  - lib/synchronisable/input_descriptor.rb
323
324
  - lib/synchronisable/locale/en.yml
324
325
  - lib/synchronisable/locale/ru.yml