pubsub_on_rails 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: 610b5e8d1415af6aa00766ef49444e4a3889aa6e1d79c0da2a74a58a09726d0c
4
- data.tar.gz: 57ac3717989db60def406d8f6a85aa4139bdffef3ee3d05b5d7dcae70823f6dc
3
+ metadata.gz: 665f27ae9c49ccdd0f1b87c8f5b788a558ebee2a22848f3ab04e272528df7e17
4
+ data.tar.gz: c7107b6d788c1ac9a0069b104b6e4db211a692ef4a6345c613a62fcbcd306859
5
5
  SHA512:
6
- metadata.gz: 43d05d52a2f8dfbc52c9cca47a731acb8c98d2b36d2d490aef3f08cf9a902bf29e9dd394f441fcd914d74fcf53230ccaaf011c0ef64e5c1be546947e6a5ca7d1
7
- data.tar.gz: 8c4fc34fc4404ff3476bef408760b10af12113720aadad1428566ab34e8350d902375022621148bf7e3016ae1cdc8ee682cdeacece0f95438d4a177a400bceec
6
+ metadata.gz: 4a0f58bbe6dd2ff109934781c580c3ac5b1a711db532a4f0a61f2a005b3b6fee0d4c89391cf53f6d33c0dc65972ccddb34843e551121053641781499fb7d0d7e
7
+ data.tar.gz: c1895dffa35a785cd78ff6ab62e2e5c5fb10c4937d96b81f7d44cfe63908d101eb30073054ef1dfd17829ebfa72a47f9ee8275e1df813c89ad1d40a7a29c6fb2
data/README.md CHANGED
@@ -16,8 +16,50 @@ This is especially true for applications covering many side effects, integration
16
16
  ```ruby
17
17
  # Gemfile
18
18
 
19
+ gem 'pubsub_on_rails', '~> 1.1.0'
20
+
21
+ # config/initializers/pub_sub.rb
22
+
23
+ require 'pub_sub/subscriptions_list'
24
+
25
+ Rails.configuration.to_prepare do
26
+ Rails.configuration.event_store = event_store = RailsEventStore::Client.new(
27
+ repository: RailsEventStoreActiveRecord::EventRepository.new(serializer: RubyEventStore::NULL)
28
+ )
29
+
30
+ PubSub::SubscriptionsList.config_path =
31
+ Rails.root.join('config/subscriptions.yml')
32
+ PubSub::SubscriptionsList.load!(event_store)
33
+ end
34
+ ```
35
+
36
+ ## Migrating from 0.0.7 to 1.0.0
37
+
38
+ 1. Update gem to version `1.0.0`
39
+
40
+ ```ruby
41
+ # Gemfile
42
+
19
43
  gem 'pubsub_on_rails', '~> 1.0.0'
44
+ ```
45
+
46
+ 2. Run Rails Event Store migrations
47
+
48
+ **MySQL**
49
+ ```
50
+ bin/rails generate rails_event_store_active_record:migration
51
+ bin/rails db:migrate
52
+ ```
20
53
 
54
+ **PostgreSQL**
55
+ ```
56
+ bin/rails generate rails_event_store_active_record:migration --data-type=jsonb
57
+ bin/rails db:migrate
58
+ ```
59
+
60
+ 3. Update initializer to use Rails Event Store Client
61
+
62
+ ```ruby
21
63
  # config/initializers/pub_sub.rb
22
64
 
23
65
  require 'pub_sub/subscriptions_list'
@@ -33,6 +75,31 @@ Rails.configuration.to_prepare do
33
75
  end
34
76
  ```
35
77
 
