Class: Dry::Effects::Providers::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/effects/providers/lock.rb

Defined Under Namespace

Classes: Backend, Handle

Constant Summary collapse

Locate =
Effect.new(type: :lock, name: :locate)

Instance Method Summary collapse

Instance Method Details

#call(backend = Undefined) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yield the block with the handler installed



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dry/effects/providers/lock.rb', line 78

def call(backend = Undefined)
  backend_replace = Undefined.default(backend) do
    parent = ::Dry::Effects.yield(Locate) { Undefined }
    Undefined.map(parent, &:backend)
  end

  with_backend(backend_replace) do
    yield
  ensure
    owned.each { unlock(_1) }
  end
end

#locateProvider

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Locate handler in the stack

Returns:



73
# File 'lib/dry/effects/providers/lock.rb', line 73

def locate = self

#lock(key, meta = Undefined) ⇒ Object



57
58
59
60
61
# File 'lib/dry/effects/providers/lock.rb', line 57

def lock(key, meta = Undefined)
  locked = backend.lock(key, meta)
  owned << locked if locked
  locked
end

#locked?(key) ⇒ Boolean

Returns:

  • (Boolean)


63
# File 'lib/dry/effects/providers/lock.rb', line 63

def locked?(key) = backend.locked?(key)

#meta(key) ⇒ Object



67
# File 'lib/dry/effects/providers/lock.rb', line 67

def meta(key) = backend.meta(key)

#ownedObject



104
105
106
# File 'lib/dry/effects/providers/lock.rb', line 104

def owned
  @owned ||= []
end

#representObject



108
109
110
111
112
113
114
# File 'lib/dry/effects/providers/lock.rb', line 108

def represent
  if owned.empty?
    super
  else
    "lock[owned=#{owned.size}]"
  end
end

#unlock(handle) ⇒ Object



65
# File 'lib/dry/effects/providers/lock.rb', line 65

def unlock(handle) = backend.unlock(handle)

#with_backend(backend) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dry/effects/providers/lock.rb', line 91

def with_backend(backend)
  if Undefined.equal?(backend)
    yield
  else
    begin
      before, @backend = @backend, backend
      yield
    ensure
      @backend = before
    end
  end
end