Class: Flipper::Registry

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

Defined Under Namespace

Classes: DuplicateKey, Error, MissingKey

Instance Method Summary collapse

Constructor Details

#initialize(source = {}) ⇒ Registry

Returns a new instance of Registry.



7
8
9
10
# File 'lib/flipper/registry.rb', line 7

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

Instance Method Details

#add(key, value) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/flipper/registry.rb', line 12

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

#clearObject



32
33
34
# File 'lib/flipper/registry.rb', line 32

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

#each(&block) ⇒ Object



28
29
30
# File 'lib/flipper/registry.rb', line 28

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

#get(key) ⇒ Object



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

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