synchronisable 1.0.2 → 1.0.3

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: 3845145123b947ba744fd0312ae7302131b9aa02
4
- data.tar.gz: 79c2c958faf936420e9ef0166e055fce4fd8d952
3
+ metadata.gz: f01f7f0b2ba76796fe60eeb27ffc074a99ca1f0f
4
+ data.tar.gz: 71334451aaf9676cddd63b1489d368b15981a968
5
5
  SHA512:
6
- metadata.gz: 2c30615cf121cd45f57ac47f659357441ad185145545294165e61dceb4d67f1cfac0d54effd7fdfc9a2d93f709fdf8025b46e825e04a0d49788a798fd6c6e916
7
- data.tar.gz: 51349d8e1a49c8e240680ba11d55fb98eec97f7c9b595c821ef0bfa0e3726f7b0cfda102e94fdb637f7851430e9a99eb0a32057f0448a31154c3ca72e8e581eb
6
+ metadata.gz: 41805d28b8840c2bc4d4c4c4434cae52e3f52ae7078741497455a14169bf39aae45f1c6e8cf7f2446a4518a0dacf5692aafa0524e11b98e7c08a24660a8bbc14
7
+ data.tar.gz: 3c8fa21d0c36007278d3d09a0e52b321e93a1517a0464436ed843d97a89b07c308220a7ebe75a6ff2f93aa54ad58e8dafdf52007351f95c552ce8e14ba06fd47
@@ -58,7 +58,7 @@ module Synchronisable
58
58
 
59
59
  hashes = @input.parse(data)
60
60
  hashes.each do |attrs|
61
- source = Source.new(@model, @parent, attrs)
61
+ source = Source.new(@model, @parent, @includes, attrs)
62
62
 
63
63
  error_handler.handle(source) do
64
64
  source.prepare
@@ -84,6 +84,8 @@ module Synchronisable
84
84
  def initialize(model, options)
85
85
  @model, @synchronizer = model, model.synchronizer
86
86
  @logger = @synchronizer.logger
87
+
88
+ @includes = options[:includes]
87
89
  @parent = options[:parent]
88
90
 
89
91
  @input = InputParser.new(@model, @synchronizer)
@@ -45,6 +45,10 @@ module Synchronisable
45
45
  raise NotImplementedError
46
46
  end
47
47
 
48
+ def model_name
49
+ @model.to_s.demodulize.underscore.to_sym
50
+ end
51
+
48
52
  protected
49
53
 
50
54
  def set_defaults
@@ -4,6 +4,8 @@ module Synchronisable
4
4
  # Responsible for guessing the user input format.
5
5
  #
6
6
  # @api private
7
+ #
8
+ # @see Synchronisable::InputDescriptor
7
9
  class InputParser
8
10
  def initialize(model, synchronizer)
9
11
  @model = model
@@ -34,12 +34,12 @@ module Synchronisable
34
34
  # ])
35
35
  #
36
36
  # @example General usage
37
- # FooModel.sync(:include => {
38
- # :assocation_model => :nested_assocaiton_model
37
+ # FooModel.sync(:includes => {
38
+ # :assocation_model => :nested_association_model
39
39
  # })
40
40
  #
41
41
  # @example Football domain use case
42
- # Match.sync(:include => {
42
+ # Match.sync(:includes => {
43
43
  # :match_players => :player
44
44
  # })
45
45
  def sync(*args)
@@ -1,3 +1,5 @@
1
+ require 'pry-byebug'
2
+
1
3
  module Synchronisable
2
4
  # TODO: Massive refactoring needed
3
5
 
@@ -9,10 +11,13 @@ module Synchronisable
9
11
  attr_accessor :import_record
10
12
  attr_reader :parent_associations, :child_associations
11
13
  attr_reader :model, :remote_attrs, :remote_id,
12
- :local_attrs, :import_ids, :parent
14
+ :local_attrs, :import_ids, :parent,
15
+ :includes
13
16
 
14
- def initialize(model, parent, remote_attrs)
17
+ def initialize(model, parent, includes, remote_attrs)
15
18
  @model, @parent, @synchronizer = model, parent, model.synchronizer
19
+ @model_name = @model.to_s.demodulize.underscore.to_sym
20
+ @includes = includes
16
21
  @remote_attrs = remote_attrs.with_indifferent_access
17
22
  end
18
23
 
@@ -26,15 +31,10 @@ module Synchronisable
26
31
  def prepare
27
32
  @remote_id = @synchronizer.extract_remote_id(@remote_attrs)
28
33
  @local_attrs = @synchronizer.map_attributes(@remote_attrs)
29
-
30
34
  @associations = @synchronizer.associations_for(@local_attrs)
31
- @parent_associations = @associations.select do |association|
32
- PARENT_ASSOCIATION_KEYS.include? association.macro
33
- end
34
35
 
35
- @child_associations = @associations.select do |association|
36
- CHILD_ASSOCIATION_KEYS.include? association.macro
37
- end
36
+ @parent_associations = filter_associations(PARENT_ASSOCIATION_KEYS)
37
+ @child_associations = filter_associations(CHILD_ASSOCIATION_KEYS)
38
38
 
