Class: RubyEventStore::ExpectedVersion

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_event_store/expected_version.rb

Constant Summary collapse

POSITION_DEFAULT =
-1.freeze
NOT_RESOLVED =
Object.new.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ ExpectedVersion

Returns a new instance of ExpectedVersion.


22
23
24
25
# File 'lib/ruby_event_store/expected_version.rb', line 22

def initialize(version)
  @version = version
  invalid_version! unless [Integer, :any, :none, :auto].any? {|i| i === version}
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.


20
21
22
# File 'lib/ruby_event_store/expected_version.rb', line 20

def version
  @version
end

Class Method Details

.anyObject


8
9
10
# File 'lib/ruby_event_store/expected_version.rb', line 8

def self.any
  new(:any)
end

.autoObject


16
17
18
# File 'lib/ruby_event_store/expected_version.rb', line 16

def self.auto
  new(:auto)
end

.noneObject


12
13
14
# File 'lib/ruby_event_store/expected_version.rb', line 12

def self.none
  new(:none)
end

Instance Method Details

#==(other_expected_version) ⇒ Object Also known as: eql?


62
63
64
65
# File 'lib/ruby_event_store/expected_version.rb', line 62

def ==(other_expected_version)
  other_expected_version.instance_of?(self.class) &&
    other_expected_version.version.equal?(version)
end

#any?Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/ruby_event_store/expected_version.rb', line 27

def any?
  version.equal?(:any)
end

#auto?Boolean

Returns:

  • (Boolean)

31
32
33
# File 'lib/ruby_event_store/expected_version.rb', line 31

def auto?
  version.equal?(:auto)
end

#hashObject


55
56
57
58
59
60
# File 'lib/ruby_event_store/expected_version.rb', line 55

def hash
  [
    self.class,
    version
  ].hash ^ BIG_VALUE
end

#none?Boolean

Returns:

  • (Boolean)

35
36
37
# File 'lib/ruby_event_store/expected_version.rb', line 35

def none?
  version.equal?(:none)
end

#resolve_for(stream, resolver = Proc.new {}) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_event_store/expected_version.rb', line 39

def resolve_for(stream, resolver = Proc.new {})
  invalid_version! if stream.global? && !any?

  case version
  when Integer
    version
  when :none
    POSITION_DEFAULT
  when :auto
    resolver[stream] || POSITION_DEFAULT
  end
end