sync_machine 0.8.1 → 1.0.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
2
  SHA256:
3
- metadata.gz: 6b00a4dbacf75b3f31caf20ef9e230ddbddea1f3e0fc0e6611b9f8956d89b135
4
- data.tar.gz: 98e2325ac8aa3dd3ab7d1fe378433df87f5ac66f5dbec252e3869e2ab3fb3597
3
+ metadata.gz: 590006776b984bcede930cae17327932ac2e5c2e6b0d4bfab23e3f3742d0ecfa
4
+ data.tar.gz: 7e04325fae0528ba6fe48d413827c694aed86f13d69460dfb96782f38f25bc03
5
5
  SHA512:
6
- metadata.gz: beec8451ec62b0392c92460aa7c962894740b077d7379d2bad335ff021c78548fe0b50373006e05cbfef7f4ec4a96e127a816aa20ac582a4ed06731b3d8bc841
7
- data.tar.gz: d6f026775a430eb5c6daa55563df3176b339b09b01020202e522af2e4bd1c0b3fdd635873f2ef0278a66cfa5b376e390d7b3ca5504c003c6b7691456d9dfa95c
6
+ metadata.gz: 6e236957b67a82d29556fa8f57c6fe00f49e0cdb0f4e9c081eb29c99a0123de65ff4aae5f3c6ca308e48eb635c957cda14ce13dc2e40e2f064f6ee17e6395fbb
7
+ data.tar.gz: d49cca0da353472f8fc561d9a612df8d63a0cd745aec41cf183bc173051719c65eaa2d63d6ab0ae63b7007169c2be6b7abce3c3fd72a3415324791d89eb55f07
data/lib/sync_machine.rb CHANGED
@@ -7,6 +7,7 @@ require "sync_machine/ensure_publication/deduper"
7
7
  require "sync_machine/ensure_publication/publication_history"
8
8
  require "sync_machine/ensure_publication_worker"
9
9
  require "sync_machine/find_subjects_worker"
10
+ require "sync_machine/orm_adapters"
10
11
  require "sync_machine/version"
11
12
 
12
13
  # A mini-framework for intelligently publishing complex model changes to an
@@ -33,6 +34,10 @@ module SyncMachine
33
34
  child_const.name.split(/::/)[0..-2].join('::').constantize
34
35
  end
35
36
 
37
+ def orm_adapter
38
+ SyncMachine::OrmAdapters.orm_adapter(self)
39
+ end
40
+
36
41
  def subject(subject_sym, opts = {})
37
42
  self.subject_sym = subject_sym
38
43
  self.subject_opts = ActiveSupport::HashWithIndifferentAccess.new(opts)
@@ -56,7 +56,7 @@ module SyncMachine
56
56
  end
57
57
 
58
58
  def orm_adapter
59
- SyncMachine.orm_adapter
59
+ SyncMachine.sync_module(self.class).orm_adapter
60
60
  end
61
61
  end
62
62
  end
@@ -56,7 +56,7 @@ module SyncMachine
56
56
  end
57
57
 
58
58
  def subject_id
59
- SyncMachine.orm_adapter.record_id_for_job(@subject.id)
59
+ @sync_module.orm_adapter.record_id_for_job(@subject.id)
60
60
  end
61
61
  end
62
62
  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: SyncMachine.orm_adapter.record_id_for_job(@subject_id)
41
+ subject_id: @sync_module.orm_adapter.record_id_for_job(@subject_id)
42
42
  )
43
43
  end
44
44
 
@@ -9,6 +9,12 @@ module SyncMachine
9
9
 
10
10
  class_attribute :hooks
11
11
 
12
+ def self.add_hook(meth, block)
13
+ sync_module = SyncMachine.sync_module(self)
14
+ hooks[meth] = Hook.new(sync_module, block)
15
+ end
16
+ private_class_method :add_hook
17
+
12
18
  def self.inherited(subclass)
13
19
  subclass.hooks = {}
14
20
  end
@@ -16,9 +22,9 @@ module SyncMachine
16
22
  def self.method_missing(meth, *args, &block)
17
23
  if meth.to_s == "subject_ids_from_#{parent.subject_sym}"
18
24
  block ||= ->(subject) { [subject.id.to_s] }
19
- hooks[meth] = Hook.new(block)
25
+ add_hook(meth, block)
20
26
  elsif meth.to_s =~ /^subject_ids_from_.*/
21
- hooks[meth] = Hook.new(block)
27
+ add_hook(meth, block)
22
28
  else
23
29
  super
24
30
  end
@@ -46,7 +52,8 @@ module SyncMachine
46
52
 
47
53
  # Wrap a "subject_ids_from_*" block.
48
54
  class Hook
49
- def initialize(block)
55
+ def initialize(sync_module, block)
56
+ @sync_module = sync_module
50
57
  @block = block
51
58
  end
52
59
 
@@ -57,7 +64,7 @@ module SyncMachine
57
64
  @block.call(record)
58
65
  end
59
66
  Array.wrap(raw_source_ids).map { |raw_source_id|
60
- SyncMachine.orm_adapter.record_id_for_job(raw_source_id)
67
+ @sync_module.orm_adapter.record_id_for_job(raw_source_id)
61
68
  }
