Skip to content

Instantly share code, notes, and snippets.

@karmajunkie
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karmajunkie/9441403 to your computer and use it in GitHub Desktop.
Save karmajunkie/9441403 to your computer and use it in GitHub Desktop.
Replay events
class ReplayTest
include Replay::EventSource
#define an event inline to the class
event SomethingHappened(name: String, pid: Integer)
apply SomethingHappened do |event|
@name = event.name
@pid = event.pid
end
apply SomethingElseHappened do |event|
@state = :happened_again
@pid = nil if event.pid == @pid
end
def do_something(valid_parameter = false)
#the command validates inputs
raise InvalidCommand.new("parameters were invalid") unless valid_parameter
#publish events
unless @pid
signal SomethingHappened.new(:name = "foo", :pid => 123)
else
signal SomethingElseHappened.new(:pid => 123)
end
end
end
#define an event outside the class
class ReplayTest::SomethingElseHappened
include Replay::Event
attribute :pid, Integer
end
#freehanding this, forgive the lack of organization
class MyEventStream
include Replay::StreamWithStorage
event_store MyEventStore
end
class MyEventStore
include Replay::EventStore
def self.load(new_obj, guid, options = {})
if options[:rebuild]
events = events_for_guid(guid)
else
#this is just the skeleton of this operation, needs work
obj = JSON.load(snapshot_json_for(guid))
obj.event_stream = new_obj.event_stream
events = events_for_guid(guid, :since => obj.version)
end
events = events_for_guid(guid)
events.each{|e| new_obj.apply(e)}
return new_obj
end
end
#there will probably be a convenience method for this. maybe.
baby_object = ReplayTest.new(MyEventStream)
#loading an event—rebuild defaults to false
root = MyEventStore.load(baby_object, guid, :rebuild => true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment