Class: SVUtil::Config

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.config_fileObject



19
20
21
# File 'lib/svutil/config.rb', line 19

def config_file
 @config_file || 'settings'
end

Class Method Details

.apply(name, value) ⇒ Object



35
36
37
38
# File 'lib/svutil/config.rb', line 35

def apply(name, value)
	@hash ||= {}
 @hash[name.to_s] = value
end

.apply_allObject



40
41
42
43
# File 'lib/svutil/config.rb', line 40

def apply_all
 @hash ||= {}
 @hash.merge!(@temp_hash) if @temp_hash
end

.config_classObject



15
16
17
# File 'lib/svutil/config.rb', line 15

def config_class
	subclasses_of(self).first || self
end

.load_and_parseObject



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

def load_and_parse
  process_options
  load_config_file
  apply_all
  validate
end

.method_missing(method_id, *args) ⇒ Object



45
46
47
48
# File 'lib/svutil/config.rb', line 45

def method_missing(method_id, *args)
  return nil unless @hash
  @hash[method_id.to_s]
end

.parse_optionsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/svutil/config.rb', line 62

def parse_options
  OptionParser.new do |opts|
    opts.on("-f", "--config [filename]", "Config file to use (default 'settings')") do |filename|
	    self.config_file = filename
   end
    opts.on("-d", "--daemon", "Run in the background as a daemon") do
      self.set(:daemon, true)
    end
    opts.on("-l", "--debug-log [log-file]", "Debug Log File") do |log|
      self.set(log_file, log)
    end
    opts.on("-T", "--trace", "Display backtrace on errors") do
      self.set(:trace, true)
    end
   yield opts if block_given?
 end.parse!
end

.process_optionsObject



58
59
60
# File 'lib/svutil/config.rb', line 58

def process_options
  parse_options
end

.set(name, value) ⇒ Object



30
31
32
33
# File 'lib/svutil/config.rb', line 30

def set(name, value)
	@temp_hash ||= {}
 @temp_hash[name.to_s] = value
end

.validateObject



50
51
52
53
54
55
56
# File 'lib/svutil/config.rb', line 50

def validate
 # TODO: Check file perms
  if (pid_file.nil? or pid_file.empty? or File.directory?(pid_file))
    STDERR.puts "PID file must be a writable file"
    exit 1
  end
end