sync_machine 0.1.0 → 0.2.0

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
- SHA1:
3
- metadata.gz: 594e4e0870147957bb2f36f65b6b028433bf5ee3
4
- data.tar.gz: 58a8af1ae9acd3b64fdff79414d6fcc3e7499332
2
+ SHA256:
3
+ metadata.gz: d74a6108d033784c23a0dbcbf0212523dbb8498b55b867d630272806f726ae19
4
+ data.tar.gz: 18b5753d9abff76d0dec3c5d98e5b0660deeb45ae525eb4db3aea25741a33f60
5
5
  SHA512:
6
- metadata.gz: da5e1182ccf5d8623190e170826e122bc6b60a819a707c4f16c61167009d90140e35263ce9f634f95bf66d17065c047cbffa940320e68a475a0939b2ca48ee80
7
- data.tar.gz: 0a94e81f19b4096e233fa457946c59ee5a0e669e95e908fe9e80ba9efa49b359f8a8b7ccf3517ebb7e839789057bf2c8e8b8a1b684cabaf6938653ee25f15921
6
+ metadata.gz: 1c7100fd78a1809760743413f3024779243b1bb6d2d2963b1ad074b92e46cf1f196f0ab9b446fd2a7179bfe36ee0bfefdfd42ff82feccb8e97ca6091b7063ca9
7
+ data.tar.gz: c09a284f84e695dcb6a8935d1ca8510589e5094f235da6c011d14c7293aca9629990c0c92897ccba08f7d538114490a6fbcb703e1c56dcb707789348faadaef8
data/.circleci/config.yml CHANGED
@@ -21,13 +21,6 @@ jobs:
21
21
  steps:
22
22
  - checkout
23
23
 
24
- # Download and cache dependencies
25
- - restore_cache:
26
- keys:
27
- - v1-dependencies-{{ checksum "Gemfile.lock" }}
28
- # fallback to using the latest cache if no exact match is found
29
- - v1-dependencies-
30
-
31
24
  - run:
32
25
  name: Configure Bundler
33
26
  command: |
@@ -40,11 +33,6 @@ jobs:
40
33
  command: |
41
34
  bundle install --jobs=4 --retry=3 --path vendor/bundle
42
35
 
43
- - save_cache:
44
- paths:
45
- - ./vendor/bundle
46
- key: v1-dependencies-{{ checksum "Gemfile.lock" }}
47
-
48
36
  # run tests!
49
37
  - run:
50
38
  name: run tests
data/.gitignore CHANGED
@@ -9,3 +9,5 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ Gemfile.lock
@@ -7,6 +7,7 @@ module SyncMachine
7
7
  class ChangeListener
8
8
  def self.inherited(base)
9
9
  base.cattr_accessor :model_syms
10
+ base.subscribe
10
11
  end
11
12
 
12
13
  def self.listen_to_models(*model_syms)
@@ -15,7 +16,7 @@ module SyncMachine
15
16
  send(
16
17
  :alias_method,
17
18
  "update_#{model_sym}_successful".to_sym,
18
- :after_subject_saved
19
+ :after_record_saved
19
20
  )
20
21
  end
21
22
  end
@@ -24,34 +25,36 @@ module SyncMachine
24
25
  Wisper.subscribe(new)
25
26
  end
26
27
 
27
- def after_create(subject)
28
- model_sym = subject.class.name.underscore.to_sym
28
+ def after_create(record)
29
+ model_sym = record.class.name.underscore.to_sym
29
30
  return unless self.class.model_syms.include?(model_sym)
30
- after_subject_saved(subject)
31
+ after_record_saved(record)
31
32
  end
32
33
 
33
- def after_subject_saved(subject)
34
- return unless changed_keys(subject).present?
34
+ def after_record_saved(record)
35
+ return unless changed_keys(record).present?
35
36
  sync_module = SyncMachine.sync_module(self.class)
36
37
  finder_class = sync_module.const_get('FindSubjectsWorker')
37
38
  finder_class.perform_async(
38
- subject.class.name,
39
- subject.id.to_s,
40
- changed_keys(subject),
39
+ record.class.name,
40
+ record_id_for_job(record.id),
41
+ changed_keys(record),
41
42
  Time.now.iso8601
42
43
  )
