Class: Valkyrie::Persistence::OptimisticLockToken

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/persistence/optimistic_lock_token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter_id:, token:) ⇒ OptimisticLockToken

Returns a new instance of OptimisticLockToken.

Parameters:

  • adapter_id (Valkyrie::ID)
  • token (String, Integer)

    for the adapter



8
9
10
11
# File 'lib/valkyrie/persistence/optimistic_lock_token.rb', line 8

def initialize(adapter_id:, token:)
  @adapter_id = adapter_id
  @token = token
end

Instance Attribute Details

#adapter_idObject (readonly)

Returns the value of attribute adapter_id.



4
5
6
# File 'lib/valkyrie/persistence/optimistic_lock_token.rb', line 4

def adapter_id
  @adapter_id
end

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/valkyrie/persistence/optimistic_lock_token.rb', line 4

def token
  @token
end

Class Method Details

.deserialize(serialized_token) ⇒ Object

Deserializing lock tokens means that we can then use the adapter id and the lock token value



27
28
29
30
31
32
# File 'lib/valkyrie/persistence/optimistic_lock_token.rb', line 27

def self.deserialize(serialized_token)
  token_parts = serialized_token.to_s.split(":", 3)
  adapter_id = token_parts[1]
  token = token_parts[2]
  new(adapter_id: Valkyrie::ID.new(adapter_id), token: token)
end

Instance Method Details

#==(other) ⇒ Object



20
21
22
23
24
# File 'lib/valkyrie/persistence/optimistic_lock_token.rb', line 20

def ==(other)
  return false unless other.is_a?(self.class)
  return false unless adapter_id == other.adapter_id
  token == other.token
end

#serializeObject Also known as: to_s

Serializing lock tokens makes them easy to cast to strings and back. Primary use case is for embedding one in a form as a hidden field



15
16
17
# File 'lib/valkyrie/persistence/optimistic_lock_token.rb', line 15

def serialize
  "lock_token:#{adapter_id}:#{token}"
end