Skip to content

Instantly share code, notes, and snippets.

@kylewelsby
Created December 1, 2016 18:47
Show Gist options
  • Save kylewelsby/60d3560c00c964f7ee69ccffe70a9a6b to your computer and use it in GitHub Desktop.
Save kylewelsby/60d3560c00c964f7ee69ccffe70a9a6b to your computer and use it in GitHub Desktop.
module Sequares
module Store
class Redis < Base
attr_accessor :connection
def initialize(connection=::Redis.new)
@connection = connection
end
def filter_events(*klasses)
entity_event_pairs = {}
connection.keys.each do |key|
next unless connection.type(key) == "list"
events = connection.lrange(key, 0, -1).to_a.collect do |hist|
::Marshal.load(hist)
end
events.each do |event|
next unless klass_in_klasses?(event, klasses)
key_split = key.split("|")
klass = ActiveSupport::Inflector.classify(key_split.first).constantize.new(key_split.last)
entity_event_pairs[klass] = event
end
end
entity_event_pairs
end
end
end
end
require "spec_helper"
describe Sequares::Store::Redis do
before :each do
Sequares.configure do |config|
config.store = Sequares::Store::Redis.new
end
end
it_behaves_like 'store'
end
RSpec.shared_examples "store" do
before :each do
EventFoo = Sequares::Event.new(:name)
module EventNS
EventFoo = Sequares::Event.new(:name)
end
class ::EntityFoo < Sequares::Entity
end
end
after :each do
Object.send(:remove_const, :EventFoo)
Object.send(:remove_const, :EntityFoo)
Object.send(:remove_const, :EventNS)
end
let(:event) { EventFoo.new(name: "bar") }
let(:ns_event) { EventNS::EventFoo.new(name: "bar") }
let(:entity) { EntityFoo.load("1") }
describe "#filter_events" do
before :each do
entity.history << event
entity.history << ns_event
subject.save_history_for_aggregate(entity)
end
it "queries the history for given events" do
events_pair = subject.filter_events(EventFoo)
# assert
expect(events_pair.size).to be 1
events_pair.each do |ent, ev|
expect(ent).to be_a EntityFoo
expect(ev).to eql event
end
end
it "queries the history by namespace" do
subject.save_history_for_aggregate(entity)
events_pair = subject.filter_events(EventNS)
# assert
expect(events_pair.length).to be 1
events_pair.each do |ent, ev|
expect(ent).to be_a EntityFoo
expect(ev).to eql ns_event
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment