Class: GoldenBrindle::Base

Inherits:
Object
  • Object
show all
Includes:
Validations
Defined in:
lib/golden_brindle/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validations

#can_change_user?, #failure, #valid?, #valid_dir?, #valid_exists?, #valid_file?, #valid_group?, #valid_user?

Constructor Details

#initialize(argv) ⇒ Base

Called by the subclass to setup the command and parse the argv arguments. The call is destructive on argv since it uses the OptionParser#parse! function.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/golden_brindle/base.rb', line 8

def initialize(argv)
  @opt = ::OptionParser.new
  @opt.banner = GoldenBrindle::Const::BANNER
  @valid = true
  # this is retarded, but it has to be done this way because -h and -v exit
  @done_validating = false
  @original_args = argv.dup
  configure
  # I need to add my own -h definition to prevent the -h by default from exiting.
  @opt.on_tail("-h", "--help", "Show this message") do
    @done_validating = true
    puts @opt
  end
  # I need to add my own -v definition to prevent the -v from exiting by default as well.
  @opt.on_tail("--version", "Show version") do
    @done_validating = true
    if VERSION
      puts "Version #{GoldenBrindle::Const::VERSION}"
    end
  end
  @opt.parse! argv
end

Instance Attribute Details

#done_validatingObject (readonly)

Returns the value of attribute done_validating.



4
5
6
# File 'lib/golden_brindle/base.rb', line 4

def done_validating
  @done_validating
end

#original_argsObject (readonly)

Returns the value of attribute original_args.



4
5
6
# File 'lib/golden_brindle/base.rb', line 4

def original_args
  @original_args
end

#validObject (readonly)

Returns the value of attribute valid.



4
5
6
# File 'lib/golden_brindle/base.rb', line 4

def valid
  @valid
end

Instance Method Details

#config_keysObject



48
49
50
# File 'lib/golden_brindle/base.rb', line 48

def config_keys
  GoldenBrindle::Const::CONFIG_KEYS
end

#configureObject



44
45
46
# File 'lib/golden_brindle/base.rb', line 44

def configure
  options []
end

#helpObject

Returns a help message. Defaults to OptionParser#help which should be good.



79
80
81
# File 'lib/golden_brindle/base.rb', line 79

def help
  @opt.help
end

#load_configObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/golden_brindle/base.rb', line 52

def load_config
  settings = {}
  begin
    settings = ::YAML.load_file(@config_file)
  ensure
    STDERR.puts "** Loading settings from #{@config_file} (they override command line)." unless @daemon || settings[:daemon]
  end

  # Config file settings will override command line settings
  settings.each do |key, value|
    key = key.to_s
    if config_keys.include?(key)
      key = 'address' if key == 'host'
      self.instance_variable_set("@#{key}", value)
    else
      failure "Unknown configuration setting: #{key}"
      @valid = false
    end
  end
end

#options(opts) ⇒ Object

Called by the implemented command to set the options for that command. Every option has a short and long version, a description, a variable to set, and a default value. No exceptions.



34
35
36
37
38
39
40
41
42
# File 'lib/golden_brindle/base.rb', line 34

def options(opts)
  # process the given options array
  opts.each do |short, long, help, variable, default|
    self.instance_variable_set(variable, default)
    @opt.on(short, long, help) do |arg|
      self.instance_variable_set(variable, arg)
    end
  end
end

#runObject

Runs the command doing it’s job. You should implement this otherwise it will throw a NotImplementedError as a reminder.

Raises:

  • (NotImplementedError)


85
86
87
# File 'lib/golden_brindle/base.rb', line 85

def run
  raise NotImplementedError
end

#validateObject

Returns true/false depending on whether the command is configured properly.



74
75
76
# File 'lib/golden_brindle/base.rb', line 74

def validate
  @valid
end