synchronisable 0.0.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.
Files changed (131) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +20 -0
  3. data/.rspec +3 -0
  4. data/.ruby-version +2 -0
  5. data/.travis.yml +4 -0
  6. data/Gemfile +9 -0
  7. data/Guardfile +14 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +22 -0
  10. data/Rakefile +7 -0
  11. data/TODO.md +20 -0
  12. data/bin/autospec +16 -0
  13. data/bin/rake +16 -0
  14. data/bin/rspec +16 -0
  15. data/lib/generators/synchronizable/USAGE +2 -0
  16. data/lib/generators/synchronizable/install_generator.rb +24 -0
  17. data/lib/generators/synchronizable/templates/create_imports_migration.rb +21 -0
  18. data/lib/generators/synchronizable/templates/initializer.rb +14 -0
  19. data/lib/synchronizable.rb +92 -0
  20. data/lib/synchronizable/context.rb +25 -0
  21. data/lib/synchronizable/dsl/associations.rb +57 -0
  22. data/lib/synchronizable/dsl/associations/association.rb +59 -0
  23. data/lib/synchronizable/dsl/associations/has_many.rb +12 -0
  24. data/lib/synchronizable/dsl/associations/has_one.rb +13 -0
  25. data/lib/synchronizable/dsl/macro.rb +100 -0
  26. data/lib/synchronizable/dsl/macro/attribute.rb +41 -0
  27. data/lib/synchronizable/dsl/macro/expression.rb +51 -0
  28. data/lib/synchronizable/dsl/macro/method.rb +15 -0
  29. data/lib/synchronizable/error_handler.rb +50 -0
  30. data/lib/synchronizable/exceptions.rb +8 -0
  31. data/lib/synchronizable/locale/en.yml +17 -0
  32. data/lib/synchronizable/locale/ru.yml +17 -0
  33. data/lib/synchronizable/model.rb +54 -0
  34. data/lib/synchronizable/model/methods.rb +55 -0
  35. data/lib/synchronizable/models/import.rb +24 -0
  36. data/lib/synchronizable/source.rb +72 -0
  37. data/lib/synchronizable/synchronizer.rb +177 -0
  38. data/lib/synchronizable/synchronizers/synchronizer_default.rb +10 -0
  39. data/lib/synchronizable/version.rb +10 -0
  40. data/lib/synchronizable/worker.rb +191 -0
  41. data/spec/dummy/.gitignore +16 -0
  42. data/spec/dummy/Rakefile +6 -0
  43. data/spec/dummy/app/assets/images/.keep +0 -0
  44. data/spec/dummy/app/assets/javascripts/application.js +16 -0
  45. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  46. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  47. data/spec/dummy/app/gateways/gateway_base.rb +19 -0
  48. data/spec/dummy/app/gateways/match_gateway.rb +15 -0
  49. data/spec/dummy/app/gateways/player_gateway.rb +26 -0
  50. data/spec/dummy/app/gateways/stage_gateway.rb +18 -0
  51. data/spec/dummy/app/gateways/team_gateway.rb +18 -0
  52. data/spec/dummy/app/gateways/tournament_gateway.rb +15 -0
  53. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  54. data/spec/dummy/app/mailers/.keep +0 -0
  55. data/spec/dummy/app/models/.keep +0 -0
  56. data/spec/dummy/app/models/match.rb +10 -0
  57. data/spec/dummy/app/models/match_player.rb +15 -0
  58. data/spec/dummy/app/models/player.rb +5 -0
  59. data/spec/dummy/app/models/stadium.rb +7 -0
  60. data/spec/dummy/app/models/stage.rb +6 -0
  61. data/spec/dummy/app/models/team.rb +5 -0
  62. data/spec/dummy/app/models/tournament.rb +7 -0
  63. data/spec/dummy/app/synchronizers/break_convention_team_synchronizer.rb +14 -0
  64. data/spec/dummy/app/synchronizers/match_synchronizer.rb +71 -0
  65. data/spec/dummy/app/synchronizers/player_synchronizer.rb +19 -0
  66. data/spec/dummy/app/synchronizers/stage_synchronizer.rb +20 -0
  67. data/spec/dummy/app/synchronizers/tournament_synchronizer.rb +20 -0
  68. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  69. data/spec/dummy/bin/bundle +3 -0
  70. data/spec/dummy/bin/rails +4 -0
  71. data/spec/dummy/bin/rake +4 -0
  72. data/spec/dummy/config.ru +4 -0
  73. data/spec/dummy/config/application.rb +26 -0
  74. data/spec/dummy/config/boot.rb +6 -0
  75. data/spec/dummy/config/database.yml +25 -0
  76. data/spec/dummy/config/environment.rb +5 -0
  77. data/spec/dummy/config/environments/development.rb +33 -0
  78. data/spec/dummy/config/environments/production.rb +80 -0
  79. data/spec/dummy/config/environments/test.rb +36 -0
  80. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  81. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  82. data/spec/dummy/config/initializers/inflections.rb +20 -0
  83. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  84. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  85. data/spec/dummy/config/initializers/session_store.rb +3 -0
  86. data/spec/dummy/config/initializers/synchronizable.rb +2 -0
  87. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  88. data/spec/dummy/config/locales/en.yml +23 -0
  89. data/spec/dummy/config/routes.rb +56 -0
  90. data/spec/dummy/db/migrate/20140422132431_create_teams.rb +11 -0
  91. data/spec/dummy/db/migrate/20140422132544_create_matches.rb +12 -0
  92. data/spec/dummy/db/migrate/20140422132708_create_players.rb +15 -0
  93. data/spec/dummy/db/migrate/20140422133122_create_match_players.rb +12 -0
  94. data/spec/dummy/db/migrate/20140422135244_create_imports.rb +21 -0
  95. data/spec/dummy/db/migrate/20140422140817_create_stadiums.rb +10 -0
  96. data/spec/dummy/db/migrate/20140507135800_create_tournaments.rb +13 -0
  97. data/spec/dummy/db/migrate/20140507135837_create_stages.rb +13 -0
  98. data/spec/dummy/db/migrate/20140507140039_add_stage_id_to_matches.rb +6 -0
  99. data/spec/dummy/db/schema.rb +103 -0
  100. data/spec/dummy/db/seeds.rb +7 -0
  101. data/spec/dummy/lib/assets/.keep +0 -0
  102. data/spec/dummy/lib/tasks/.keep +0 -0
  103. data/spec/dummy/log/.keep +0 -0
  104. data/spec/dummy/public/404.html +58 -0
  105. data/spec/dummy/public/422.html +58 -0
  106. data/spec/dummy/public/500.html +57 -0
  107. data/spec/dummy/public/favicon.ico +0 -0
  108. data/spec/dummy/public/robots.txt +5 -0
  109. data/spec/dummy/vendor/assets/javascripts/.keep +0 -0
  110. data/spec/dummy/vendor/assets/stylesheets/.keep +0 -0
  111. data/spec/factories/import.rb +5 -0
  112. data/spec/factories/match.rb +9 -0
  113. data/spec/factories/match_player.rb +9 -0
  114. data/spec/factories/player.rb +10 -0
  115. data/spec/factories/remote/match.rb +21 -0
  116. data/spec/factories/remote/player.rb +18 -0
  117. data/spec/factories/remote/stage.rb +26 -0
  118. data/spec/factories/remote/team.rb +21 -0
  119. data/spec/factories/remote/tournament.rb +18 -0
  120. data/spec/factories/stadium.rb +7 -0
  121. data/spec/factories/team.rb +16 -0
  122. data/spec/models/match_spec.rb +41 -0
  123. data/spec/models/team_spec.rb +85 -0
  124. data/spec/spec_helper.rb +64 -0
  125. data/spec/synchronizable/dsl/macro_spec.rb +59 -0
  126. data/spec/synchronizable/models/import_spec.rb +10 -0
  127. data/spec/synchronizable/support/has_macro.rb +12 -0
  128. data/spec/synchronizable/support/has_macro_subclass.rb +9 -0
  129. data/spec/synchronizable/synchronizable_spec.rb +60 -0
  130. data/synchronizable.gemspec +39 -0
  131. metadata +506 -0