78
+ 4. Override `EventWorker` or override `EventHandlerBuilder` if needed
79
+
80
+ For example when you want to have different workers for different events:
81
+
82
+ ```ruby
83
+ # config/initializers/pub_sub.rb
84
+
85
+ PubSub::EventHandlerBuilder.class_eval do
86
+ def call(event)
87
+ if async?
88
+ if class_name.to_s.include?('MyType')
89
+ SingleThreadEventWorker.perform_in(2.seconds, class_name.to_s, event.event_id)
90
+ else
91
+ EventWorker.perform_in(2.seconds, class_name.to_s, event.event_id)
92
+ end
93
+ else
94
+ class_name.new(event).call!
95
+ end
96
+ end
97
+ end
98
+ ```
99
+
100
+ 5. Add event objects for Rails Event Store streams. Check [Event](README.md#Event) section.
101
+ 6. Update test cases to use new matchers. Check [Testing](README.md#Testing) section.
102
+
36
103
  ## Entities
37
104
 
38
105
  There are five entities that are core to PubSub on Rails: domains, events, event publishers, event handlers and subscriptions.
@@ -69,13 +136,17 @@ Event example:
69
136
  ```ruby
70
137
  # app/events/ordering/order_created_event.rb
71
138
 
72
- module Ordering
73
- class OrderCreatedEvent < PubSub::DomainEvent
74
- attribute :order_id, Types::Strict::Integer
75
- attribute :customer_id, Types::Strict::Integer
76
- attribute :line_items, Types::Strict::Array
77
- attribute :total_amount, Types::Strict::Float
78
- attribute :comment, Types::Strict::String.optional
139
+ module PubSub
140
+ module Ordering
141
+ class OrderCreatedEvent < PubSub::EventWithType
142
+ schema do
143
+ attribute :order_id, Types::Strict::Integer
144
+ attribute :customer_id, Types::Strict::Integer
145
+ attribute :line_items, Types::Strict::Array
146
+ attribute :total_amount, Types::Strict::Float
147
+ attribute :comment, Types::Strict::String.optional
148
+ end
149
+ end
79
150
  end
80
151
  end
81
152
  ```
@@ -351,15 +422,19 @@ logging:
351
422
 
352
423
  ## Payload verification
353
424
 
354
- Every time event is emitted, its payload is supplied to corresponding event class and is verified.
425
+ Every time event is emitted, its payload is supplied to corresponding `Dry::Struct` event class and is verified.
355
426
  This ensures that whenever we emit event we can be sure its payload is matching specification.
356
427
 
357
428
  Example:
358
429
 
359
430
  ```ruby
360
- module Accounts
361
- class PersonCreatedEvent < PubSub::DomainEvent
362
- attribute :person_id, Types::Strict::Integer
431
+ module PubSub
432
+ module Accounts
433
+ class PersonCreatedEvent < PubSub::EventWithType
434
+ schema do
435
+ attribute :person_id, Types::Strict::Integer
436
+ end
437
+ end
363
438
  end
364
439
  end
365
440
  ```
@@ -48,22 +48,18 @@ module PubSub
48
48
  end
49
49
  end
50
50
 
51
- def dry_struct_event_class
52
- @dry_struct_event_class ||= event_class.name.remove('PubSub').constantize
53
- end
54
-
55
51
  # rubocop:disable Metrics/MethodLength
56
52
  def full_payload
57
- dry_struct_event_class.attribute_names.each_with_object({}) do |attribute_name, result|
53
+ attribute_names.each_with_object({}) do |attribute_name, result|
58
54
  result[attribute_name] = PayloadAttribute.new(
59
55
  attribute_name, explicit_payload, context
60
56
  ).get
61
57
  rescue PayloadAttribute::CannotEvaluate => e
62
- next if dry_struct_event_class.schema.key(attribute_name).default?
58
+ next if schema.key(attribute_name).default?
63
59
 
64
60
  raise(
65
61
  EventPayloadArgumentMissing,
66
- "Event [#{dry_struct_event_class.name}] expects [#{attribute_name}] " \
62
+ "Event [#{event_class.name}] expects [#{attribute_name}] " \
67
63
  "payload attribute to be either exposed as [#{e.message}] method in emitting object " \
68
64
  'or provided as argument'
69
65
  )
@@ -71,6 +67,14 @@ module PubSub
71
67
  end
72
68
  # rubocop:enable Metrics/MethodLength
73
69
 
70
+ def attribute_names
71
+ (abstract_event_class || event_class.instance_variable_get(:@schema_validator)).attribute_names
72
+ end
73
+
74
+ def schema
75
+ (abstract_event_class || event_class.instance_variable_get(:@schema_validator)).schema
76
+ end
77
+
74
78
  def event_store
75
79
  Rails.configuration.event_store
76
80
  end
@@ -4,7 +4,7 @@ module PubSub
4
4
  super(
5
5
  event_id:,
6
6
  metadata:,
7
- data: self.class.name.remove('PubSub').constantize.new(
7
+ data: self.class.instance_variable_get(:@schema_validator).new(
8
8
  data.deep_symbolize_keys
9
9
  ).attributes
10
10
  )
@@ -13,5 +13,9 @@ module PubSub
13
13
  def stream_names
14
14
  []
15
15
  end
16
+
17
+ def self.schema(&block)
18
+ instance_variable_set(:@schema_validator, Class.new(Dry::Struct, &block))
19
+ end
16
20
  end
17
21
  end
@@ -1,3 +1,3 @@
1
1
  module PubSub
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pubsub_on_rails
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
  - Stevo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-02 00:00:00.000000000 Z
11
+ date: 2023-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-struct
@@ -96,6 +96,7 @@ files:
96
96
  - lib/pub_sub/version.rb
97
97
  - lib/pubsub_on_rails.rb
98
98
  - pubsub_on_rails-0.0.7.gem
99
+ - pubsub_on_rails-1.0.0.gem
99
100
  - pubsub_on_rails.gemspec
100
101
  homepage: https://github.com/Selleo/pubsub_on_rails
101
102
  licenses: