Class: Flipper::Registry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/flipper/registry.rb

Overview

Internal: Used to store registry of groups by name.

Defined Under Namespace

Classes: DuplicateKey, Error, KeyNotFound

Instance Method Summary collapse

Constructor Details

#initialize(source = {}) ⇒ Registry

Returns a new instance of Registry.



21
22
23
24
# File 'lib/flipper/registry.rb', line 21

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

Instance Method Details

#add(key, value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/flipper/registry.rb', line 34

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

#clearObject



66
67
68
# File 'lib/flipper/registry.rb', line 66

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

#each(&block) ⇒ Object



62
63
64
# File 'lib/flipper/registry.rb', line 62

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

#get(key) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/flipper/registry.rb', line 46

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/flipper/registry.rb', line 55

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

#keysObject



26
27
28
# File 'lib/flipper/registry.rb', line 26

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

#valuesObject



30
31
32
# File 'lib/flipper/registry.rb', line 30

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