Class: Morpheus::Cli::CliRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/morpheus/cli/cli_registry.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCliRegistry

Returns a new instance of CliRegistry.



6
7
8
# File 'lib/morpheus/cli/cli_registry.rb', line 6

def initialize
  @commands = {}
end

Class Method Details

.add(klass, command_name = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/morpheus/cli/cli_registry.rb', line 25

def add(klass, command_name=nil)
  klass_command_name = cli_ize(klass.name.split('::')[-1])

  if has_command?(klass_command_name) && !command_name.nil?
    instance.remove(klass_command_name)
    instance.add(command_name, klass)
  else
    instance.add(klass_command_name, klass)
  end
end

.allObject



44
45
46
# File 'lib/morpheus/cli/cli_registry.rb', line 44

def all
  instance.all
end

.exec(command_name, args) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/morpheus/cli/cli_registry.rb', line 16

def exec(command_name, args)
  # begin
    Term::ANSIColor::coloring = STDOUT.isatty
    instance.get(command_name).new.handle(args)
  # rescue SystemExit, Interrupt
  #   puts "Interrupted..."
  # end
end

.has_command?(command_name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/morpheus/cli/cli_registry.rb', line 36

def has_command?(command_name)
  if command_name.nil? || command_name == ''
    false
  else
    !instance.get(command_name).nil?
  end
end

.instanceObject



12
13
14
# File 'lib/morpheus/cli/cli_registry.rb', line 12

def instance
  @instance ||= CliRegistry.new
end

Instance Method Details

#add(cmd_name, klass) ⇒ Object



71
72
73
# File 'lib/morpheus/cli/cli_registry.rb', line 71

def add(cmd_name, klass)
  @commands[cmd_name.to_sym] = klass
end

#allObject



63
64
65
# File 'lib/morpheus/cli/cli_registry.rb', line 63

def all
  @commands.reject {|cmd, klass| klass.hidden_command }
end

#get(cmd_name) ⇒ Object



67
68
69
# File 'lib/morpheus/cli/cli_registry.rb', line 67

def get(cmd_name)
  @commands[cmd_name.to_sym]
end

#remove(cmd_name) ⇒ Object



75
76
77
# File 'lib/morpheus/cli/cli_registry.rb', line 75

def remove(cmd_name)
  @commands.delete(cmd_name.to_sym)
end