Class: GoldenBrindle::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/golden_brindle/command.rb

Overview

A Singleton class that manages all of the available commands and handles running them.

Class Method Summary collapse

Class Method Details

.commandsObject

Builds a list of possible commands from the Command derivates list



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/golden_brindle/command.rb', line 33

def commands
  GoldenBrindle::Actions.constants.inject([]) do |memo, action|
    constants = constantize("GoldenBrindle::Actions::#{action.to_s}").constants
    if constants.empty?
      memo << action.to_s.downcase
    else
      constants.each do |subaction|
        memo << "#{action.to_s}::#{subaction.to_s}".downcase
      end
    end
    memo
  end
end

.constantize(camel_cased_word) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/golden_brindle/command.rb', line 22

def constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?
  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

Prints a list of available commands.



48
49
50
51
52
53
54
55
56
# File 'lib/golden_brindle/command.rb', line 48

def print_command_list
  puts "#{GoldenBrindle::Const::BANNER}\nAvailable commands are:\n\n"
  commands.each do |name|
    puts " - #{name}\n"
  end

  puts "\nEach command takes -h as an option to get help."

end

.run(args) ⇒ Object

Runs the args against the first argument as the command name. If it has any errors it returns a false, otherwise it return true.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/golden_brindle/command.rb', line 60

def run(args)
  # find the command
  cmd_name = args.shift

  if !cmd_name or cmd_name == "?" or cmd_name == "help"
    print_command_list
    return true
  elsif cmd_name == "--version"
    puts "Golden Brindle #{GoldenBrindle::Const::VERSION}"
    return true
  end

  begin
    cmd_name = cmd_name.split("::").map{|x| x.capitalize}.join("::")
    constant = constantize("GoldenBrindle::Actions::#{cmd_name}")
    command = constant.new(args)
  rescue OptionParser::InvalidOption
    STDERR.puts "#$! for command '#{cmd_name}'"
    STDERR.puts "Try #{cmd_name} -h to get help."
    return false
  rescue
    STDERR.puts "ERROR RUNNING '#{cmd_name}': #$!"
    STDERR.puts "Use help command to get help"
    return false
  end

  # Normally the command is NOT valid right after being created
  # but sometimes (like with -h or -v) there's no further processing
  # needed so the command is already valid so we can skip it.
  if !command.done_validating
    if !command.validate
      STDERR.puts "#{cmd_name} reported an error. Use golden_brindle #{cmd_name} -h to get help."
      return false
    else
      command.run
    end
  end

  true
end