Class: Messaging::Write::Substitute::Write

Inherits:
Object
  • Object
show all
Includes:
Messaging::Write
Defined in:
lib/messaging/write/substitute.rb

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Messaging::Write

included, #initial, #message_data_batch, register_telemetry_sink, #reply

Instance Attribute Details

#raise_expected_version_errorObject



18
19
20
# File 'lib/messaging/write/substitute.rb', line 18

def raise_expected_version_error
  @raise_expected_version_error ||= false
end

#sinkObject

Returns the value of attribute sink.



16
17
18
# File 'lib/messaging/write/substitute.rb', line 16

def sink
  @sink
end

Class Method Details

.build(session: nil) ⇒ Object



23
24
25
26
27
# File 'lib/messaging/write/substitute.rb', line 23

def self.build(session: nil)
  new.tap do |instance|
    ::Telemetry.configure instance
  end
end

Instance Method Details

#call(*args, **keyword_args) ⇒ Object Also known as: write

Raises:

  • (MessageStore::ExpectedVersion::Error)


29
30
31
32
# File 'lib/messaging/write/substitute.rb', line 29

def call(*args, **keyword_args)
  raise MessageStore::ExpectedVersion::Error if raise_expected_version_error
  super(*args, **keyword_args)
end

#message_replies(&blk) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/messaging/write/substitute.rb', line 138

def message_replies(&blk)
  if blk.nil?
    return sink.replied_records.map { |record| record.data.message }
  end

  sink.replied_records.select do |record|
    blk.call(record.data.message, record.data.stream_name)
  end.map { |record| record.data.message }
end

#message_writes(&blk) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/messaging/write/substitute.rb', line 117

def message_writes(&blk)
  if blk.nil?
    return sink.written_records.map { |record| record.data.message }
  end

  sink.written_records.select do |record|
    blk.call(record.data.message, record.data.stream_name, record.data.expected_version, record.data.reply_stream_name)
  end.map { |record| record.data.message }
end

#one_message_reply(&blk) ⇒ Object Also known as: one_reply



148
149
150
151
152
153
154
155
156
# File 'lib/messaging/write/substitute.rb', line 148

def one_message_reply(&blk)
  messages = message_replies(&blk)

  if messages.length > 1
    raise Error, "More than one matching message reply was written"
  end

  messages.first
end

#one_message_write(&blk) ⇒ Object Also known as: one_message



127
128
129
130
131
132
133
134
135
# File 'lib/messaging/write/substitute.rb', line 127

def one_message_write(&blk)
  messages = message_writes(&blk)

  if messages.length > 1
    raise Error, "More than one matching message was written"
  end

  messages.first
end

#raise_expected_version_error!Object



35
36
37
38
# File 'lib/messaging/write/substitute.rb', line 35

def raise_expected_version_error!
  self.raise_expected_version_error = true
  nil
end

#replied?(message = nil, &blk) ⇒ Boolean

TODO need to make same changes as made to written?

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/messaging/write/substitute.rb', line 89

def replied?(message=nil, &blk)
  if message.nil?
    if blk.nil?
      return sink.recorded_replied?
    end

    return sink.recorded_replied? do |record|
      blk.call(record.data.message, record.data.stream_name)
    end
  end

  written = sink.recorded_replied? do |record|
    record.data.message == message
  end

  if !written
    return false
  end

  if blk.nil?
    return true
  end

  sink.recorded_replied? do |record|
    blk.call(record.data.stream_name)
  end
end

#replies(&blk) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/messaging/write/substitute.rb', line 78

def replies(&blk)
  if blk.nil?
    return sink.replied_records
  end

  sink.replied_records.select do |record|
    blk.call(record.data.message, record.data.stream_name)
  end
end

#writes(&blk) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/messaging/write/substitute.rb', line 40

def writes(&blk)
  if blk.nil?
    return sink.written_records
  end

  sink.written_records.select do |record|
    blk.call(record.data.message, record.data.stream_name, record.data.expected_version, record.data.reply_stream_name)
  end
end

#written?(message = nil, &blk) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/messaging/write/substitute.rb', line 50

def written?(message=nil, &blk)
  if message.nil?
    if blk.nil?
      return sink.recorded_written?
    end

    return sink.recorded_written? do |record|
      blk.call(record.data.message, record.data.stream_name, record.data.expected_version, record.data.reply_stream_name)
    end
  end

  written = sink.recorded_written? do |record|
    record.data.message == message
  end

  if !written
    return false
  end

  if blk.nil?
    return true
  end

  return sink.recorded_written? do |record|
    blk.call(record.data.stream_name, record.data.expected_version, record.data.reply_stream_name)
  end
end