Class: MessageStore::EventStore::Put

Inherits:
Object
  • Object
show all
Includes:
Log::Dependency
Defined in:
lib/message_store/event_store/put.rb

Defined Under Namespace

Modules: Assertions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(session: nil) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/message_store/event_store/put.rb', line 10

def self.build(session: nil)
  session ||= Session.build

  instance = new
  ::EventStore::HTTP::Write.configure(instance, session: session)
  instance
end

.call(message_data, stream_name, expected_version: nil, session: nil) ⇒ Object



18
19
20
21
# File 'lib/message_store/event_store/put.rb', line 18

def self.call(message_data, stream_name, expected_version: nil, session: nil)
  instance = build(session: session)
  instance.(message_data, stream_name, expected_version: expected_version)
end

Instance Method Details

#call(messages, stream_name, expected_version: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/message_store/event_store/put.rb', line 23

def call(messages, stream_name, expected_version: nil)
  messages = Array(messages)

  expected_version = ExpectedVersion.canonize(expected_version)

  logger.trace { "Putting message data (Stream Name: #{stream_name}, Batch Size: #{messages.count}, Types: #{messages.map(&:type).inspect}, Expected Version: #{expected_version.inspect})" }

  messages.each do |message_data|
    message_data. = nil if message_data.&.empty?
  end

  begin
    location = write.(
      messages,
      stream_name,
      expected_version: expected_version
    )
  rescue ::EventStore::HTTP::Write::ExpectedVersionError => error
    raise ExpectedVersion::Error, error.message
  end

  *, position = location.path.split '/'

  logger.debug { "Put message data done (Stream Name: #{stream_name}, Batch Size: #{messages.count}, Types: #{messages.map(&:type).inspect}, Position: #{position}, Expected Version: #{expected_version.inspect})" }

  position.to_i
end