43
44
  end
44
45
 
45
46
  private
46
47
 
47
- def changed_keys(subject)
48
- changed_keys = subject.changes.keys
49
- subject.class.reflect_on_all_associations.each do |assoc|
50
- if assoc.macro == :embeds_one && subject.send(assoc.name).try(:changed?)
51
- changed_keys << assoc.name
52
- end
53
- end
54
- changed_keys
48
+ def changed_keys(record)
49
+ orm_adapter.change_listener_changed_keys(record)
50
+ end
51
+
52
+ def record_id_for_job(record_id)
53
+ orm_adapter.record_id_for_job(record_id)
54
+ end
55
+
56
+ def orm_adapter
57
+ SyncMachine.orm_adapter
55
58
  end
56
59
  end
57
60
  end
@@ -38,7 +38,7 @@ module SyncMachine
38
38
  payload_class.create!(
39
39
  body: payload_body,
40
40
  generated_at: Time.now,
41
- subject_id: @subject_id.to_s
41
+ subject_id: SyncMachine.orm_adapter.record_id_for_job(@subject_id)
42
42
  )
43
43
  end
44
44
 
@@ -56,7 +56,7 @@ module SyncMachine
56
56
  end
57
57
 
58
58
  def subject_id
59
- @subject.id.to_s
59
+ SyncMachine.orm_adapter.record_id_for_job(@subject.id)
60
60
  end
61
61
  end
62
62
  end
@@ -53,7 +53,9 @@ module SyncMachine
53
53
  else
54
54
  @block.call(record)
55
55
  end
56
- Array.wrap(raw_source_ids).map(&:to_s)
56
+ Array.wrap(raw_source_ids).map { |raw_source_id|
57
+ SyncMachine.orm_adapter.record_id_for_job(raw_source_id)
58
+ }
57
59
  end
58
60
  end
59
61
  end
@@ -0,0 +1,20 @@
1
+ module SyncMachine
2
+ module OrmAdapter
3
+ # Adapt SyncMachine for use with ActiveRecord-based applications.
4
+ module ActiveRecord
5
+ def self.setup
6
+ require "active_record"
7
+ require "wisper/active_record"
8
+ Wisper::ActiveRecord.extend_all
9
+ end
10
+
11
+ def self.change_listener_changed_keys(record)
12
+ record.previous_changes.keys
13
+ end
14
+
15
+ def self.record_id_for_job(record_id)
16
+ record_id
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module SyncMachine
2
+ module OrmAdapter
3
+ # Adapt SyncMachine for use with Mongoid-based applications.
4
+ module Mongoid
5
+ def self.setup
6
+ require "mongoid"
7
+ require "wisper/mongoid"
8
+ Wisper::Mongoid.extend_all
9
+ end
10
+
11
+ def self.change_listener_changed_keys(record)
12
+ record.changes.keys
13
+ end
14
+
15
+ def self.record_id_for_job(record_id)
16
+ record_id.to_s
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module SyncMachine
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
data/lib/sync_machine.rb CHANGED
@@ -1,20 +1,47 @@
1
1
  require "active_support/core_ext/class"
2
2
  require "active_support/core_ext/string"
3
- require "mongoid"
4
3
  require "sync_machine/change_listener"
5
4
  require "sync_machine/ensure_publication"
6
5
  require "sync_machine/ensure_publication/deduper"
7
6
  require "sync_machine/ensure_publication/publication_history"
8
7
  require "sync_machine/ensure_publication_worker"
9
8
  require "sync_machine/find_subjects_worker"
9
+ require "sync_machine/orm_adapter/active_record"
10
+ require "sync_machine/orm_adapter/mongoid"
10
11
  require "sync_machine/version"
11
- require "wisper/mongoid"
12
12
 
13
13
  # A mini-framework for intelligently publishing complex model changes to an
14
14
  # external API..
15
15
  module SyncMachine
