event_sourcery 0.16.1 → 0.17.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 851b272af6920686f8033e9d948e99281bd13886
4
- data.tar.gz: 9f09be230953049d28e1e1686bb5a77f679105a9
3
+ metadata.gz: d676408ca476e668d70f08dd1c29ce1c93f25415
4
+ data.tar.gz: e34dfb803088dce3cff191f95b251af18470f659
5
5
  SHA512:
6
- metadata.gz: f8653075d18913fc0bfdb3c69c607e28a47f71cf5dd76d1204fec159ae931c67343b67a3b12ff588385e79e3a06f2f1515c19e673623e2915e7bea4f079c32ca
7
- data.tar.gz: 9f8562bb232db84fcf2d28973946385aacbbe98e712a396d2dd34cec0e7b0429943b530d6e5be3a671ed95b48c0920560055bcf3970281719d40907c41ff6a0d
6
+ metadata.gz: 6b9fa74a6699e135575a52e5edfe4f0c9cd32c9ee1ee6cc74ae32ca1a543f6090044d2aebc63c8ed9ee35481aeff9f5990d1d4911cf4cf1d15b3923abedbec14
7
+ data.tar.gz: e17b0c1cb636abfb81524d1197afe8bf4836f5ffdda50180bb1c7322c3a0e1ab66595d0e80ef1810b4d86496defe33c2edfe499367dec660939a4d74f13081f8
data/CHANGELOG.md CHANGED
@@ -5,7 +5,10 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/).
7
7
 
8
- ## [Unreleased]
8
+ ## [0.17.0] - 2018-03-22
9
+ ### Added
10
+ - Allow changing the event class using Event#with
11
+ - Allow upcasting events using custom event classes
9
12
 
10
13
  ## [0.16.1] - 2018-01-17
11
14
  - Fixed bug with Sequel gem expecting processes_event_types to be an Array
@@ -12,6 +12,31 @@ module EventSourcery
12
12
  end
13
13
  end
14
14
 
15
+ # Use this method to add "upcasting" to your events.
16
+ #
17
+ # To upcast your events override the `upcast` class method on your event.
18
+ # The `upcast` method will be passed the event allowing you to modify it
19
+ # before it is passed to your event processors.
20
+ #
21
+ # A good place to start is using the `Event#with` method that allows you to
22
+ # easily change attributes.
23
+ #
24
+ # @param Event
25
+ # @return Event
26
+ # @example
27
+ # Foo = Class.new(EventSourcery::Event) do
28
+ # def self.upcast(event)
29
+ # body = event.body
30
+ #
31
+ # body['bar'] ||= 'baz'
32
+ #
33
+ # event.with(body: body)
34
+ # end
35
+ # end
36
+ def self.upcast(event)
37
+ event
38
+ end
39
+
15
40
  attr_reader :id, :uuid, :aggregate_id, :type, :body, :version, :created_at, :correlation_id, :causation_id
16
41
 
17
42
  # @!attribute [r] id
@@ -104,8 +129,18 @@ module EventSourcery
104
129
  # # Of course, with can accept any number of event attributes:
105
130
  #
106
131
  # new_event = old_event.with(id: 42, version: 77, body: { 'attr' => 'value' })
107
- def with(**attributes)
108
- self.class.new(**to_h.merge!(attributes))
132
+ #
133
+ # # When using typed events you can also override the event class:
134
+ #
135
+ # new_event = old_event.with(event_class: ItemRemoved)
136
+ # new_event.type # => "item_removed"
137
+ # new_event.class # => ItemRemoved
138
+ def with(event_class: self.class, **attributes)
139
+ if self.class != Event && !attributes[:type].nil? && attributes[:type] != type
140
+ raise Error, 'When using typed events change the type by changing the event class.'
141
+ end
142
+
143
+ event_class.new(**to_h.merge!(attributes))
109
144
  end
110
145
 
111
146
  # returns a hash of the event attributes
@@ -8,12 +8,16 @@ module EventSourcery
8
8
  def build(event_data)
9
9
  type = event_data.fetch(:type)
10
10
  klass = event_type_serializer.deserialize(type)
11
- klass.new(event_data)
11
+ upcast(klass.new(event_data))
12
12
  end
13
13
 
14
14
  private
15
15
 
16
16
  attr_reader :event_type_serializer
17
+
18
+ def upcast(event)
19
+ event.class.upcast(event)
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -1,4 +1,4 @@
1
1
  module EventSourcery
2
2
  # Defines the version
3
- VERSION = '0.16.1'.freeze
3
+ VERSION = '0.17.0'.freeze
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_sourcery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.1
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Envato
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-17 00:00:00.000000000 Z
11
+ date: 2018-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  version: '0'
143
143
  requirements: []
144
144
  rubyforge_project:
145
- rubygems_version: 2.5.2
145
+ rubygems_version: 2.6.14
146
146
  signing_key:
147
147
  specification_version: 4
148
148
  summary: Event Sourcing Library