Class: LogSinks::Repository

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/log_sinks/repository.rb

Overview

Repository stores references to loggers by logger name.

Instance Method Summary collapse

Constructor Details

#initializeRepository

Initializes new instance of [LogSinks] class.



11
12
13
14
# File 'lib/log_sinks/repository.rb', line 11

def initialize
  @store = {}
  @mutex = Mutex.new
end

Instance Method Details

#get_or_add(name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/log_sinks/repository.rb', line 22

def get_or_add(name)
  name = canonize_name(name)
  logger = @store[name]

  if logger.nil?
    logger = @mutex.synchronize do
      @store[name] = yield(name) unless @store.key?(name)
      @store[name]
    end
  end
  logger
end

#key?(name) ⇒ Boolean

Tests if logger with given name exists

Returns:

  • (Boolean)

    true if logger exists; false otherwise.



18
19
20
# File 'lib/log_sinks/repository.rb', line 18

def key?(name)
  @store.key? canonize_name(name)
end