synchronisable 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.coveralls.yml +1 -0
- data/.rspec +0 -1
- data/.travis.yml +5 -0
- data/Gemfile +8 -1
- data/README.md +11 -4
- data/Rakefile +22 -3
- data/TODO.md +1 -1
- data/lib/generators/{synchronizable → synchronisable}/USAGE +0 -0
- data/lib/generators/{synchronizable → synchronisable}/install_generator.rb +2 -2
- data/lib/generators/{synchronizable → synchronisable}/templates/create_imports_migration.rb +4 -4
- data/lib/generators/{synchronizable → synchronisable}/templates/initializer.rb +2 -2
- data/lib/{synchronizable.rb → synchronisable.rb} +13 -13
- data/lib/{synchronizable → synchronisable}/context.rb +1 -1
- data/lib/{synchronizable → synchronisable}/dsl/associations.rb +4 -4
- data/lib/{synchronizable → synchronisable}/dsl/associations/association.rb +3 -3
- data/lib/{synchronizable → synchronisable}/dsl/associations/has_many.rb +2 -2
- data/lib/{synchronizable → synchronisable}/dsl/associations/has_one.rb +2 -2
- data/lib/{synchronizable → synchronisable}/dsl/macro.rb +8 -8
- data/lib/{synchronizable → synchronisable}/dsl/macro/attribute.rb +4 -4
- data/lib/{synchronizable → synchronisable}/dsl/macro/expression.rb +4 -4
- data/lib/{synchronizable → synchronisable}/dsl/macro/method.rb +4 -4
- data/lib/{synchronizable → synchronisable}/error_handler.rb +4 -4
- data/lib/{synchronizable → synchronisable}/exceptions.rb +1 -1
- data/lib/{synchronizable → synchronisable}/locale/en.yml +0 -0
- data/lib/{synchronizable → synchronisable}/locale/ru.yml +0 -0
- data/lib/{synchronizable → synchronisable}/model.rb +15 -15
- data/lib/{synchronizable → synchronisable}/model/methods.rb +6 -6
- data/lib/{synchronizable → synchronisable}/models/import.rb +6 -6
- data/lib/{synchronizable → synchronisable}/source.rb +3 -3
- data/lib/{synchronizable → synchronisable}/synchronizer.rb +18 -18
- data/lib/{synchronizable → synchronisable}/synchronizers/synchronizer_default.rb +2 -2
- data/lib/{synchronizable → synchronisable}/version.rb +2 -2
- data/lib/{synchronizable → synchronisable}/worker.rb +14 -14
- data/spec/dummy/app/models/match.rb +1 -1
- data/spec/dummy/app/models/match_player.rb +1 -1
- data/spec/dummy/app/models/player.rb +1 -1
- data/spec/dummy/app/models/stadium.rb +1 -1
- data/spec/dummy/app/models/stage.rb +1 -1
- data/spec/dummy/app/models/team.rb +1 -1
- data/spec/dummy/app/models/tournament.rb +1 -1
- data/spec/dummy/app/synchronizers/break_convention_team_synchronizer.rb +1 -1
- data/spec/dummy/app/synchronizers/match_synchronizer.rb +6 -6
- data/spec/dummy/app/synchronizers/player_synchronizer.rb +1 -1
- data/spec/dummy/app/synchronizers/stage_synchronizer.rb +1 -1
- data/spec/dummy/app/synchronizers/tournament_synchronizer.rb +1 -1
- data/spec/dummy/config/application.rb +1 -1
- data/spec/dummy/config/initializers/synchronizable.rb +1 -1
- data/spec/dummy/db/migrate/20140422135244_create_imports.rb +4 -4
- data/spec/dummy/db/schema.rb +3 -3
- data/spec/factories/import.rb +1 -1
- data/spec/models/match_spec.rb +10 -10
- data/spec/models/team_spec.rb +14 -18
- data/spec/spec_helper.rb +15 -5
- data/spec/{synchronizable → synchronisable}/dsl/macro_spec.rb +1 -1
- data/spec/synchronisable/models/import_spec.rb +10 -0
- data/spec/{synchronizable → synchronisable}/support/has_macro.rb +1 -1
- data/spec/{synchronizable → synchronisable}/support/has_macro_subclass.rb +0 -0
- data/spec/{synchronizable/synchronizable_spec.rb → synchronisable/synchronisable_spec.rb} +9 -9
- data/{synchronizable.gemspec → synchronisable.gemspec} +6 -4
- metadata +94 -79
- data/spec/synchronizable/models/import_spec.rb +0 -10
@@ -1,9 +1,9 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
1
|
+
require 'synchronisable/worker'
|
2
|
+
require 'synchronisable/models/import'
|
3
3
|
|
4
|
-
module
|
4
|
+
module Synchronisable
|
5
5
|
module Model
|
6
|
-
# Methods that will be attached to
|
6
|
+
# Methods that will be attached to synchronisable model class.
|
7
7
|
module Methods
|
8
8
|
# Creates a new worker, that initiates synchronization
|
9
9
|
# for this particular model.
|
@@ -21,7 +21,7 @@ module Synchronizable
|
|
21
21
|
#
|
22
22
|
# @param data [Array<Hash>] array of hashes with remote attributes.
|
23
23
|
#
|
24
|
-
# @see
|
24
|
+
# @see Synchronisable::Worker
|
25
25
|
#
|
26
26
|
# @example Supplying array of hashes with remote attributes
|
27
27
|
# FooModel.sync([
|
@@ -48,7 +48,7 @@ module Synchronizable
|
|
48
48
|
|
49
49
|
# Count of import records for this model.
|
50
50
|
def imports_count
|
51
|
-
Import.where(
|
51
|
+
Import.where(synchronisable_type: self).count
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
@@ -1,11 +1,11 @@
|
|
1
|
-
module
|
1
|
+
module Synchronisable
|
2
2
|
class Import < ActiveRecord::Base
|
3
|
-
belongs_to :
|
3
|
+
belongs_to :synchronisable, polymorphic: true
|
4
4
|
|
5
5
|
serialize :attrs, Hash
|
6
6
|
|
7
|
-
scope :
|
8
|
-
scope :
|
7
|
+
scope :with_synchronisable_type, ->(type) { where(synchronisable_type: type) }
|
8
|
+
scope :with_synchronisable_ids, ->(ids) { where(synchronisable_id: ids) }
|
9
9
|
|
10
10
|
scope :with_remote_id, ->(id) { where(remote_id: id.to_s) }
|
11
11
|
scope :with_remote_ids, ->(ids) { where(remote_id: ids.map(&:to_s)) }
|
@@ -14,9 +14,9 @@ module Synchronizable
|
|
14
14
|
with_remote_id(id).first
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
17
|
+
def destroy_with_synchronisable
|
18
18
|
ActiveRecord::Base.transaction do
|
19
|
-
|
19
|
+
synchronisable.try :destroy
|
20
20
|
destroy
|
21
21
|
end
|
22
22
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module Synchronisable
|
2
2
|
# Synchronization source.
|
3
3
|
class Source
|
4
4
|
attr_accessor :import_record
|
@@ -26,7 +26,7 @@ module Synchronizable
|
|
26
26
|
|
27
27
|
@import_record = Import.find_by(
|
28
28
|
:remote_id => @remote_id,
|
29
|
-
:
|
29
|
+
:synchronisable_type => @model
|
30
30
|
)
|
31
31
|
|
32
32
|
set_parent_attribute
|
@@ -37,7 +37,7 @@ module Synchronizable
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def local_record
|
40
|
-
@import_record.try(:
|
40
|
+
@import_record.try(:synchronisable)
|
41
41
|
end
|
42
42
|
|
43
43
|
def dump_message
|
@@ -1,16 +1,16 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require '
|
1
|
+
require 'synchronisable/dsl/macro'
|
2
|
+
require 'synchronisable/dsl/associations'
|
3
|
+
require 'synchronisable/exceptions'
|
4
4
|
|
5
|
-
module
|
5
|
+
module Synchronisable
|
6
6
|
# @abstract Subclass to create your model specific synchronizer class to
|
7
7
|
# setup synchronization attributes and behavior.
|
8
8
|
#
|
9
|
-
# @see
|
10
|
-
# @see
|
9
|
+
# @see Synchronisable::DSL::Macro
|
10
|
+
# @see Synchronisable::SynchronizerDefault
|
11
11
|
class Synchronizer
|
12
|
-
include
|
13
|
-
include
|
12
|
+
include Synchronisable::DSL::Macro
|
13
|
+
include Synchronisable::DSL::Associations
|
14
14
|
|
15
15
|
SYMBOL_ARRAY_CONVERTER = ->(source) { (source || []).map(&:to_s) }
|
16
16
|
|
@@ -47,7 +47,7 @@ module Synchronizable
|
|
47
47
|
# Lambda that returns a hash with remote attributes by id.
|
48
48
|
#
|
49
49
|
# @example Common use case
|
50
|
-
# class FooSynchronizer <
|
50
|
+
# class FooSynchronizer < Synchronisable::Synchronizer
|
51
51
|
# find do |id|
|
52
52
|
# remote_source.find { |h| h[:foo_id] == id } }
|
53
53
|
# end
|
@@ -57,33 +57,33 @@ module Synchronizable
|
|
57
57
|
# Lambda, that will be called before synchronization
|
58
58
|
# of each record and its assocations.
|
59
59
|
#
|
60
|
-
# @param source [
|
60
|
+
# @param source [Synchronisable::Source] synchronization source
|
61
61
|
#
|
62
62
|
# @return [Boolean] `true` to continue sync, `false` to cancel
|
63
63
|
method :before_sync
|
64
64
|
# Lambda, that will be called every time
|
65
65
|
# after record and its associations is synchronized.
|
66
66
|
#
|
67
|
-
# @param source [
|
67
|
+
# @param source [Synchronisable::Source] synchronization source
|
68
68
|
method :after_sync
|
69
69
|
|
70
70
|
# Lambda, that will be called before synchronization of each record.
|
71
71
|
#
|
72
|
-
# @param source [
|
72
|
+
# @param source [Synchronisable::Source] synchronization source
|
73
73
|
#
|
74
74
|
# @return [Boolean] `true` to continue sync, `false` to cancel
|
75
75
|
method :before_record_sync
|
76
76
|
|
77
77
|
# Lambda, that will be called every time after record is synchronized.
|
78
78
|
#
|
79
|
-
# @param source [
|
79
|
+
# @param source [Synchronisable::Source] synchronization source
|
80
80
|
method :after_record_sync
|
81
81
|
|
82
82
|
# Lambda, that will be called before each association sync.
|
83
83
|
#
|
84
|
-
# @param source [
|
84
|
+
# @param source [Synchronisable::Source] synchronization source
|
85
85
|
# @param id association remote id
|
86
|
-
# @param association [
|
86
|
+
# @param association [Synchronisable::DSL::Associations::Association]
|
87
87
|
# association builder
|
88
88
|
#
|
89
89
|
# @return [Boolean] `true` to continue sync, `false` to cancel
|
@@ -91,9 +91,9 @@ module Synchronizable
|
|
91
91
|
|
92
92
|
# Lambda, that will be called every time after association if synchronized.
|
93
93
|
#
|
94
|
-
# @param source [
|
94
|
+
# @param source [Synchronisable::Source] synchronization source
|
95
95
|
# @param id association remote id
|
96
|
-
# @param association [
|
96
|
+
# @param association [Synchronisable::DSL::Associations::Association]
|
97
97
|
# association builder
|
98
98
|
method :after_association_sync
|
99
99
|
|
@@ -159,7 +159,7 @@ module Synchronizable
|
|
159
159
|
after.(*args) if after
|
160
160
|
end
|
161
161
|
|
162
|
-
# Throws the {
|
162
|
+
# Throws the {Synchronisable::MissedRemoteIdError} if given id isn't present.
|
163
163
|
#
|
164
164
|
# @param id id to check
|
165
165
|
#
|
@@ -1,9 +1,9 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
require '
|
1
|
+
require 'synchronisable/error_handler'
|
2
|
+
require 'synchronisable/context'
|
3
|
+
require 'synchronisable/source'
|
4
|
+
require 'synchronisable/models/import'
|
5
5
|
|
6
|
-
module
|
6
|
+
module Synchronisable
|
7
7
|
# Responsible for model synchronization.
|
8
8
|
#
|
9
9
|
# @api private
|
@@ -20,7 +20,7 @@ module Synchronizable
|
|
20
20
|
# @overload run(model, data)
|
21
21
|
# @overload run(model)
|
22
22
|
#
|
23
|
-
# @return [
|
23
|
+
# @return [Synchronisable::Context] synchronization context
|
24
24
|
def run(model, *args)
|
25
25
|
options = args.extract_options!
|
26
26
|
data = args.first
|
@@ -35,7 +35,7 @@ module Synchronizable
|
|
35
35
|
# If not specified worker will try to get the data
|
36
36
|
# using `fetch` lambda/proc defined in corresponding synchronizer
|
37
37
|
#
|
38
|
-
# @return [
|
38
|
+
# @return [Synchronisable::Context] synchronization context
|
39
39
|
def run(data)
|
40
40
|
sync do |context|
|
41
41
|
error_handler = ErrorHandler.new(@logger, context)
|
@@ -87,7 +87,7 @@ module Synchronizable
|
|
87
87
|
|
88
88
|
# Method called by {#run} for each remote model attribute hash
|
89
89
|
#
|
90
|
-
# @param source [
|
90
|
+
# @param source [Synchronisable::Source] synchronization source
|
91
91
|
#
|
92
92
|
# @return [Boolean] `true` if synchronization was completed
|
93
93
|
# without errors, `false` otherwise
|
@@ -117,8 +117,8 @@ module Synchronizable
|
|
117
117
|
def create_record_pair(source)
|
118
118
|
local_record = @model.create!(source.local_attrs)
|
119
119
|
import_record = Import.create!(
|
120
|
-
:
|
121
|
-
:
|
120
|
+
:synchronisable_id => local_record.id,
|
121
|
+
:synchronisable_type => @model.to_s,
|
122
122
|
:remote_id => source.remote_id,
|
123
123
|
:attrs => source.local_attrs
|
124
124
|
)
|
@@ -143,10 +143,10 @@ module Synchronizable
|
|
143
143
|
|
144
144
|
# Synchronizes associations.
|
145
145
|
#
|
146
|
-
# @param source [
|
146
|
+
# @param source [Synchronisable::Source] synchronization source
|
147
147
|
#
|
148
|
-
# @see
|
149
|
-
# @see
|
148
|
+
# @see Synchronisable::DSL::Associations
|
149
|
+
# @see Synchronisable::DSL::Associations::Association
|
150
150
|
def sync_associations(source)
|
151
151
|
if verbose_logging? && source.associations.present?
|
152
152
|
@logger.info "starting associations sync"
|
@@ -185,7 +185,7 @@ module Synchronizable
|
|
185
185
|
end
|
186
186
|
|
187
187
|
def verbose_logging?
|
188
|
-
|
188
|
+
Synchronisable.logging[:verbose]
|
189
189
|
end
|
190
190
|
end
|
191
191
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class MatchSynchronizer <
|
1
|
+
class MatchSynchronizer < Synchronisable::Synchronizer
|
2
2
|
@gateway = MatchGateway.new
|
3
3
|
|
4
4
|
has_one :team, key: 'home_team_id'
|
@@ -39,12 +39,12 @@ class MatchSynchronizer < Synchronizable::Synchronizer
|
|
39
39
|
match = source.local_record
|
40
40
|
remote_player_ids = source.remote_attrs[ref_type.to_sym] || {}
|
41
41
|
|
42
|
-
player_imports =
|
43
|
-
.
|
42
|
+
player_imports = Synchronisable::Import
|
43
|
+
.with_synchronisable_type(Player)
|
44
44
|
.with_remote_ids(remote_player_ids)
|
45
45
|
|
46
46
|
local_player_ids = player_imports.map do |import|
|
47
|
-
import.
|
47
|
+
import.synchronisable.id
|
48
48
|
end
|
49
49
|
|
50
50
|
local_player_ids.each do |player_id|
|
@@ -60,8 +60,8 @@ class MatchSynchronizer < Synchronizable::Synchronizer
|
|
60
60
|
match_player = MatchPlayer.find_by(attrs)
|
61
61
|
unless match_player
|
62
62
|
remote_id = "#{attrs[:match_id]}_#{attrs[:player_id]}"
|
63
|
-
|
64
|
-
:
|
63
|
+
Synchronisable::Import.create!(
|
64
|
+
:synchronisable => MatchPlayer.create!(attrs),
|
65
65
|
:remote_id => remote_id,
|
66
66
|
:attrs => attrs
|
67
67
|
)
|
@@ -1,2 +1,2 @@
|
|
1
|
-
|
1
|
+
Synchronisable.configure do |config|
|
2
2
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
class CreateImports < ActiveRecord::Migration
|
2
2
|
def self.up
|
3
3
|
create_table :imports do |t|
|
4
|
-
t.string :
|
5
|
-
t.integer :
|
4
|
+
t.string :synchronisable_type, null: false
|
5
|
+
t.integer :synchronisable_id, null: false
|
6
6
|
t.text :attrs
|
7
7
|
t.string :remote_id, null: false
|
8
8
|
|
@@ -10,12 +10,12 @@ class CreateImports < ActiveRecord::Migration
|
|
10
10
|
end
|
11
11
|
|
12
12
|
add_index :imports, :remote_id
|
13
|
-
add_index :imports, [:
|
13
|
+
add_index :imports, [:synchronisable_type, :synchronisable_id]
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.down
|
17
17
|
remove_index :imports, :remote_id
|
18
|
-
remove_index :imports, [:
|
18
|
+
remove_index :imports, [:synchronisable_type, :synchronisable_id]
|
19
19
|
drop_table :imports
|
20
20
|
end
|
21
21
|
end
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -14,8 +14,8 @@
|
|
14
14
|
ActiveRecord::Schema.define(version: 20140507140039) do
|
15
15
|
|
16
16
|
create_table "imports", force: true do |t|
|
17
|
-
t.string "
|
18
|
-
t.integer "
|
17
|
+
t.string "synchronisable_type", null: false
|
18
|
+
t.integer "synchronisable_id", null: false
|
19
19
|
t.text "attrs"
|
20
20
|
t.string "remote_id", null: false
|
21
21
|
t.datetime "created_at"
|
@@ -23,7 +23,7 @@ ActiveRecord::Schema.define(version: 20140507140039) do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
add_index "imports", ["remote_id"], name: "index_imports_on_remote_id"
|
26
|
-
add_index "imports", ["
|
26
|
+
add_index "imports", ["synchronisable_type", "synchronisable_id"], name: "index_imports_on_synchronisable_type_and_synchronisable_id"
|
27
27
|
|
28
28
|
create_table "match_players", force: true do |t|
|
29
29
|
t.integer "match_id"
|