class WSLight::Config

Reads config file and parses command parameters

Constants

CONFIG_FILE
DEFAULT_OPTIONS

Public Class Methods

new() click to toggle source
# File lib/ws_light/config.rb, line 24
def initialize
  @config = DEFAULT_OPTIONS.merge(yaml_options).merge(command_line_options)
  store_options
end

Public Instance Methods

command_line_options() click to toggle source
# File lib/ws_light/config.rb, line 47
def command_line_options
  options = {}
  OptionParser.new do |opts|
    opts.banner = 'Usage: ws_light [options]'

    opts.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
      options['verbose'] = v
    end

    opts.on('-l NUMBER', '--left-pin NUMBER', 'Pin to which the left motion detector is connected') do |number|
      options['pin_left'] = number
    end

    opts.on('-r NUMBER', '--right-pin NUMBER', 'Pin to which the right motion detector is connected') do |number|
      options['pin_right'] = number
    end

    opts.on('-o PATH', '--log PATH', 'path to the log file') do |log_file|
      options['log_file'] = log_file
    end

    opts.on('--quiet-log', 'do not log detected motions') do
      options['track_motion_in_log'] = false
    end

    opts.on('--debug', 'output all log messages to stdout, too') do
      options['debug'] = true
    end
  end.parse!
  options
end
parse() click to toggle source
# File lib/ws_light/config.rb, line 35
def parse
  @config
end
store_options() click to toggle source
# File lib/ws_light/config.rb, line 29
def store_options
  File.open(CONFIG_FILE, 'w') do |file|
    file.puts @config.to_yaml
  end
end
yaml_options() click to toggle source
# File lib/ws_light/config.rb, line 39
def yaml_options
  if File.exist? CONFIG_FILE
    ::YAML.safe_load(File.read(CONFIG_FILE))
  else
    {}
  end
end