Class: Tumugi::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/tumugi/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



28
29
30
31
32
33
34
35
36
# File 'lib/tumugi/config.rb', line 28

def initialize
  @workers = 1
  @max_retry = 3
  @retry_interval = 300 #seconds
  @timeout = nil # meaning no timeout

  @section_procs = {}
  @section_instances = {}
end

Instance Attribute Details

#max_retryObject

Returns the value of attribute max_retry.



6
7
8
# File 'lib/tumugi/config.rb', line 6

def max_retry
  @max_retry
end

#retry_intervalObject

Returns the value of attribute retry_interval.



7
8
9
# File 'lib/tumugi/config.rb', line 7

def retry_interval
  @retry_interval
end

#timeoutObject

Returns the value of attribute timeout.



8
9
10
# File 'lib/tumugi/config.rb', line 8

def timeout
  @timeout
end

#workersObject

Returns the value of attribute workers.



5
6
7
# File 'lib/tumugi/config.rb', line 5

def workers
  @workers
end

Class Method Details

.camelize(term) ⇒ Object



17
18
19
20
21
22
# File 'lib/tumugi/config.rb', line 17

def self.camelize(term)
  string = term.to_s
  string = string.sub(/^[a-z\d]*/) { $&.capitalize }
  string.gsub!(/(?:_|(\/))([a-z\d]*)/) { $2.capitalize }
  string
end

.loggerObject



24
25
26
# File 'lib/tumugi/config.rb', line 24

def self.logger
  @logger ||= Tumugi::ScopedLogger.new("tumugi-config")
end

.register_section(name, *args) ⇒ Object



12
13
14
15
# File 'lib/tumugi/config.rb', line 12

def self.register_section(name, *args)
  @@sections[name] = Struct.new(camelize(name), *args)
  logger.info { "registered config section '#{name}' with '#{args}'" }
end

Instance Method Details

#section(name, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tumugi/config.rb', line 38

def section(name, &block)
  if block_given?
    raise Tumugi::ConfigError.new('You cannot change section') if frozen?
    @section_procs[name] ||= block
    return nil
  end

  section_class = @@sections[name]
  if section_class.nil?
    raise ConfigError.new("Config section '#{name}' is not registered.")
  end

  if @section_instances[name].nil?
    @section_instances[name] = section_class.new
    begin
      @section_procs[name].call(@section_instances[name])
    rescue NoMethodError => e
      Config.logger.error "#{e.message}. Available attributes are #{@section_instances[name].members}"
      raise e
    end if @section_procs[name]
  end
  @section_instances[name].clone.freeze
end