Sha256: 583a8a5f860bc7ce4e64030eca1cdc512bf5533c09b0b40d1faa61221a6a0c9a

Contents?: true

Size: 1.27 KB

Versions: 22

Compression:

Stored size: 1.27 KB

Contents

require 'thread'

module Flipper
  # Internal: Used to store registry of groups by name.
  class Registry
    include Enumerable

    class Error < StandardError; end
    class DuplicateKey < Error; end

    class KeyNotFound < Error
      # Public: The key that was not found
      attr_reader :key

      def initialize(key)
        @key = key
        super("Key #{key.inspect} not found")
      end
    end

    def initialize(source = {})
      @mutex = Mutex.new
      @source = source
    end

    def keys
      @mutex.synchronize { @source.keys }
    end

    def values
      @mutex.synchronize { @source.values }
    end

    def add(key, value)
      key = key.to_sym

      @mutex.synchronize {
        if @source[key]
          raise DuplicateKey, "#{key} is already registered"
        else
          @source[key] = value
        end
      }
    end

    def get(key)
      key = key.to_sym
      @mutex.synchronize {
        @source.fetch(key) {
          raise KeyNotFound.new(key)
        }
      }
    end

    def key?(key)
      key = key.to_sym
      @mutex.synchronize {
        @source.has_key?(key)
      }
    end

    def each(&block)
      @mutex.synchronize { @source.dup }.each(&block)
    end

    def clear
      @mutex.synchronize { @source.clear }
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
flipper-0.10.2 lib/flipper/registry.rb
flipper-0.10.1 lib/flipper/registry.rb
flipper-0.10.0 lib/flipper/registry.rb
flipper-0.9.2 lib/flipper/registry.rb
flipper-0.9.1 lib/flipper/registry.rb
flipper-0.9.0 lib/flipper/registry.rb
flipper-0.9.0.beta1 lib/flipper/registry.rb
flipper-0.8.0 lib/flipper/registry.rb
flipper-0.7.5 lib/flipper/registry.rb
flipper-0.7.4 lib/flipper/registry.rb
flipper-0.7.3 lib/flipper/registry.rb
flipper-0.7.2 lib/flipper/registry.rb
flipper-0.7.1 lib/flipper/registry.rb
flipper-0.7.0 lib/flipper/registry.rb
flipper-0.7.0.beta6 lib/flipper/registry.rb
flipper-0.7.0.beta5 lib/flipper/registry.rb
flipper-0.7.0.beta4 lib/flipper/registry.rb
flipper-0.7.0.beta3 lib/flipper/registry.rb
flipper-0.7.0.beta2 lib/flipper/registry.rb
flipper-0.7.0.beta1 lib/flipper/registry.rb