Sha256: 53557fcdf38b569d07abfb2d354db36c40221536ee4e99b3a942a0fe9ffc7002

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 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
          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
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-effects-0.3.0 lib/dry/effects/effects/lock.rb
dry-effects-0.2.0 lib/dry/effects/effects/lock.rb