39
39
  @import_record = Import.find_by(
40
40
  :remote_id => @remote_id.to_s,
@@ -75,7 +75,6 @@ module Synchronisable
75
75
  def set_belongs_to_parent_foreign_key
76
76
  return unless @parent && parent_has_current_model_as_reflection?
77
77
  @local_attrs[parent_foreign_key_name] = @parent.local_record.id
78
-
79
78
  end
80
79
 
81
80
  def parent_foreign_key_name
@@ -92,5 +91,9 @@ module Synchronisable
92
91
  %i(has_one has_many).include?(reflection.macro)
93
92
  end
94
93
  end
94
+
95
+ def filter_associations(macroses)
96
+ @associations.select { |a| macroses.include? a.macro }
97
+ end
95
98
  end
96
99
  end
@@ -2,7 +2,7 @@ module Synchronisable
2
2
  module VERSION
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- PATCH = 2
5
+ PATCH = 3
6
6
  SUFFIX = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH, SUFFIX].compact.join('.')
@@ -1,4 +1,5 @@
1
1
  require 'synchronisable/worker/base'
2
+ require 'pry-byebug'
2
3
 
3
4
  module Synchronisable
4
5
  module Worker
@@ -17,9 +18,7 @@ module Synchronisable
17
18
  log_info("starting #{type} associations sync", :blue) if associations.present?
18
19
 
19
20
  associations.each do |association, ids|
20
- ids.each do |id|
21
- send(:"sync_#{type}_association", id, association)
22
- end
21
+ ids.each { |id| send(:"sync_#{type}_association", id, association) }
23
22
  end
24
23
 
25
24
  log_info("done #{type} associations sync", :blue) if associations.present?
@@ -44,13 +43,34 @@ module Synchronisable
44
43
  end
45
44
 
46
45
  def sync_child_association(id, association)
46
+ return unless can_sync_association?(association)
47
+
47
48
  log_info("synchronizing child association with id: #{id}", :blue)
48
49
 
49
50
  @synchronizer.with_association_sync_callbacks(@source, id, association) do
50
51
  attrs = association.model.synchronizer.find(id)
51
- Controller.call(association.model, [attrs], { :parent => @source })
52
+
53
+ Controller.call(association.model, [attrs],
54
+ child_association_options(association))
52
55
  end
53
56
  end
57
+
58
+ def can_sync_association?(association)
59
+ @includes.nil? || (
60
+ @includes.try(:include?, association.name) ||
61
+ @includes == association.name
62
+ )
63
+ end
64
+
65
+ def child_association_options(association)
66
+ default = @includes.nil? ? nil : {}
67
+ child_includes = @includes.try(:fetch, association.name) || default
68
+
69
+ {
70
+ :parent => @source,
71
+ :includes => child_includes
72
+ }
73
+ end
54
74
  end
55
75
  end
56
76
  end
@@ -13,6 +13,7 @@ module Synchronisable
13
13
 
14
14
  def initialize(synchronizer, source)
15
15
  @synchronizer, @source = synchronizer, source
16
+ @includes = source.includes
16
17
  end
17
18
 
18
19
  def logger
@@ -31,6 +31,11 @@ class MatchSynchronizer < Synchronisable::Synchronizer
31
31
 
32
32
  class << self
33
33
  def sync_match_players(source, ref_type)
34
+
35
+ # to support :includes option
36
+ return unless source.includes.nil? ||
37
+ source.includes.try(:include?, :match_players)
38
+
34
39
  match = source.local_record
35
40
  remote_player_ids = source.remote_attrs[ref_type.to_sym] || {}
36
41
 
@@ -15,27 +15,26 @@ describe Match do
15
15
  it { is_expected.to change { Synchronisable::Import.count }.by(11) }
16
16
  end
17
17
 
18
- # TODO: Left here until :include option is implemented
19
- #
20
- # context 'when associations specified with :include option' do
21
- # subject do
22
- # -> { sync_match }
23
- # end
18
+ context 'when associations specified with :includes option' do
19
+ subject do
20
+ -> { sync_match }
21
+ end
24
22
 
25
- # def sync_match
26
- # Match.sync(:include => {
27
- # :match => {
28
- # :team => :players
29
- # }
30
- # })
31
- # end
23
+ def sync_match
24
+ Match.sync(:includes => {
25
+ :team => :players,
26
+ :blha => {
27
+ :x => :z,
28
+ :y => :f
29
+ }
30
+ })
31
+ end
32
32
 
33
- # it { is_expected.to change { Match.count }.by(1) }
34
- # it { is_expected.to change { Team.count }.by(2) }
35
- # it { is_expected.to change { Player.count }.by(22) }
36
- # it { is_expected.to change { MatchPlayer.count }.by(22) }
33
+ it { is_expected.to change { Match.count }.by(1) }
34
+ it { is_expected.to change { Team.count }.by(2) }
35
+ it { is_expected.to change { Player.count }.by(4) }
37
36
 
38
- # it { is_expected.to change { Synchronisable::Import.count }.by(47) }
39
- # end
37
+ it { is_expected.to change { Synchronisable::Import.count }.by(7) }
38
+ end
40
39
  end
41
40
  end
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.2
4
+ version: 1.0.3
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-06-09 00:00:00.000000000 Z
11
+ date: 2014-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -449,7 +449,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
449
449
  version: '0'
450
450
  requirements: []
451
451
  rubyforge_project:
452
- rubygems_version: 2.2.1
452
+ rubygems_version: 2.2.0
453
453
  signing_key:
454
454
  specification_version: 4
455
455
  summary: Provides base fuctionality (models, DSL) for AR synchronization with external