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.



22
23
24
25
26
27
28
29
# File 'lib/tumugi/config.rb', line 22

def initialize
  @workers = 1
  @max_retry = 3
  @retry_interval = 300 #seconds
  @param_auto_bind_enabled = true
  @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

#param_auto_bind_enabledObject

Returns the value of attribute param_auto_bind_enabled.



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

def param_auto_bind_enabled
  @param_auto_bind_enabled
end

#retry_intervalObject

Returns the value of attribute retry_interval.



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

def retry_interval
  @retry_interval
end

#workersObject

Returns the value of attribute workers.



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

def workers
  @workers
end

Class Method Details

.camelize(term) ⇒ Object



15
16
17
18
19
20
# File 'lib/tumugi/config.rb', line 15

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

.register_section(name, *args) ⇒ Object



10
11
12
13
# File 'lib/tumugi/config.rb', line 10

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

Instance Method Details

#section(name, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tumugi/config.rb', line 31

def section(name, &block)
  section_class = @@sections[name]
  if block_given?
    @section_procs[name] ||= block
  elsif section_class.nil?
    raise ConfigError, "Config section '#{name}' is not registered."
  else
    @section_instances[name] ||= section_class.new
    if @section_procs[name]
      begin
        @section_procs[name].call(@section_instances[name])
      rescue NoMethodError => e
        Tumugi.logger.error "#{e.message}. Available attributes are #{@section_instances[name].members}"
        raise e
      end
    end
    @section_instances[name]
  end
end