Class: SVUtil::Config

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

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_handlersObject (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_blockObject (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

#attrsObject (readonly)

Returns the value of attribute attrs.



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

def attrs
  @attrs
end

#config_fileObject



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

def config_file
  @config_file ||= "settings"
end

Class Method Details

.defaults {|@defaults| ... } ⇒ Object

Yields:



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 handle_options(&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(test_options = 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!(test_options || 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(options = {})
  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

#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
  self.instance_exec(self, &self.class.validate_block) unless self.class.validate_block.nil?
  true
end