Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An ID Reservation component
module IdentificationComponent
module Messages
module Events
class Identified
include Messaging::Message
attribute :source_id, String
attribute :common_id, String
end
end
end
end
module IdentificationComponent
class Identify
include Log::Dependency
include Messaging::StreamName
include Dependency
include Configure
include Messages::Events
dependency :write, Messaging::Postgres::Write
dependency :get_last, MessageStore::Postgres::Get::Last
dependency :identifier, Identifier::UUID::Random
category :identification
configure :identify
def self.build(session: nil, category: nil)
instance = new
instance.configure(session: session, category: category)
instance
end
def configure(session: nil, category: nil)
Messaging::Postgres::Write.configure(self, session: session)
MessageStore::Postgres::Get::Last.configure(self, session: session)
Identifier::UUID::Random.configure(self)
self.category = category unless category.nil?
end
def call(source_id, common_id: nil, &block)
stream_name = stream_name(source_id)
Try.(MessageStore::ExpectedVersion::Error) do
common_id ||= identifier.get
reservation = Identified.build(
source_id: source_id,
common_id: common_id
)
block.call(reservation) unless block.nil?
write.initial(reservation, stream_name)
return common_id
end
event_data = get_last.(stream_name)
event_data.data[:common_id]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.