-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upcasting records to newer types as transformation in pipeline
Usage: class Mapper < PipelineMapper def initialize(upcast_map: {}) super(Pipeline.new( Transformation::Upcast.new(upcast_map), )) end end RubyEventStore::Client.new( mapper: Mapper.new(upcast_map: { 'OldEventType' => lambda { |record| Record.new( event_type: 'NewEventType', data: ..., metadata: record.metadata, timestamp: record.timestamp, valid_at: record.valid_at, event_id: record.event_id ) } }), repository: ... )
- Loading branch information
1 parent
e272342
commit d57a9e9
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
ruby_event_store/lib/ruby_event_store/mappers/transformation/upcast.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module RubyEventStore | ||
module Mappers | ||
module Transformation | ||
class Upcast | ||
class RecordUpcaster | ||
def initialize(upcast_map) | ||
@upcast_map = upcast_map | ||
end | ||
|
||
def call(record) | ||
identity = lambda { |r| r } | ||
new_record = @upcast_map.fetch(record.event_type, identity)[record] | ||
if new_record == record | ||
record | ||
else | ||
call(new_record) | ||
end | ||
end | ||
end | ||
|
||
def initialize(upcast_map) | ||
@record_upcaster = RecordUpcaster.new(upcast_map) | ||
end | ||
|
||
def dump(record) | ||
record | ||
end | ||
|
||
def load(record) | ||
@record_upcaster.call(record) | ||
end | ||
end | ||
end | ||
end | ||
end |
57 changes: 57 additions & 0 deletions
57
ruby_event_store/spec/mappers/transformation/upcast_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'spec_helper' | ||
|
||
module RubyEventStore | ||
module Mappers | ||
module Transformation | ||
RSpec.describe Upcast do | ||
let(:time) { Time.now.utc } | ||
let(:uuid) { SecureRandom.uuid } | ||
|
||
let(:record_v1) do | ||
Record.new( | ||
event_id: uuid, | ||
metadata: {some: 'meta'}, | ||
data: [{some: 'value'}], | ||
event_type: 'record.v1', | ||
timestamp: time, | ||
valid_at: time, | ||
) | ||
end | ||
let(:record_v2) do | ||
Record.new( | ||
event_id: uuid, | ||
metadata: {some: 'meta'}, | ||
data: {as_hash: [{some: 'value'}]}, | ||
event_type: 'record.v2', | ||
timestamp: time, | ||
valid_at: time, | ||
) | ||
end | ||
let(:upcast_map) do | ||
{ | ||
'record.v1' => lambda do |r| | ||
Record.new( | ||
event_id: r.event_id, | ||
metadata: r.metadata, | ||
timestamp: r.timestamp, | ||
valid_at: r.valid_at, | ||
event_type: 'record.v2', | ||
data: {as_hash: r.data} | ||
) | ||
end | ||
} | ||
end | ||
|
||
specify "#dump" do | ||
expect(Upcast.new(upcast_map).dump(record_v1)).to eq(record_v1) | ||
expect(Upcast.new(upcast_map).dump(record_v2)).to eq(record_v2) | ||
end | ||
|
||
specify "#load" do | ||
expect(Upcast.new(upcast_map).load(record_v1)).to eq(record_v2) | ||
expect(Upcast.new(upcast_map).load(record_v2)).to eq(record_v2) | ||
end | ||
end | ||
end | ||
end | ||
end |