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



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dry/effects/providers/lock.rb', line 88

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:



81
82
83
# File 'lib/dry/effects/providers/lock.rb', line 81

def locate
  self
end

#lock(key, meta = Undefined) ⇒ Object



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

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

#locked?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#meta(key) ⇒ Object



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

def meta(key)
  backend.meta(key)
end

#ownedObject



114
115
116
# File 'lib/dry/effects/providers/lock.rb', line 114

def owned
  @owned ||= []
end

#representObject



118
119
120
121
122
123
124
# File 'lib/dry/effects/providers/lock.rb', line 118

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

#unlock(handle) ⇒ Object



69
70
71
# File 'lib/dry/effects/providers/lock.rb', line 69

def unlock(handle)
  backend.unlock(handle)
end

#with_backend(backend) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dry/effects/providers/lock.rb', line 101

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