Class: Sourced::Backends::TestBackend::State
- Inherits:
-
Object
- Object
- Sourced::Backends::TestBackend::State
- Defined in:
- lib/sourced/backends/test_backend.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#events_by_correlation_id ⇒ Object
readonly
Returns the value of attribute events_by_correlation_id.
-
#events_by_stream_id ⇒ Object
readonly
Returns the value of attribute events_by_stream_id.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#stream_id_seq_index ⇒ Object
readonly
Returns the value of attribute stream_id_seq_index.
Instance Method Summary collapse
- #copy ⇒ Object
-
#initialize(events: [], commands: [], groups: Hash.new { |h, k| h[k] = Group.new(k, self) }, events_by_correlation_id: Hash.new { |h, k| h[k] = [] }, events_by_stream_id: Hash.new { |h, k| h[k] = [] }, stream_id_seq_index: {}) ⇒ State
constructor
A new instance of State.
- #next_command(&reserve) ⇒ Object
- #schedule_commands(commands) ⇒ Object
Constructor Details
#initialize(events: [], commands: [], groups: Hash.new { |h, k| h[k] = Group.new(k, self) }, events_by_correlation_id: Hash.new { |h, k| h[k] = [] }, events_by_stream_id: Hash.new { |h, k| h[k] = [] }, stream_id_seq_index: {}) ⇒ State
Returns a new instance of State.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/sourced/backends/test_backend.rb', line 103 def initialize( events: [], commands: [], groups: Hash.new { |h, k| h[k] = Group.new(k, self) }, events_by_correlation_id: Hash.new { |h, k| h[k] = [] }, events_by_stream_id: Hash.new { |h, k| h[k] = [] }, stream_id_seq_index: {} ) @events = events @groups = groups @events_by_correlation_id = events_by_correlation_id @commands = commands @command_locks = {} @events_by_stream_id = events_by_stream_id @stream_id_seq_index = stream_id_seq_index end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
101 102 103 |
# File 'lib/sourced/backends/test_backend.rb', line 101 def commands @commands end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
101 102 103 |
# File 'lib/sourced/backends/test_backend.rb', line 101 def events @events end |
#events_by_correlation_id ⇒ Object (readonly)
Returns the value of attribute events_by_correlation_id.
101 102 103 |
# File 'lib/sourced/backends/test_backend.rb', line 101 def events_by_correlation_id @events_by_correlation_id end |
#events_by_stream_id ⇒ Object (readonly)
Returns the value of attribute events_by_stream_id.
101 102 103 |
# File 'lib/sourced/backends/test_backend.rb', line 101 def events_by_stream_id @events_by_stream_id end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
101 102 103 |
# File 'lib/sourced/backends/test_backend.rb', line 101 def groups @groups end |
#stream_id_seq_index ⇒ Object (readonly)
Returns the value of attribute stream_id_seq_index.
101 102 103 |
# File 'lib/sourced/backends/test_backend.rb', line 101 def stream_id_seq_index @stream_id_seq_index end |
Instance Method Details
#copy ⇒ Object
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/sourced/backends/test_backend.rb', line 149 def copy self.class.new( events: events.dup, commands: commands.dup, groups: deep_dup(groups), events_by_correlation_id: deep_dup(events_by_correlation_id), events_by_stream_id: deep_dup(events_by_stream_id), stream_id_seq_index: deep_dup(stream_id_seq_index) ) end |
#next_command(&reserve) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/sourced/backends/test_backend.rb', line 125 def next_command(&reserve) now = Time.now.utc if block_given? return nil if @commands.empty? idx = @commands.index do |c| !@command_locks[c.stream_id] && c.created_at <= now end return nil unless idx cmd = @commands[idx] @command_locks[cmd.stream_id] = true begin yield cmd @commands.delete_at(idx) ensure @command_locks.delete(cmd.stream_id) end cmd else @commands.first end end |
#schedule_commands(commands) ⇒ Object
121 122 123 |
# File 'lib/sourced/backends/test_backend.rb', line 121 def schedule_commands(commands) @commands = (@commands + commands).sort_by(&:created_at) end |