An ID Reservation component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module IdentificationComponent | |
module Messages | |
module Events | |
class Identified | |
include Messaging::Message | |
attribute :source_id, String | |
attribute :common_id, String | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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