Module: Loops::CLI::Commands::ClassMethods
- Defined in:
- lib/loops/cli/commands.rb
Constant Summary collapse
- @@running =
We set this to true when a command is running
false
Instance Method Summary collapse
-
#[](command_name) ⇒ Command?
Return the registered command from the command name.
-
#command_names ⇒ Array<String>
Get a list of command names.
-
#execute ⇒ Object
Parse arguments, find and execute command requested.
-
#load_and_instantiate(command_name) ⇒ Command?
Load and instantiate a given command.
-
#register_command(command_name) ⇒ Object
Register a Loops command.
-
#running? ⇒ Boolean
Returns running status.
Instance Method Details
#[](command_name) ⇒ Command?
Return the registered command from the command name.
60 61 62 63 |
# File 'lib/loops/cli/commands.rb', line 60 def [](command_name) command_name = command_name.to_sym @@commands[command_name] ||= load_and_instantiate(command_name) end |
#command_names ⇒ Array<String>
Get a list of command names.
49 50 51 |
# File 'lib/loops/cli/commands.rb', line 49 def command_names @@commands.keys.map { |c| c.to_s } end |
#execute ⇒ Object
Parse arguments, find and execute command requested.
27 28 29 30 31 32 |
# File 'lib/loops/cli/commands.rb', line 27 def execute @@running = true parse(ARGV).run! ensure @@running = false end |
#load_and_instantiate(command_name) ⇒ Command?
Load and instantiate a given command.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/loops/cli/commands.rb', line 72 def load_and_instantiate(command_name) command_name = command_name.to_s retried = false begin const_name = command_name.capitalize.gsub(/_(.)/) { $1.upcase } Loops::Commands.const_get("#{const_name}Command").new rescue NameError if retried then nil else retried = true require File.join(Loops::LIB_ROOT, 'loops/commands', "#{command_name}_command") retry end end end |
#register_command(command_name) ⇒ Object
Register a Loops command.
39 40 41 42 |
# File 'lib/loops/cli/commands.rb', line 39 def register_command(command_name) @@commands ||= {} @@commands[command_name.to_sym] = nil end |
#running? ⇒ Boolean
Returns running status
21 22 23 |
# File 'lib/loops/cli/commands.rb', line 21 def running? @@running end |