Class: RR::Commander

Inherits:
Object
  • Object
show all
Defined in:
lib/refined-refinements/cli/commander.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.command(command_name, command_class) ⇒ Object



12
13
14
# File 'lib/refined-refinements/cli/commander.rb', line 12

def self.command(command_name, command_class)
  self.commands[command_name] = command_class
end

.commandsObject



8
9
10
# File 'lib/refined-refinements/cli/commander.rb', line 8

def self.commands
  @commands ||= Hash.new
end

Instance Method Details

#commandsObject



31
32
33
# File 'lib/refined-refinements/cli/commander.rb', line 31

def commands
  self.class.commands
end

#helpObject



24
25
26
27
28
29
# File 'lib/refined-refinements/cli/commander.rb', line 24

def help
  self.commands.reduce(self.help_template) do |buffer, (command_name, command_class)|
    command_help = command_class.help && command_class.help.split("\n").map { |line| line.sub(/^ {4}/, '') }.join("\n")
    command_class.help ? [buffer, command_help].join("\n") : buffer
  end.colourise
end

#help_template(program_name) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/refined-refinements/cli/commander.rb', line 16

def help_template(program_name)
  <<-EOF
<red.bold>:: #{program_name} ::</red.bold>

<cyan.bold>Commands</cyan.bold>
  EOF
end

#run(command_name, args) ⇒ Object



35
36
37
38
39
# File 'lib/refined-refinements/cli/commander.rb', line 35

def run(command_name, args)
  command_class = self.class.commands[command_name]
  command = command_class.new(args)
  command.run
end