Sha256: f44c8500ceacb480a46e1a2e331e07367bd7a0ebd9347c0709f7253e57218288

Contents?: true

Size: 1.22 KB

Versions: 72

Compression:

Stored size: 1.22 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 do
        if @source[key]
          raise DuplicateKey, "#{key} is already registered"
        else
          @source[key] = value
        end
      end
    end

    def get(key)
      key = key.to_sym
      @mutex.synchronize do
        @source[key]
      end
    end

    def key?(key)
      key = key.to_sym
      @mutex.synchronize do
        @source.key?(key)
      end
    end

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

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

Version data entries

72 entries across 72 versions & 1 rubygems

Version Path
flipper-0.26.1 lib/flipper/registry.rb
flipper-0.26.0 lib/flipper/registry.rb
flipper-0.26.0.rc2 lib/flipper/registry.rb
flipper-0.26.0.rc1 lib/flipper/registry.rb
flipper-0.25.4 lib/flipper/registry.rb
flipper-0.25.3 lib/flipper/registry.rb
flipper-0.25.2 lib/flipper/registry.rb
flipper-0.25.1 lib/flipper/registry.rb
flipper-0.25.0 lib/flipper/registry.rb
flipper-0.24.1 lib/flipper/registry.rb
flipper-0.24.0 lib/flipper/registry.rb
flipper-0.23.1 lib/flipper/registry.rb
flipper-0.23.0 lib/flipper/registry.rb
flipper-0.22.2 lib/flipper/registry.rb
flipper-0.22.1 lib/flipper/registry.rb
flipper-0.22.0 lib/flipper/registry.rb
flipper-0.21.0 lib/flipper/registry.rb
flipper-0.21.0.rc2 lib/flipper/registry.rb
flipper-0.21.0.rc1 lib/flipper/registry.rb
flipper-0.20.4 lib/flipper/registry.rb