Class: Dry::Effects::Providers::Lock
- Inherits:
-
Object
- Object
- Dry::Effects::Providers::Lock
- Defined in:
- lib/dry/effects/providers/lock.rb
Defined Under Namespace
Constant Summary collapse
- Locate =
Effect.new(type: :lock, name: :locate)
Instance Method Summary collapse
-
#call(backend = Undefined) ⇒ Object
private
Yield the block with the handler installed.
-
#locate ⇒ Provider
private
Locate handler in the stack.
- #lock(key, meta = Undefined) ⇒ Object
- #locked?(key) ⇒ Boolean
- #meta(key) ⇒ Object
- #owned ⇒ Object
- #represent ⇒ Object
- #unlock(handle) ⇒ Object
- #with_backend(backend) ⇒ Object
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 |
#locate ⇒ Provider
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
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, = Undefined) locked = backend.lock(key, ) owned << locked if locked locked end |
#locked?(key) ⇒ 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 (key) backend.(key) end |
#owned ⇒ Object
114 115 116 |
# File 'lib/dry/effects/providers/lock.rb', line 114 def owned @owned ||= [] end |
#represent ⇒ Object
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 |