diff --git a/ruby_event_store/lib/ruby_event_store/link_by_metadata.rb b/ruby_event_store/lib/ruby_event_store/link_by_metadata.rb index e516554700..dbd702834b 100644 --- a/ruby_event_store/lib/ruby_event_store/link_by_metadata.rb +++ b/ruby_event_store/lib/ruby_event_store/link_by_metadata.rb @@ -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 \ No newline at end of file diff --git a/ruby_event_store/spec/link_by_metadata_spec.rb b/ruby_event_store/spec/link_by_metadata_spec.rb index 0e27527423..d93d090c2d 100644 --- a/ruby_event_store/spec/link_by_metadata_spec.rb +++ b/ruby_event_store/spec/link_by_metadata_spec.rb @@ -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