Class: Morpheus::Cli::CliRegistry
- Inherits:
-
Object
- Object
- Morpheus::Cli::CliRegistry
- Defined in:
- lib/morpheus/cli/cli_registry.rb
Class Method Summary collapse
- .add(klass, command_name = nil) ⇒ Object
- .all ⇒ Object
- .exec(command_name, args) ⇒ Object
- .has_command?(command_name) ⇒ Boolean
- .instance ⇒ Object
Instance Method Summary collapse
- #add(cmd_name, klass) ⇒ Object
- #all ⇒ Object
- #get(cmd_name) ⇒ Object
-
#initialize ⇒ CliRegistry
constructor
A new instance of CliRegistry.
- #remove(cmd_name) ⇒ Object
Constructor Details
#initialize ⇒ CliRegistry
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 |
.all ⇒ Object
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
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 |
.instance ⇒ Object
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 |
#all ⇒ Object
63 64 65 |
# File 'lib/morpheus/cli/cli_registry.rb', line 63 def all @commands 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 |