Skip to content

Commit

Permalink
RailsES::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 b07fea1 commit 40357a3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
6 changes: 6 additions & 0 deletions rails_event_store/lib/rails_event_store/link_by_metadata.rb
Expand Up @@ -18,4 +18,10 @@ def initialize(event_store: Rails.configuration.event_store, prefix: nil)
end
end

class LinkByEventType < RubyEventStore::LinkByEventType
def initialize(event_store: Rails.configuration.event_store, prefix: nil)
super(event_store: event_store, prefix: prefix)
end
end

end
23 changes: 23 additions & 0 deletions rails_event_store/spec/link_by_metadata_spec.rb
Expand Up @@ -89,4 +89,27 @@ module RailsEventStore
end
end

RSpec.describe LinkByEventType do
let(:event_store) { RailsEventStore::Client.new }
let(:event) { OrderCreated.new }

before do
rails = double("Rails", configuration: Rails::Application::Configuration.new)
stub_const("Rails", rails)
Rails.configuration.event_store = event_store
end

specify "default prefix" do
event_store.subscribe_to_all_events(LinkByEventType.new)
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(prefix: "e-"))
event_store.publish(event)
expect(event_store.read.stream("e-OrderCreated").each.to_a).to eq([event])
end
end

end
13 changes: 3 additions & 10 deletions ruby_event_store/lib/ruby_event_store/link_by_metadata.rb
@@ -1,11 +1,7 @@
module RubyEventStore
class LinkByMetadata

def initialize(
event_store:,
key:,
prefix: nil
)
def initialize(event_store:, key:, prefix: nil)
@event_store = event_store
@key = key
@prefix = prefix || ["$by", key, nil].join("_")
Expand Down Expand Up @@ -43,12 +39,9 @@ def initialize(event_store:, prefix: nil)
end

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

def call(event)
Expand Down
6 changes: 3 additions & 3 deletions ruby_event_store/spec/link_by_metadata_spec.rb
Expand Up @@ -60,7 +60,7 @@ module RubyEventStore
expect(event_store.read.stream("sweet+Paris").each.to_a).to eq([ev])
end

specify "array of ids" do
specify "explicitly passes array of ids instead of a single id" do
event_store.subscribe_to_all_events(LinkByMetadata.new(event_store: event_store, key: :city))
expect(event_store).to receive(:link_to_stream).with(instance_of(Array), any_args)
event_store.publish(ev = OrderCreated.new(metadata:{
Expand Down Expand Up @@ -136,12 +136,12 @@ module RubyEventStore
expect(event_store.read.stream("e-OrderCreated").each.to_a).to eq([event])
end

specify "array of ids" do
specify "explicitly passes array of ids instead of a single id" 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 40357a3

Please sign in to comment.