Class: SVUtil::Config
- Inherits:
-
Object
- Object
- SVUtil::Config
- Defined in:
- lib/svutil/config.rb
Class Attribute Summary collapse
-
.cli_option_handlers ⇒ Object
readonly
Returns the value of attribute cli_option_handlers.
-
.validate_block ⇒ Object
readonly
Returns the value of attribute validate_block.
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
- #config_file ⇒ Object
Class Method Summary collapse
Instance Method Summary collapse
- #init(test_options = nil) ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #set(options = {}) ⇒ Object
- #set_cli_option(option, value) ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
31 32 33 34 35 36 37 38 |
# File 'lib/svutil/config.rb', line 31 def initialize @cli_options = {} @attrs = if self.class.defaults self.class.defaults.attrs.clone else {} end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *args) ⇒ Object (private)
127 128 129 130 131 132 133 |
# File 'lib/svutil/config.rb', line 127 def method_missing(method_id, *args) if method_id.to_s =~ /=$/ @attrs[method_id.to_s[0...-1]] = args.first else value = @attrs[method_id.to_s] end end |
Class Attribute Details
.cli_option_handlers ⇒ Object (readonly)
Returns the value of attribute cli_option_handlers.
53 54 55 |
# File 'lib/svutil/config.rb', line 53 def cli_option_handlers @cli_option_handlers end |
.validate_block ⇒ Object (readonly)
Returns the value of attribute validate_block.
54 55 56 |
# File 'lib/svutil/config.rb', line 54 def validate_block @validate_block end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
29 30 31 |
# File 'lib/svutil/config.rb', line 29 def attrs @attrs end |
#config_file ⇒ Object
47 48 49 |
# File 'lib/svutil/config.rb', line 47 def config_file @config_file ||= "settings" end |
Class Method Details
.defaults {|@defaults| ... } ⇒ Object
56 57 58 59 60 |
# File 'lib/svutil/config.rb', line 56 def defaults @defaults ||= Defaults.new yield @defaults if block_given? return @defaults end |
.handle_options(&block) ⇒ Object
62 63 64 65 |
# File 'lib/svutil/config.rb', line 62 def (&block) @cli_option_handlers ||= [] @cli_option_handlers << block end |
.validator(&block) ⇒ Object
67 68 69 |
# File 'lib/svutil/config.rb', line 67 def validator(&block) @validate_block = block end |
Instance Method Details
#init(test_options = nil) ⇒ Object
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 124 |
# File 'lib/svutil/config.rb', line 87 def init( = nil) 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 (self.class.cli_option_handlers || []).each do |handler| self.instance_exec(opts, &handler) end end.parse!( || 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)| @attrs[k.to_s] = v end end end |
#set(options = {}) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/svutil/config.rb', line 40 def set( = {}) if block_given? yield self end self.validate end |
#set_cli_option(option, value) ⇒ Object
72 73 74 75 |
# File 'lib/svutil/config.rb', line 72 def set_cli_option(option, value) @cli_options ||= {} @cli_options[option.to_s] = value end |
#validate ⇒ Object
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 self.instance_exec(self, &self.class.validate_block) unless self.class.validate_block.nil? true end |