Sha256: 632fd27e62488dc145a72c24df017d1d3883037ec1d579cd3fc99da7cb6ed3e4

Contents?: true

Size: 1.83 KB

Versions: 2

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Wireless
  # A Hash wrapper with synchronized get, set and check methods.
  class SynchronizedStore
    def initialize(store = {}, replace: false, type: :key)
      @type = type
      @replace = replace
      @store = store
      @lock = Mutex.new
    end

    # Retrieve a value from the store
    #
    # A synchronized version of:
    #
    #   store[key]
    #
    def [](key)
      @lock.synchronize { @store[key] }
    end

    # Add a key/value to the store
    #
    # A synchronized version of:
    #
    #   store[key] = value
    #
    def []=(key, value)
      @lock.synchronize do
        if !@replace && @store.include?(key)
          # XXX don't expose the receiver as this class is an internal
          # implementation detail
          raise Wireless::KeyError.new(
            "#{@type} already exists: #{key}",
            key: key
          )
        end

        @store[key] = value
      end
    end

    # Retrieve a value from the store. If it doesn't exist and a block is
    # supplied, create and return it; otherwise, raise a KeyError.
    #
    # A synchronized version of:
    #
    #   store[key] ||= value
    #
    def get_or_create(key)
      @lock.synchronize do
        if @store.include?(key)
          @store[key]
        elsif block_given?
          @store[key] = yield
        else
          # XXX don't expose the receiver as this class is an internal
          # implementation detail
          raise Wireless::KeyError.new(
            "#{@type} not found: #{key}",
            key: key
          )
        end
      end
    end

    alias get! get_or_create

    # Returns true if the store contains the key, false otherwise
    #
    # A synchronized version of:
    #
    #   store.include?(key)
    #
    def include?(key)
      @lock.synchronize { @store.include?(key) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wireless-0.1.0 lib/wireless/synchronized_store.rb
wireless-0.0.2 lib/wireless/synchronized_store.rb