Class: Kitsune::Kit::Adapters::FakeStateStore

Inherits:
StateStores::Store show all
Defined in:
lib/kitsune/kit/adapters/fake_state_store.rb

Constant Summary collapse

SCHEMA_VERSION =
1

Instance Method Summary collapse

Constructor Details

#initializeFakeStateStore

Returns a new instance of FakeStateStore.



13
14
15
16
17
18
# File 'lib/kitsune/kit/adapters/fake_state_store.rb', line 13

def initialize
  super
  @states = {}
  @mutex = Mutex.new
  @operation_mutexes = Hash.new { |hash, key| hash[key] = Mutex.new }
end

Instance Method Details

#delete(environment) ⇒ Object



38
39
40
41
# File 'lib/kitsune/kit/adapters/fake_state_store.rb', line 38

def delete(environment)
  name = safe_environment(environment)
  @mutex.synchronize { @states.delete(name) ? true : nil }
end

#read(environment) ⇒ Object



20
21
22
23
# File 'lib/kitsune/kit/adapters/fake_state_store.rb', line 20

def read(environment)
  name = safe_environment(environment)
  @mutex.synchronize { copy(@states.fetch(name) { empty_state(name) }) }
end

#update(environment) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/kitsune/kit/adapters/fake_state_store.rb', line 25

def update(environment)
  name = safe_environment(environment)
  @mutex.synchronize do
    updated = yield(copy(@states.fetch(name) { empty_state(name) }))
    updated["updated_at"] = Time.now.utc.iso8601(6)
    validate!(updated, name)
    @states[name] = copy(updated)
  end
  read(name)
end

#with_execution_lock(environment) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kitsune/kit/adapters/fake_state_store.rb', line 43

def with_execution_lock(environment)
  name = safe_environment(environment)
  lock = @mutex.synchronize { @operation_mutexes[name] }
  unless lock.try_lock
    raise Errors::UnsafeOperationError.new(
      "another mutating operation is active for #{name}",
      hint: "Wait for it to finish or stop it safely before retrying."
    )
  end
  yield
ensure
  lock&.unlock if lock&.owned?
end

#write(environment, state) ⇒ Object



36
# File 'lib/kitsune/kit/adapters/fake_state_store.rb', line 36

def write(environment, state) = update(environment) { state }