@@ -0,0 +1,16 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Dummy::Application.load_tasks
File without changes
@@ -0,0 +1,16 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require turbolinks
16
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,19 @@
1
+ class GatewayBase
2
+ def id_key
3
+ raise NotImplementedError,
4
+ I18n.t('errors.gateway_method_missing', method: 'id_key')
5
+ end
6
+
7
+ def source
8
+ raise NotImplementedError,
9
+ I18n.t('errors.gateway_method_missing', method: 'source')
10
+ end
11
+
12
+ def fetch
13
+ source
14
+ end
15
+
16
+ def find(id)
17
+ source.find { |h| h[id_key] == id }
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ class MatchGateway < GatewayBase
2
+ def id_key
3
+ :match_id
4
+ end
5
+
6
+ def source
7
+ @source ||= [
8
+ FactoryGirl.build(:remote_match,
9
+ :match_id => 'match_0',
10
+ :home_team => 'team_0',
11
+ :away_team => 'team_1'
12
+ )
13
+ ]
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ class PlayerGateway < GatewayBase
2
+ def id_key
3
+ :player_id
4
+ end
5
+
6
+ def source
7
+ @source ||= [
8
+ FactoryGirl.build(:remote_player,
9
+ :team => 'team_0',
10
+ :player_id => 'player_0'
11
+ ),
12
+ FactoryGirl.build(:remote_player,
13
+ :team => 'team_0',
14
+ :player_id => 'player_2'
15
+ ),
16
+ FactoryGirl.build(:remote_player,
17
+ :team => 'team_1',
18
+ :player_id => 'player_1'
19
+ ),
20
+ FactoryGirl.build(:remote_player,
21
+ :team => 'team_1',
22
+ :player_id => 'player_3'
23
+ )
24
+ ]
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ class StageGateway < GatewayBase
2
+ def id_key
3
+ :stage_id
4
+ end
5
+
6
+ def source
7
+ @source ||= [
8
+ FactoryGirl.build(:remote_stage,
9
+ :stage_id => 'stage_0',
10
+ :matches_ids => %w(match_0)
11
+ ),
12
+ FactoryGirl.build( :remote_stage,
13
+ :stage_id => 'stage_1',
14
+ :matches_ids => %w(match_0)
15
+ )
16
+ ]
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ class TeamGateway < GatewayBase
2
+ def id_key
3
+ :maet_id
4
+ end
5
+
6
+ def source
7
+ @source ||= [
8
+ FactoryGirl.build(:remote_team,
9
+ :maet_id => 'team_0',
10
+ :players_ids => %w(player_0 player_2)
11
+ ),
12
+ FactoryGirl.build(:remote_team,
13
+ :maet_id => 'team_1',
14
+ :players_ids => %w(player_1 player_3)
15
+ )
16
+ ]
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ class TournamentGateway < GatewayBase
2
+ def id_key
3
+ :tour_id
4
+ end
5
+
6
+ def source
7
+ @source ||= [
8
+ FactoryGirl.build(:remote_tournament,
9
+ :tour_id => 'tournament_0',
10
+ :is_current => true,
11
+ :stages_ids => %w(stage_0 stage_1)
12
+ )
13
+ ]
14
+ end
15
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
File without changes
@@ -0,0 +1,10 @@
1
+ class Match < ActiveRecord::Base
2
+ belongs_to :home_team, class_name: 'Team'
3
+ belongs_to :away_team, class_name: 'Team'
4
+
5
+ belongs_to :stage
6
+
7
+ has_many :match_players, dependent: :destroy
8
+
9
+ synchronizable
10
+ end
@@ -0,0 +1,15 @@
1
+ class MatchPlayer < ActiveRecord::Base
2
+ REF_TYPES = [
3
+ REF_TYPE_LINE_UP_HOME = 'line_up_home',
4
+ REF_TYPE_LINE_UP_AWAY = 'line_up_away',
5
+ REF_TYPE_SUBSTITUTES_HOME = 'substitutes_home',
6
+ REF_TYPE_SUBSTITUTES_AWAY = 'substitutes_away'
7
+ ]
8
+
9
+ belongs_to :match
10
+ belongs_to :player
11
+
12
+ validates :match, :player, presence: true
13
+
14
+ synchronizable
15
+ end
@@ -0,0 +1,5 @@
1
+ class Player < ActiveRecord::Base
2
+ belongs_to :team
3
+
4
+ synchronizable
5
+ end
@@ -0,0 +1,7 @@
1
+ class Stadium < ActiveRecord::Base
2
+ # There is no synchronizer defined for this model,
3
+ # so synchronization worker is gonna try
4
+ # to use remote attributes as is to create a record
5
+
6
+ synchronizable
7
+ end
@@ -0,0 +1,6 @@
1
+ class Stage < ActiveRecord::Base
2
+ belongs_to :tournament
3
+ has_many :matches
4
+
5
+ synchronizable
6
+ end
@@ -0,0 +1,5 @@
1
+ class Team < ActiveRecord::Base
2
+ has_many :players
3
+
4
+ synchronizable BreakConventionTeamSynchronizer
5
+ end
@@ -0,0 +1,7 @@
1
+ class Tournament < ActiveRecord::Base
2
+ has_many :stages
3
+
4
+ validates :name, :beginning, :ending, :presence => true
5
+
6
+ synchronizable
7
+ end
@@ -0,0 +1,14 @@
1
+ class BreakConventionTeamSynchronizer < Synchronizable::Synchronizer
2
+ @gateway = TeamGateway.new
3
+
4
+ remote_id :maet_id
5
+ mappings(
6
+ :eman => :name,
7
+ :yrtnuoc => :country,
8
+ :ytic => :city
9
+ )
10
+ except :ignored_1, :ignored_2
11
+
12
+ find { |id| @gateway.find(id) }
13
+ fetch { @gateway.fetch }
14
+ end
@@ -0,0 +1,71 @@
1
+ class MatchSynchronizer < Synchronizable::Synchronizer
2
+ @gateway = MatchGateway.new
3
+
4
+ has_one :team, key: 'home_team_id'
5
+ has_one :team, key: 'away_team_id'
6
+
7
+ has_many :players
8
+
9
+ remote_id :match_id
10
+
11
+ mappings(
12
+ :gninnigeb => :beginning,
13
+ :home_team => :home_team_id,
14
+ :away_team => :away_team_id,
15
+ :rehtaew => :weather,
16
+ :ln_home => :line_up_home,
17
+ :ln_away => :line_up_away,
18
+ :sub_home => :substitutes_home,
19
+ :sub_away => :substitutes_away
20
+ )
21
+
22
+ except :ignored_1, :ignored_2,
23
+ :line_up_home, :line_up_away,
24
+ :substitutes_home, :substitutes_away
25
+
26
+ destroy_missed true
27
+
28
+ find { |id| @gateway.find(id) }
29
+ fetch { @gateway.fetch }
30
+
31
+ after_sync do |source|
32
+ MatchPlayer::REF_TYPES.each do |ref_type|
33
+ sync_match_players(source, ref_type)
34
+ end
35
+ end
36
+
37
+ class << self
38
+ def sync_match_players(source, ref_type)
39
+ match = source.local_record
40
+ remote_player_ids = source.remote_attrs[ref_type.to_sym] || {}
41
+
42
+ player_imports = Synchronizable::Import
43
+ .with_synchronizable_type(Player)
44
+ .with_remote_ids(remote_player_ids)
45
+
46
+ local_player_ids = player_imports.map do |import|
47
+ import.synchronizable.id
48
+ end
49
+
50
+ local_player_ids.each do |player_id|
51
+ sync_match_player({
52
+ :match_id => match.id,
53
+ :player_id => player_id,
54
+ :ref_type => ref_type
55
+ })
56
+ end
57
+ end
58
+
59
+ def sync_match_player(attrs)
60
+ match_player = MatchPlayer.find_by(attrs)
61
+ unless match_player
62
+ remote_id = "#{attrs[:match_id]}_#{attrs[:player_id]}"
63
+ Synchronizable::Import.create!(
64
+ :synchronizable => MatchPlayer.create!(attrs),
65
+ :remote_id => remote_id,
66
+ :attrs => attrs
67
+ )
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,19 @@
1
+ class PlayerSynchronizer < Synchronizable::Synchronizer
2
+ @gateway = PlayerGateway.new
3
+
4
+ remote_id :player_id
5
+
6
+ mappings(
7
+ :eman_tsrif => :first_name,
8
+ :eman_tsal => :last_name,
9
+ :yadhtrib => :birthday,
10
+ :pihsnezitic => :citizenship,
11
+ :thgieh => :height,
12
+ :thgiew => :weight,
13
+ :team => :team_id
14
+ )
15
+ only :team_id, :first_name, :last_name
16
+
17
+ find { |id| @gateway.find(id) }
18
+ fetch { @gateway.fetch }
19
+ end
@@ -0,0 +1,20 @@
1
+ class StageSynchronizer < Synchronizable::Synchronizer
2
+ @gateway = StageGateway.new
3
+
4
+ has_many :matches
5
+
6
+ remote_id :stage_id
7
+
8
+ mappings(
9
+ :tour_id => :tournament_id,
10
+ :gninnigeb => :beginning,
11
+ :gnidge => :ending,
12
+ :eman => :name,
13
+ :rebmun => :number
14
+ )
15
+
16
+ except :ignored_1, :ignored_2
17
+
18
+ find { |id| @gateway.find(id) }
19
+ fetch { @gateway.fetch }
20
+ end
@@ -0,0 +1,20 @@
1
+ class TournamentSynchronizer < Synchronizable::Synchronizer
2
+ @gateway = TournamentGateway.new
3
+
4
+ has_many :stages
5
+
6
+ remote_id :tour_id
7
+
8
+ mappings(
9
+ :eman => :name,
10
+ :eman_trohs => :short_name,
11
+ :gninnigeb => :beginning,
12
+ :gnidge => :ending,
13
+ :tnerruc_si => :is_current
14
+ )
15
+
16
+ only :name, :beginning, :ending
17
+
18
+ find { |id| @gateway.find(id) }
19
+ fetch { @gateway.fetch }
20
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6
+ <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')