62
69
  end
63
70
  end
@@ -0,0 +1,20 @@
1
+ Bundler.require(:default)
2
+
3
+ require "sync_machine/orm_adapters/active_record_adapter" if Module.const_defined?(:ActiveRecord)
4
+ require "sync_machine/orm_adapters/mongoid_adapter" if Module.const_defined?(:Mongoid)
5
+
6
+ module SyncMachine
7
+ # Adapt generic SyncMachine functionality to a specific ORM.
8
+ module OrmAdapters
9
+ def self.orm_adapter(sync_module)
10
+ subject_class = sync_module.subject_class
11
+ if const_defined?(:ActiveRecordAdapter) &&
12
+ subject_class < ActiveRecord::Base
13
+ ActiveRecordAdapter
14
+ elsif const_defined?(:MongoidAdapter) &&
15
+ subject_class.included_modules.include?(Mongoid::Document)
16
+ MongoidAdapter
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,28 @@
1
+ begin
2
+ require 'wisper/activerecord'
3
+ rescue LoadError
4
+ Kernel.abort(
5
+ "Please install the wisper-activerecord gem when using SyncMachine with ActiveRecord."
6
+ )
7
+ end
8
+
9
+ module SyncMachine
10
+ module OrmAdapters
11
+ # Adapt generic SyncMachine functionality to ActiveRecord.
12
+ module ActiveRecordAdapter
13
+ def self.change_listener_changed_keys(record)
14
+ record.previous_changes.keys
15
+ end
16
+
17
+ def self.record_id_for_job(record_id)
18
+ record_id
19
+ end
20
+
21
+ def self.sufficient_changes_to_find_subjects?(record)
22
+ change_listener_changed_keys(record).present?
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ Wisper::ActiveRecord.extend_all
@@ -0,0 +1,28 @@
1
+ begin
2
+ require 'wisper/mongoid'
3
+ rescue LoadError
4
+ Kernel.abort(
5
+ "Please install the fhwang-wisper-mongoid gem when using SyncMachine with Mongoid."
6
+ )
7
+ end
8
+
9
+ module SyncMachine
10
+ module OrmAdapters
11
+ # Adapt generic SyncMachine functionality to Mongoid.
12
+ module MongoidAdapter
13
+ def self.change_listener_changed_keys(record)
14
+ record.changes.keys
15
+ end
16
+
17
+ def self.record_id_for_job(record_id)
18
+ record_id.to_s
19
+ end
20
+
21
+ def self.sufficient_changes_to_find_subjects?(_record)
22
+ true
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ Wisper::Mongoid.extend_all
@@ -1,3 +1,3 @@
1
1
  module SyncMachine
2
- VERSION = "0.8.1".freeze
2
+ VERSION = "1.0.0".freeze
3
3
  end
data/sync_machine.gemspec CHANGED
@@ -39,9 +39,14 @@ Gem::Specification.new do |spec|
39
39
  spec.add_runtime_dependency "sidekiq"
40
40
  spec.add_runtime_dependency "wisper"
41
41
 
42
+ spec.add_development_dependency "activerecord"
42
43
  spec.add_development_dependency "bundler", "~> 2.0"
43
44
  spec.add_development_dependency "factory_bot"
45
+ spec.add_development_dependency "fhwang-wisper-mongoid"
46
+ spec.add_development_dependency "mongoid"
44
47
  spec.add_development_dependency "rake", "~> 10.0"
45
48
  spec.add_development_dependency "rspec", "~> 3.0"
46
49
  spec.add_development_dependency "rspec_junit_formatter"
50
+ spec.add_development_dependency "sqlite3"
51
+ spec.add_development_dependency "wisper-activerecord"
47
52
  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.8.1
4
+ version: 1.0.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-11-07 00:00:00.000000000 Z
11
+ date: 2019-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: bundler
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,34 @@ dependencies:
80
94
  - - ">="
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: fhwang-wisper-mongoid
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: mongoid
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
83
125
  - !ruby/object:Gem::Dependency
84
126
  name: rake
85
127
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +164,34 @@ dependencies:
122
164
  - - ">="
123
165
  - !ruby/object:Gem::Version
124
166
  version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: sqlite3
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'
181
+ - !ruby/object:Gem::Dependency
182
+ name: wisper-activerecord
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
125
195
  description: A mini-framework for intelligently publishing complex model changes to
126
196
  an external API.
127
197
  email:
@@ -151,6 +221,9 @@ files:
151
221
  - lib/sync_machine/ensure_publication/publication_history.rb
152
222
  - lib/sync_machine/ensure_publication_worker.rb
153
223
  - lib/sync_machine/find_subjects_worker.rb
224
+ - lib/sync_machine/orm_adapters.rb
225
+ - lib/sync_machine/orm_adapters/active_record_adapter.rb
226
+ - lib/sync_machine/orm_adapters/mongoid_adapter.rb
154
227
  - lib/sync_machine/version.rb
155
228
  - sync_machine.gemspec
156
229
  homepage: https://github.com/fhwang/sync_machine