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

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

Constant Summary collapse

Lock =
Effect.new(type: :lock, name: :lock)
Meta =
Effect.new(type: :lock, name: :meta)
Unlock =
Effect.new(type: :lock, name: :unlock)
Locked =
Effect.new(type: :lock, name: :locked?)

Instance Method Summary collapse

Constructor Details

#initializeLock

Returns a new instance of Lock.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dry/effects/effects/lock.rb', line 12

def initialize
  super

  module_eval do
    define_method(:lock) do |key, meta: Undefined, &block|
      if block
        begin
          handle = ::Dry::Effects.yield(Lock.payload(key, meta))
          block.(!handle.nil?)
        ensure
          ::Dry::Effects.yield(Unlock.payload(handle)) if handle
        end
      else
        ::Dry::Effects.yield(Lock.payload(key, meta))
      end
    end

    define_method(:unlock) do |key|
      ::Dry::Effects.yield(Unlock.(key))
    end

    define_method(:locked?) do |key|
      ::Dry::Effects.yield(Locked.payload(key))
    end

    define_method(:lock_meta) do |key|
      ::Dry::Effects.yield(Meta.payload(key))
    end
  end
end