Class: StackFu::Commands::Command

Inherits:
Object
  • Object
show all
Includes:
OperatingSystems, Rendering
Defined in:
lib/stackfu/commands/command.rb

Constant Summary

Constants included from Rendering

Rendering::LEFT_MARGIN

Constants included from OperatingSystems

OperatingSystems::FriendlyNames, OperatingSystems::OperatingSystems

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rendering

#done, #error, #fill_values_from_options, #menu_for, #render_target, #spinner, #table, #warning

Methods included from OperatingSystems

#convert_os, #os_name

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 = []
  parse_options(args.reverse)
  validate(self.class.subcommand_options[subcommand.to_sym])
end

Class Attribute Details

.aliasesObject

Returns the value of attribute aliases.



9
10
11
# File 'lib/stackfu/commands/command.rb', line 9

def aliases
  @aliases
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



6
7
8
# File 'lib/stackfu/commands/command.rb', line 6

def errors
  @errors
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/stackfu/commands/command.rb', line 6

def options
  @options
end

#parametersObject

Returns the value of attribute parameters.



6
7
8
# File 'lib/stackfu/commands/command.rb', line 6

def parameters
  @parameters
end

#subcommandObject

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, options)
    (@subcommand_options ||= {})[name.to_sym] = options
  end

  def kls.subcommand_options
    @subcommand_options || {}
  end

  def kls.error_messages(spec)
    (@error_messages ||= {}).merge!(spec)
  end

  def kls.error_for(type)
    (@error_messages ||= {})[type]
  end
end

Instance Method Details

#commandObject



56
57
58
# File 'lib/stackfu/commands/command.rb', line 56

def command
  self.class.name.demodulize.underscore.gsub /_command/, ""
end

#params?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/stackfu/commands/command.rb', line 66

def params?
  parameters.any?
end

#runObject



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, options
end

#valid?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/stackfu/commands/command.rb', line 79

def valid?
  validate(self.class.subcommand_options[subcommand.to_sym])
end