16
+ mattr_accessor :orm_adapter
17
+
18
+ def self.use_active_record
19
+ setup_orm_adapter(OrmAdapter::ActiveRecord)
20
+ end
21
+
22
+ def self.use_mongoid
23
+ setup_orm_adapter(OrmAdapter::Mongoid)
24
+ end
25
+
26
+ def self.setup_orm_adapter(orm_adapter)
27
+ self.orm_adapter = orm_adapter
28
+ orm_adapter.setup
29
+ end
30
+
16
31
  def self.extended(base)
17
32
  base.mattr_accessor :subject_sym
33
+
34
+ # Force loading of all relevant classes. Should only be necessary when
35
+ # running your application in a way that it defers loading constants, i.e.,
36
+ # Rails' development or test mode.
37
+ def base.eager_load
38
+ const_names = %w(
39
+ Payload FindSubjectsWorker EnsurePublicationWorker ChangeListener
40
+ )
41
+ const_names.each do |const_name|
42
+ const_get(const_name)
43
+ end
44
+ end
18
45
  end
19
46
 
20
47
  def self.sync_module(child_const)
@@ -28,29 +55,4 @@ module SyncMachine
28
55
  def subject_class
29
56
  subject_sym.to_s.camelize.constantize
30
57
  end
31
-
32
- def setup
33
- define_payload_class unless const_defined?('Payload')
34
- const_get('ChangeListener').subscribe
35
- end
36
-
37
- # :reek:TooManyStatements is unavoidable with this sort of dynamic class
38
- # definition
39
- def define_payload_class
40
- collection_name = name.underscore + '_payloads'
41
- payload_class = Class.new do
42
- include Mongoid::Document
43
- store_in collection: collection_name
44
-
45
- field :body, type: Hash
46
- field :generated_at, type: Time
47
- field :subject_id, type: String
48
-
49
- validates :generated_at, presence: true
50
- validates :subject_id, presence: true, uniqueness: true
51
- end
52
- const_set('Payload', payload_class)
53
- end
54
58
  end
55
-
56
- Wisper::Mongoid.extend_all
data/sync_machine.gemspec CHANGED
@@ -36,14 +36,16 @@ Gem::Specification.new do |spec|
36
36
  spec.require_paths = ["lib"]
37
37
 
38
38
  spec.add_runtime_dependency "activesupport"
39
- spec.add_runtime_dependency "fhwang-wisper-mongoid"
40
- spec.add_runtime_dependency "mongoid"
41
39
  spec.add_runtime_dependency "sidekiq"
42
40
  spec.add_runtime_dependency "wisper"
43
41
 
44
42
  spec.add_development_dependency "bundler", "~> 2.0"
45
43
  spec.add_development_dependency "factory_bot"
44
+ spec.add_development_dependency "fhwang-wisper-mongoid"
45
+ spec.add_development_dependency "mongoid"
46
46
  spec.add_development_dependency "rake", "~> 10.0"
47
47
  spec.add_development_dependency "rspec", "~> 3.0"
48
48
  spec.add_development_dependency "rspec_junit_formatter"
49
+ spec.add_development_dependency "sqlite3"
50
+ spec.add_development_dependency "wisper-activerecord"
49
51
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sync_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francis Hwang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-26 00:00:00.000000000 Z
11
+ date: 2019-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: fhwang-wisper-mongoid
28
+ name: sidekiq
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: mongoid
42
+ name: wisper
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,27 +53,27 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: sidekiq
56
+ name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :runtime
61
+ version: '2.0'
62
+ type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '2.0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: wisper
70
+ name: factory_bot
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
- type: :runtime
76
+ type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
@@ -81,21 +81,21 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: bundler
84
+ name: fhwang-wisper-mongoid
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: '2.0'
89
+ version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: '2.0'
96
+ version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: factory_bot
98
+ name: mongoid
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -150,6 +150,34 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: sqlite3
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: wisper-activerecord
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
153
181
  description: A mini-framework for intelligently publishing complex model changes to
154
182
  an external API.
155
183
  email:
@@ -167,7 +195,6 @@ files:
167
195
  - ".travis.yml"
168
196
  - CODE_OF_CONDUCT.md
