synchronisable 1.1.1 → 1.1.2

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: 46e5d5f7aa22b3aba79572bb609e1944e14e5d6d
4
- data.tar.gz: 5858c6ad336fa0e59ef3c682d29beb8c0be80aed
3
+ metadata.gz: 00ba5533ba3d705a1e3c245124ceb78afd57da1d
4
+ data.tar.gz: abdedfecc1bf48eb0554cace0b9c323ccefe6fa6
5
5
  SHA512:
6
- metadata.gz: e925cf5b4ecf118c8f74dfa0837fde9c9942581826d9d907d88c23b135a6633dce8f6a6ce4a745d7eaa4a18e80ed7adab9f3cb188496e537bac8fbc7fb32f634
7
- data.tar.gz: 0d40c8a690b4e03fb8770f63e5d425be912aca8c7bacec78f0f3ec13b40ac6bcb38008a37e3cb7c93ceb4ef6b31b6f19f3dbebe858a7cc568f805e155c384ebe
6
+ metadata.gz: 2cd022bab4b92cd95a3b738c0b49f935a5fdcb9abac8c38b2716ae506baa371e893881e0524f3495db65c44f10b9c032ba52d93fc5812a549fa97dfed934c401
7
+ data.tar.gz: fd464896a1dce4ff99789b5e595186d5a6c043b0051759917a6a1479512484403fb31905bca4fae3f921bdfede5a102685b40d34d2f41540e21f12e34fbdcb70
@@ -19,6 +19,8 @@ module Synchronisable
19
19
  attr_reader :logger
20
20
 
21
21
  class << self
22
+ VALID_OPTIONS = %i(includes parent)
23
+
22
24
  # Creates a new instance of controller and initiates model synchronization.
23
25
  #
24
26
  # @overload call(model, data, options)
@@ -32,10 +34,36 @@ module Synchronisable
32
34
  #
33
35
  # @return [Synchronisable::Context] synchronization context
34
36
  def call(model, *args)
35
- options = args.extract_options!
37
+ data, options = *extract_args(args)
38
+ new(model, options).call(data)
39
+ end
40
+
41
+ private
42
+
43
+ # Model.sync({ tournament_id: '1014' }, {})
44
+ # ^^^^ <- data ^^ <- options
45
+
46
+ # Figures out what is meant to be data,
47
+ # and what is options.
48
+ def extract_args(args)
49
+ last_hash = args.extract_options!
50
+
36
51
  data = args.first
52
+ opts = {}
37
53
 
38
- new(model, options).call(data)
54
+ if last_hash.present?
55
+ if options?(last_hash)
56
+ opts = last_hash
57
+ elsif data.blank?
58
+ data = last_hash
59
+ end
60
+ end
61
+
62
+ [data, opts]
63
+ end
64
+
65
+ def options?(hash)
66
+ hash.any? { |k, _| VALID_OPTIONS.include? k }
39
67
  end
40
68
  end
41
69
 
@@ -58,6 +86,7 @@ module Synchronisable
58
86
  context.before = @model.imports_count
59
87
 
60
88
  hashes = @input.parse(data)
89
+
61
90
  hashes.each do |attrs|
62
91
  error_handler.handle(source) do
63
92
  source.prepare(data, attrs)
@@ -25,7 +25,7 @@ module Synchronisable
25
25
  when input.empty?
26
26
  @synchronizer.fetch
27
27
  when input.params?
28
- find_or_fetch_by_data(input.data)
28
+ find_or_fetch_by_params(input.data)
29
29
  when input.remote_id?
30
30
  @synchronizer.find(data)
31
31
  when input.local_id?
@@ -41,9 +41,9 @@ module Synchronisable
41
41
 
42
42
  private
43
43
 
44
- def find_or_fetch_by_data(data)
45
- sync_method = data.key?(:id) ? :find : :fetch
46
- @synchronizer.send(sync_method, data)
44
+ def find_or_fetch_by_params(params)
45
+ sync_method = params.key?(:id) ? :find : :fetch
46
+ @synchronizer.send(sync_method, params)
47
47
  end
48
48
 
49
49
  def find_by_array_of_ids(input)
@@ -41,6 +41,16 @@ module Synchronisable
41
41
  # Match.sync(:includes => {
42
42
  # :match_players => :player
43
43
  # })
44
+ #
45
+ # @example Here is all possible ways to call #sync:
46
+ # Model.sync
47
+ # Model.sync({ :a1 => 1, :a2 => 2 })
48
+ # Model.sync({ :includes => {...}, :parent => xxx })
49
+ # Model.sync(123)
50
+ # Model.sync('asdf')
51
+ # Model.sync([{ :a1 => 1, :a2 => 2 }, { :a1 => 3, :a2 => 4 }, ...])
52
+ # Model.sync([123, 345, 456])
53
+ # Model.sync(['123', '345', '456'])
44
54
  def sync(*args)
45
55
  Controller.call(self, *args)
46
56
  end
@@ -44,7 +44,6 @@ module Synchronisable
44
44
  @import_record = find_import
45
45
 
46
46
  remove_association_keys_from_local_attrs
47
- set_belongs_to_parent_foreign_key
48
47
  end
49
48
 
50
49
  def find_import
@@ -92,26 +91,6 @@ module Synchronisable
92
91
  end
93
92
  end
94
93
 
95
- def set_belongs_to_parent_foreign_key
96
- return unless @parent && parent_has_current_model_as_reflection?
97
- @local_attrs[parent_foreign_key_name] = @parent.local_record.id
98
- end
99
-
100
- def parent_foreign_key_name
101
- "#{parent_name}_id"
102
- end
103
-
104
- def parent_name
105
- @parent.model.table_name.singularize
106
- end
107
-
108
- def parent_has_current_model_as_reflection?
109
- @parent.model.reflections.values.any? do |reflection|
110
- reflection.plural_name == @model.table_name &&
111
- %i(has_one has_many).include?(reflection.macro)
112
- end
113
- end
114
-
115
94
  def filter_associations(macroses)
116
95
  @associations.select { |a| macroses.include? a.macro }
117
96
  end
@@ -2,7 +2,7 @@ module Synchronisable
2
2
  module VERSION
3
3
  MAJOR = 1
4
4
  MINOR = 1
5
- PATCH = 1
5
+ PATCH = 2
6
6
  SUFFIX = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH, SUFFIX].compact.join('.')
@@ -1,4 +1,6 @@
1
1
  class MatchSynchronizer < Synchronisable::Synchronizer
2
+ gateway MatchGateway
3
+
2
4
  has_one :team, key: 'home_team_id'
3
5
  has_one :team, key: 'away_team_id'
4
6
 
@@ -21,8 +23,6 @@ class MatchSynchronizer < Synchronisable::Synchronizer
21
23
 
22
24
  destroy_missed true
23
25
 
24
- gateway MatchGateway
25
-
26
26
  after_sync do |source|
27
27
  MatchPlayer::REF_TYPES.each do |ref_type|
28
28
  sync_match_players(source, ref_type)
@@ -121,9 +121,10 @@ describe Team do
121
121
  end
122
122
 
123
123
  context 'when remote id is not specified in attributes hash' do
124
- subject { Team.sync([remote_attrs.last]) }
125
-
126
- its(:errors) { should have(1).items }
124
+ it "return synchronization result that contains 1 error" do
125
+ result = Team.sync([remote_attrs.last])
126
+ expect(result.errors.count).to eq(1)
127
+ end
127
128
  end
128
129
 
129
130
  context 'when local record does not exist' do
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.1.1
4
+ version: 1.1.2
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-31 00:00:00.000000000 Z
11
+ date: 2014-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord