Class: PgEventstore::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_eventstore/stream.rb

Constant Summary collapse

SYSTEM_STREAM_PREFIX =

Returns a stream prefix of the system stream.

Returns:

  • (String)

    a stream prefix of the system stream

'$'
NON_EXISTING_STREAM_REVISION =

Returns:

  • (Integer)
-1
# @return [Array<String>]
KNOWN_SYSTEM_STREAMS =

Returns:

  • (Array<String>)
%w[$streams].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, stream_name:, stream_id:) ⇒ Stream

Returns a new instance of Stream.

Parameters:

  • context (String)
  • stream_name (String)
  • stream_id (String)


44
45
46
47
48
# File 'lib/pg_eventstore/stream.rb', line 44

def initialize(context:, stream_name:, stream_id:)
  @context = context
  @stream_name = stream_name
  @stream_id = stream_id
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



33
34
35
# File 'lib/pg_eventstore/stream.rb', line 33

def context
  @context
end

#stream_idObject

Returns the value of attribute stream_id.



39
40
41
# File 'lib/pg_eventstore/stream.rb', line 39

def stream_id
  @stream_id
end

#stream_nameObject

Returns the value of attribute stream_name.



36
37
38
# File 'lib/pg_eventstore/stream.rb', line 36

def stream_name
  @stream_name
end

Class Method Details

.all_streamPgEventstore::Stream

Produces “all” stream instance. “all” stream does not represent any specific stream. Instead, it indicates that a specific command should be performed over any kind of streams if possible



18
19
20
21
22
# File 'lib/pg_eventstore/stream.rb', line 18

def all_stream
  allocate.tap do |stream|
    stream.instance_variable_set(:@all_stream, true)
  end
end

.system_stream(name) ⇒ PgEventstore::Stream

Parameters:

  • name (String)

Returns:



26
27
28
# File 'lib/pg_eventstore/stream.rb', line 26

def system_stream(name)
  new(context: name, stream_name: '', stream_id: '')
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/pg_eventstore/stream.rb', line 96

def ==(other)
  return false unless other.is_a?(Stream)

  to_hash == other.to_hash
end

#all_stream?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/pg_eventstore/stream.rb', line 51

def all_stream?
  !!@all_stream
end

#deconstructArray Also known as: to_a

Returns:

  • (Array)


62
63
64
# File 'lib/pg_eventstore/stream.rb', line 62

def deconstruct
  [context, stream_name, stream_id]
end

#deconstruct_keys(keys) ⇒ Hash<Symbol => String>

Parameters:

  • keys (Array<Symbol>, nil)

Returns:

  • (Hash<Symbol => String>)


69
70
71
72
73
74
# File 'lib/pg_eventstore/stream.rb', line 69

def deconstruct_keys(keys)
  hash = { context: context, stream_name: stream_name, stream_id: stream_id }
  return hash unless keys

  hash.slice(*keys)
end

#eql?(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/pg_eventstore/stream.rb', line 88

def eql?(other)
  return false unless other.is_a?(Stream)

  hash == other.hash
end

#hashInteger

Returns:

  • (Integer)


82
83
84
# File 'lib/pg_eventstore/stream.rb', line 82

def hash
  to_hash.hash
end

#system?Boolean

Determine whether a stream is reserved by ‘pg_eventstore`. You can’t append events to such streams.

Returns:

  • (Boolean)


57
58
59
# File 'lib/pg_eventstore/stream.rb', line 57

def system?
  all_stream? || context.start_with?(SYSTEM_STREAM_PREFIX)
end

#to_hashHash

Returns:

  • (Hash)


77
78
79
# File 'lib/pg_eventstore/stream.rb', line 77

def to_hash
  deconstruct_keys(nil)
end