Class: SVUtil::Config

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

Overview

TODO: We could make Config a module so that it cannot be instantiated (see dalibornasevic.com/posts/9-ruby-singleton-pattern-again)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.attrsObject (readonly)

Returns the value of attribute attrs.



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

def attrs
  @attrs
end

.cli_option_handlersObject

Returns the value of attribute cli_option_handlers.



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

def cli_option_handlers
  @cli_option_handlers
end

.cli_optionsObject

Returns the value of attribute cli_options.



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

def cli_options
  @cli_options
end

.config_fileObject



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

def config_file
  @config_file ||= "settings"
end

.defaults {|@defaults| ... } ⇒ Object (readonly)

Returns the value of attribute defaults.

Yields:



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

def defaults
  @defaults
end

.option_sourceObject

Used mainly for testing



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

def option_source
  @option_source
end

Class Method Details

.clear_allObject



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

def clear_all
  @attrs = {}
end

.handle_options(&block) ⇒ Object



68
69
70
71
# File 'lib/svutil/config.rb', line 68

def handle_options(&block)
  @cli_option_handlers ||= []
  @cli_option_handlers << block
end

.initObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/svutil/config.rb', line 87

def init
  set do |c|
    self.config_provided_on_cli = false
    OptionParser.new do |opts|
      opts.on("-f", "--config [filename]", "Config file to use (default 'settings')") do |filename|
        self.config_file = filename.strip
        self.config_provided_on_cli = true
      end
      opts.on("-d", "--daemon", "Run in the background as a daemon") do
        set_cli_option(:daemon, true)
      end
      opts.on("-l", "--debug-log [log-file]", "Debug Log File") do |log|
        set_cli_option(:log_file, log)
      end
      opts.on("-T", "--trace", "Display backtrace on errors") do
        set_cli_option(:trace, true)
      end
      opts.on("-P", "--pid [pid-file]", "PID File") do |pid|
        set_cli_option(:pid_file, pid)
      end
      # Handle CLI passed options
      (cli_option_handlers || []).each do |handler|
        handler.call(opts)
      end
    end.parse!(self.option_source || ARGV)

    # Process the config file
    if (self.config_file && File.exists?(self.config_file)) || self.config_provided_on_cli
      load_config_file
    end

    # Finally apply any CLI options
    (cli_options || {}).each do |(k,v)|
      c.send("#{k}=", v)
    end
  end
end

.set(options = {}) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/svutil/config.rb', line 46

def set(options = {})
  @attrs ||= {}
  if block_given?
    yield self
  end
  self.validate
end

.set_cli_option(option, value) ⇒ Object



63
64
65
66
# File 'lib/svutil/config.rb', line 63

def set_cli_option(option, value)
  @cli_options ||= {}
  @cli_options[option.to_s] = value
end

.validateObject



77
78
79
80
81
82
83
84
85
# File 'lib/svutil/config.rb', line 77

def validate
 # TODO: Check file perms
  if ((pid_file.nil? or pid_file.empty? or File.directory?(pid_file)) && self.daemon)
    err("PID file must be a writable file")
  end
  # TODO: Validate the writability of the log file
  @validate_block.call(self) unless @validate_block.nil?
  true
end

.validator(&block) ⇒ Object



73
74
75
# File 'lib/svutil/config.rb', line 73

def validator(&block)
  @validate_block = block
end