Class: StackFu::Commands::Command
- Inherits:
-
Object
- Object
- StackFu::Commands::Command
- Includes:
- OperatingSystems, Rendering
- Defined in:
- lib/stackfu/commands/command.rb
Direct Known Subclasses
ConfigCommand, DeployCommand, DumpCommand, GenerateCommand, HelpCommand, ListCommand, PublishCommand
Constant Summary
Constants included from Rendering
Constants included from OperatingSystems
OperatingSystems::FriendlyNames, OperatingSystems::OperatingSystems
Class Attribute Summary collapse
-
.aliases ⇒ Object
Returns the value of attribute aliases.
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#subcommand ⇒ Object
Returns the value of attribute subcommand.
Class Method Summary collapse
Instance Method Summary collapse
- #command ⇒ Object
-
#initialize(args = []) ⇒ Command
constructor
A new instance of Command.
- #params? ⇒ Boolean
- #run ⇒ Object
- #valid? ⇒ Boolean
Methods included from Rendering
#done, #error, #fill_values_from_options, #menu_for, #render_target, #spinner, #table, #warning
Methods included from OperatingSystems
Constructor Details
#initialize(args = []) ⇒ Command
Returns a new instance of Command.
60 61 62 63 64 |
# File 'lib/stackfu/commands/command.rb', line 60 def initialize(args=[]) @errors = [] (args.reverse) validate(self.class.[subcommand.to_sym]) end |
Class Attribute Details
.aliases ⇒ Object
Returns the value of attribute aliases.
9 10 11 |
# File 'lib/stackfu/commands/command.rb', line 9 def aliases @aliases end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
6 7 8 |
# File 'lib/stackfu/commands/command.rb', line 6 def errors @errors end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/stackfu/commands/command.rb', line 6 def end |
#parameters ⇒ Object
Returns the value of attribute parameters.
6 7 8 |
# File 'lib/stackfu/commands/command.rb', line 6 def parameters @parameters end |
#subcommand ⇒ Object
Returns the value of attribute subcommand.
6 7 8 |
# File 'lib/stackfu/commands/command.rb', line 6 def subcommand @subcommand end |
Class Method Details
.command_for(command) ⇒ Object
17 18 19 20 21 |
# File 'lib/stackfu/commands/command.rb', line 17 def command_for(command) StackFu::Commands.const_get("#{command.camelize}Command") rescue NameError raise Exceptions::UnknownCommand end |
.create(command, args = []) ⇒ Object
11 12 13 14 15 |
# File 'lib/stackfu/commands/command.rb', line 11 def create(command, args=[]) command ||= "help" klass = aliases[command.to_sym]||command_for(command) klass.new(args) end |
.inherited(kls) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/stackfu/commands/command.rb', line 23 def inherited(kls) def kls.aliases(*aliases) aliases.each do |a| (Command.aliases||={})[a.to_sym] = self end end def kls.alias_subcommand(spec) (@subcommand_aliases ||= {}).merge!(spec) end def kls.resolve(subcommand) (@subcommand_aliases ||= {})[subcommand.to_sym] or subcommand.to_sym end def kls.subcommand(name, ) ( ||= {})[name.to_sym] = end def kls. || {} end def kls.(spec) ( ||= {}).merge!(spec) end def kls.error_for(type) ( ||= {})[type] end end |
Instance Method Details
#command ⇒ Object
56 57 58 |
# File 'lib/stackfu/commands/command.rb', line 56 def command self.class.name.demodulize.underscore.gsub /_command/, "" end |
#params? ⇒ Boolean
66 67 68 |
# File 'lib/stackfu/commands/command.rb', line 66 def params? parameters.any? end |
#run ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/stackfu/commands/command.rb', line 70 def run unless self.respond_to?(subcommand) error = self.class.error_for(:missing_subcommand) error ||= "Command #{command} doesn't have a subcommand \"#{subcommand}\". Try \"stackfu #{command} help\" for more information." raise Exceptions::InvalidSubcommand, error % [subcommand] end send subcommand, parameters, end |
#valid? ⇒ Boolean
79 80 81 |
# File 'lib/stackfu/commands/command.rb', line 79 def valid? validate(self.class.[subcommand.to_sym]) end |