Sha256: 64824c16454212eff2ea061cd1210c373feb8480bb8331014183ee43eba745a0

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require 'dry/effects/effect'

module Dry
  module Effects
    module Effects
      class Lock < ::Module
        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?)

        def initialize
          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
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-effects-0.1.5 lib/dry/effects/effects/lock.rb
dry-effects-0.1.4 lib/dry/effects/effects/lock.rb