sync_machine 1.0.0 → 1.1.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: 590006776b984bcede930cae17327932ac2e5c2e6b0d4bfab23e3f3742d0ecfa
4
- data.tar.gz: 7e04325fae0528ba6fe48d413827c694aed86f13d69460dfb96782f38f25bc03
3
+ metadata.gz: a87f8dc11ee9675808f703b22cbe674c17d1ed0e05896eb2b061c2a85062f5e9
4
+ data.tar.gz: f21649d91d260a5c4499544881e14e615d9e657e6f776ef331e408021117c600
5
5
  SHA512:
6
- metadata.gz: 6e236957b67a82d29556fa8f57c6fe00f49e0cdb0f4e9c081eb29c99a0123de65ff4aae5f3c6ca308e48eb635c957cda14ce13dc2e40e2f064f6ee17e6395fbb
7
- data.tar.gz: d49cca0da353472f8fc561d9a612df8d63a0cd745aec41cf183bc173051719c65eaa2d63d6ab0ae63b7007169c2be6b7abce3c3fd72a3415324791d89eb55f07
6
+ metadata.gz: 9ed147b8be624b64816c1e9837265a05f2aa4322a82e7bfa75873199d24ab63a27ed133fbc6d3bcb51c625479a31a698b73a0eae43c1a09bfd222067c8d368b7
7
+ data.tar.gz: 4634324884852210076115e04707590a2dda67d083d968fc5c45eb793370b8ff458c442e697ebdd5f4d14b3e8a03d3eaa1d6643e5a8a8ee8cae721fdbf3febe7
data/.rubocop.yml CHANGED
@@ -1,3 +1,10 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'lib/rails/generators/active_record/templates/*.rb'
4
+ - 'lib/rails/generators/mongoid/templates/*.rb'
5
+ - 'lib/rails/generators/rspec/templates/*.rb'
6
+ - 'lib/rails/generators/templates/*.rb'
7
+
1
8
  Layout/ExtraSpacing:
2
9
  Enabled: false
3
10
 
