Skip to content

Commit

Permalink
LinkByEventType
Browse files Browse the repository at this point in the history
Issue: #72 #221 #346
  • Loading branch information
paneq committed Jul 2, 2018
1 parent 661ebe2 commit b07fea1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ruby_event_store/lib/ruby_event_store/link_by_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,22 @@ def initialize(event_store:, prefix: nil)
)
end
end

class LinkByEventType
def initialize(
event_store:,
prefix: "$by_type_"
)
@event_store = event_store
@prefix = prefix
end

def call(event)
@event_store.link_to_stream(
[event.message_id],
stream_name: "#{@prefix}#{event.type}"
)
end
end

end
26 changes: 26 additions & 0 deletions ruby_event_store/spec/link_by_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,31 @@ module RubyEventStore
expect(event_store.read.stream("c-CAU").each.to_a).to eq([event])
end
end

RSpec.describe LinkByEventType do
let(:event_store) do
RubyEventStore::Client.new(repository: InMemoryRepository.new)
end
let(:event) { OrderCreated.new }

specify "default prefix" do
event_store.subscribe_to_all_events(LinkByEventType.new(event_store: event_store))
event_store.publish(event)
expect(event_store.read.stream("$by_type_OrderCreated").each.to_a).to eq([event])
end

specify "custom prefix" do
event_store.subscribe_to_all_events(LinkByEventType.new(event_store: event_store, prefix: "e-"))
event_store.publish(event)
expect(event_store.read.stream("e-OrderCreated").each.to_a).to eq([event])
end

specify "array of ids" do
event_store.subscribe_to_all_events(LinkByEventType.new(event_store: event_store))
expect(event_store).to receive(:link_to_stream).with(instance_of(Array), any_args)
event_store.publish(ev = OrderCreated.new())
end
end

end

0 comments on commit b07fea1

Please sign in to comment.