169
197
  - Gemfile
170
- - Gemfile.lock
171
198
  - LICENSE.txt
172
199
  - README.md
173
200
  - Rakefile
@@ -180,6 +207,8 @@ files:
180
207
  - lib/sync_machine/ensure_publication/publication_history.rb
181
208
  - lib/sync_machine/ensure_publication_worker.rb
182
209
  - lib/sync_machine/find_subjects_worker.rb
210
+ - lib/sync_machine/orm_adapter/active_record.rb
211
+ - lib/sync_machine/orm_adapter/mongoid.rb
183
212
  - lib/sync_machine/version.rb
184
213
  - sync_machine.gemspec
185
214
  homepage: https://github.com/fhwang/sync_machine
@@ -204,7 +233,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
204
233
  version: '0'
205
234
  requirements: []
206
235
  rubyforge_project:
207
- rubygems_version: 2.6.14
236
+ rubygems_version: 2.7.8
208
237
  signing_key:
209
238
  specification_version: 4
210
239
  summary: A mini-framework for intelligently publishing complex model changes to an
data/Gemfile.lock DELETED
@@ -1,80 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- sync_machine (0.1.0)
5
- activesupport
6
- fhwang-wisper-mongoid
7
- mongoid
8
- sidekiq
9
- wisper
10
-
11
- GEM
12
- remote: https://rubygems.org/
13
- specs:
14
- activemodel (5.2.1)
15
- activesupport (= 5.2.1)
16
- activesupport (5.2.1)
17
- concurrent-ruby (~> 1.0, >= 1.0.2)
18
- i18n (>= 0.7, < 2)
19
- minitest (~> 5.1)
20
- tzinfo (~> 1.1)
21
- bson (4.3.0)
22
- concurrent-ruby (1.0.5)
23
- connection_pool (2.2.2)
24
- diff-lcs (1.3)
25
- factory_bot (5.0.2)
26
- activesupport (>= 4.2.0)
27
- fhwang-wisper-mongoid (0.2.0)
28
- mongoid (>= 6)
29
- wisper
30
- i18n (1.1.1)
31
- concurrent-ruby (~> 1.0)
32
- minitest (5.11.3)
33
- mongo (2.6.2)
34
- bson (>= 4.3.0, < 5.0.0)
35
- mongoid (6.4.2)
36
- activemodel (>= 5.1, < 6.0.0)
37
- mongo (>= 2.5.1, < 3.0.0)
38
- rack (2.0.6)
39
- rack-protection (2.0.4)
40
- rack
41
- rake (10.4.2)
42
- redis (4.0.2)
43
- rspec (3.6.0)
44
- rspec-core (~> 3.6.0)
45
- rspec-expectations (~> 3.6.0)
46
- rspec-mocks (~> 3.6.0)
47
- rspec-core (3.6.0)
48
- rspec-support (~> 3.6.0)
49
- rspec-expectations (3.6.0)
50
- diff-lcs (>= 1.2.0, < 2.0)
51
- rspec-support (~> 3.6.0)
52
- rspec-mocks (3.6.0)
53
- diff-lcs (>= 1.2.0, < 2.0)
54
- rspec-support (~> 3.6.0)
55
- rspec-support (3.6.0)
56
- rspec_junit_formatter (0.4.1)
57
- rspec-core (>= 2, < 4, != 2.12.0)
58
- sidekiq (5.0.5)
59
- concurrent-ruby (~> 1.0)
60
- connection_pool (~> 2.2, >= 2.2.0)
61
- rack-protection (>= 1.5.0)
62
- redis (>= 3.3.4, < 5)
63
- thread_safe (0.3.6)
64
- tzinfo (1.2.5)
65
- thread_safe (~> 0.1)
66
- wisper (2.0.0)
67
-
68
- PLATFORMS
69
- ruby
70
-
71
- DEPENDENCIES
72
- bundler (~> 2.0)
73
- factory_bot
74
- rake (~> 10.0)
75
- rspec (~> 3.0)
76
- rspec_junit_formatter
77
- sync_machine!
78
-
79
- BUNDLED WITH
80
- 2.0.1