Class: Rox::Core::FlagRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/rox/core/repositories/flag_repository.rb

Instance Method Summary collapse

Constructor Details

#initializeFlagRepository

Returns a new instance of FlagRepository.



4
5
6
7
8
9
# File 'lib/rox/core/repositories/flag_repository.rb', line 4

def initialize
  @variants = {}
  @flag_added_handlers = []
  @mutex = Mutex.new
  @handlers_mutex = Mutex.new
end

Instance Method Details

#add_flag(variant, name) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/rox/core/repositories/flag_repository.rb', line 11

def add_flag(variant, name)
  variant.name = name if variant.name.nil? || variant.name.empty?
  @mutex.synchronize do
    @variants[name] = variant
  end
  raise_flag_added_event(variant)
end

#all_flagsObject



25
26
27
28
29
# File 'lib/rox/core/repositories/flag_repository.rb', line 25

def all_flags
  @mutex.synchronize do
    return @variants.values
  end
end

#flag(name) ⇒ Object



19
20
21
22
23
# File 'lib/rox/core/repositories/flag_repository.rb', line 19

def flag(name)
  @mutex.synchronize do
    return @variants[name]
  end
end

#raise_flag_added_event(flag) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/rox/core/repositories/flag_repository.rb', line 37

def raise_flag_added_event(flag)
  handlers = []
  @handlers_mutex.synchronize do
    handlers = @flag_added_handlers.clone
  end

  handlers.each do |handler|
    handler.call(flag)
  end
end

#register_flag_added_handler(&block) ⇒ Object



31
32
33
34
35
# File 'lib/rox/core/repositories/flag_repository.rb', line 31

def register_flag_added_handler(&block)
  @handlers_mutex.synchronize do
    @flag_added_handlers << block
  end
end