Sha256: 517ad1519fd79f2f7d6949a40186a557da8aa205a37edc05cc5518a08aaab504

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 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.(key, meta))
                  block.(!handle.nil?)
                ensure
                  ::Dry::Effects.yield(Unlock.(handle)) if handle
                end
              else
                ::Dry::Effects.yield(Lock.(key, meta))
              end
            end

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

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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dry-effects-0.1.3 lib/dry/effects/effects/lock.rb
dry-effects-0.1.2 lib/dry/effects/effects/lock.rb
dry-effects-0.1.1 lib/dry/effects/effects/lock.rb
dry-effects-0.1.0 lib/dry/effects/effects/lock.rb
dry-effects-0.1.0.alpha2 lib/dry/effects/effects/lock.rb
dry-effects-0.1.0.alpha lib/dry/effects/effects/lock.rb