@@ -0,0 +1,27 @@
1
+ require "rails/generators"
2
+
3
+ module ActiveRecord
4
+ # Generate SyncMachine files for ActiveRecord.
5
+ class SyncMachineGenerator < Rails::Generators::NamedBase
6
+ class_option :subject, type: :string
7
+ source_root File.expand_path('templates', __dir__)
8
+
9
+ def create_payload_file
10
+ template "payload.rb", "app/models/#{file_path}/payload.rb"
11
+ end
12
+
13
+ def create_payload_migration
14
+ generate(
15
+ "migration",
16
+ "create_#{singular_name}_payloads body:text generated_at:datetime subject_id:integer"
17
+ )
18
+ end
19
+
20
+ private
21
+
22
+ def subject
23
+ options['subject'] || file_name.split(/_to_/).first
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,10 @@
1
+ module <%= class_name %>
2
+ class Payload < ActiveRecord::Base
3
+ self.table_name = "<%= singular_name %>_payloads"
4
+
5
+ serialize :body
6
+
7
+ validates :generated_at, presence: true
8
+ validates :subject_id, presence: true, uniqueness: true
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ require "rails/generators"
2
+
3
+ module Mongoid
4
+ # Generate SyncMachine files for Mongoid.
5
+ class SyncMachineGenerator < Rails::Generators::NamedBase
6
+ class_option :subject, type: :string
7
+ source_root File.expand_path('templates', __dir__)
8
+
9
+ def create_payload_file
10
+ template "payload.rb", "app/models/#{file_path}/payload.rb"
11
+ end
12
+
13
+ private
14
+
15
+ def subject
16
+ options['subject'] || file_name.split(/_to_/).first
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ module <%= class_name %>
2
+ class Payload
3
+ include ::Mongoid::Document
4
+
5
+ field :body, type: Hash
6
+ field :generated_at, type: Time
7
+ field :subject_id, type: String
8
+
9
+ validates :generated_at, presence: true
10
+ validates :subject_id, presence: true, uniqueness: true
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ require "rails/generators"
2
+
3
+ module Rspec
4
+ # Generate SyncMachine files for RSpec.
5
+ class SyncMachineGenerator < ::Rails::Generators::NamedBase
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ def create_service_spec_file
9
+ template "service_spec.rb", "spec/services/#{file_path}_spec.rb"
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,18 @@
1
+ require 'rails_helper'
2
+
3
+ describe <%= class_name %> do
4
+ before do
5
+ SyncMachine.eager_load(<%= class_name %>)
6
+ clear_workers
7
+ end
8
+
9
+ def clear_workers
10
+ <%= class_name %>::FindSubjectsWorker.clear
11
+ <%= class_name %>::EnsurePublicationWorker.clear
12
+ end
13
+
14
+ def drain_workers
15
+ <%= class_name %>::FindSubjectsWorker.drain
16
+ <%= class_name %>::EnsurePublicationWorker.drain
17
+ end
18
+ end
@@ -0,0 +1,48 @@
1
+ require "rails/generators"
2
+
3
+ # Generate SyncMachine files.
4
+ class SyncMachineGenerator < Rails::Generators::NamedBase
5
+ class_option :subject, type: :string
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ hook_for :orm
9
+ hook_for :test_framework
10
+
11
+ def create_sync_file
12
+ template "sync.rb", "app/services/#{file_path}.rb"
13
+ end
14
+
15
+ def create_find_subjects_worker_file
16
+ template(
17
+ "find_subjects_worker.rb",
18
+ "app/workers/#{file_path}/find_subjects_worker.rb"
19
+ )
20
+ end
21
+
22
+ def create_ensure_publication_worker_file
23
+ template(
24
+ "ensure_publication_worker.rb",
25
+ "app/workers/#{file_path}/ensure_publication_worker.rb"
26
+ )
27
+ end
28
+
29
+ def create_change_listener_file
30
+ template(
31
+ "change_listener.rb", "app/services/#{file_path}/change_listener.rb"
32
+ )
33
+ end
34
+
35
+ def append_subscribe_to_initializer
36
+ initializer_path = "config/initializers/sync_machines.rb"
37
+ create_file(initializer_path, "") unless File.exist?(initializer_path)
38
+ append_to_file(
39
+ initializer_path, "#{class_name}::ChangeListener.subscribe\n"
40
+ )
41
+ end
42
+
43
+ private
44
+
45
+ def subject
46
+ options['subject'] || file_name.split(/_to_/).first
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ module <%= class_name %>
2
+ class ChangeListener < SyncMachine::ChangeListener
3
+ listen_to_models :<%= subject %>
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ module <%= class_name %>
2
+ class EnsurePublicationWorker < SyncMachine::EnsurePublicationWorker
3
+ check_publishable do |<%= subject %>|
4
+ # Return a boolean value indicating whether the <%= subject %>
5
+ # should be published at all.
6
+ end
7
+
8
+ build do |<%= subject %>|
9
+ # Generate and return a payload to send to the external service.
10
+ end
11
+
12
+ publish do |<%= subject %>, payload|
13
+ # Send the payload to the external service.
14
+ end
15
+
16
+ after_publish do |<%= subject %>|
17
+ # Execute any actions after a successful publish. This step is optional
18
+ # and can be deleted entirely.
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,5 @@
1
+ module <%= class_name %>
2
+ class FindSubjectsWorker < SyncMachine::FindSubjectsWorker
3
+ subject_ids_from_<%= subject %>
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ module <%= class_name %>
2
+ extend SyncMachine
3
+
4
+ subject :<%= subject %>
5
+ end
6
+
@@ -1,3 +1,3 @@
1
1
  module SyncMachine
2
- VERSION = "1.0.0".freeze
2
+ VERSION = "1.1.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sync_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francis Hwang
@@ -214,6 +214,17 @@ files:
214
214
  - Rakefile
215
215
  - bin/console
216
216
  - bin/setup
217
+ - lib/rails/generators/active_record/sync_machine_generator.rb
218
+ - lib/rails/generators/active_record/templates/payload.rb
219
+ - lib/rails/generators/mongoid/sync_machine_generator.rb
220
+ - lib/rails/generators/mongoid/templates/payload.rb
221
+ - lib/rails/generators/rspec/sync_machine_generator.rb
222
+ - lib/rails/generators/rspec/templates/service_spec.rb
223
+ - lib/rails/generators/sync_machine_generator.rb
224
+ - lib/rails/generators/templates/change_listener.rb
225
+ - lib/rails/generators/templates/ensure_publication_worker.rb
226
+ - lib/rails/generators/templates/find_subjects_worker.rb
227
+ - lib/rails/generators/templates/sync.rb
217
228
  - lib/sync_machine.rb
218
229
  - lib/sync_machine/change_listener.rb
219
230
  - lib/sync_machine/ensure_publication.rb