Class: Ing::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/ing/command.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, *args) ⇒ Command

Returns a new instance of Command.



18
19
20
21
22
23
# File 'lib/ing/command.rb', line 18

def initialize(klass, *args)
  self.options        = (Hash === args.last ? args.pop : {})
  self.command_class = klass
  self.command_meth  = extract_method!(args, command_class)
  self.args           = args
end

Class Attribute Details

.parserObject



7
8
9
# File 'lib/ing/command.rb', line 7

def parser
  OptionParsers::Trollop
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



16
17
18
# File 'lib/ing/command.rb', line 16

def args
  @args
end

#command_classObject

Returns the value of attribute command_class.



16
17
18
# File 'lib/ing/command.rb', line 16

def command_class
  @command_class
end

#command_methObject

Returns the value of attribute command_meth.



16
17
18
# File 'lib/ing/command.rb', line 16

def command_meth
  @command_meth
end

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/ing/command.rb', line 16

def options
  @options
end

Class Method Details

.execute(klass, *args, &config) ⇒ Object



11
12
13
# File 'lib/ing/command.rb', line 11

def execute(klass, *args, &config)
  new(klass, *args).execute(&config)
end

Instance Method Details

#classy?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ing/command.rb', line 25

def classy?
  command_class.respond_to?(:new)
end

#describeObject



39
40
41
# File 'lib/ing/command.rb', line 39

def describe
  with_option_parser {|p| p.describe}
end

#execute {|instance| ... } ⇒ Object

Yields:



33
34
35
36
37
# File 'lib/ing/command.rb', line 33

def execute
  yield instance if block_given?
  classy? ? instance.send(command_meth, *args) : 
            instance.send(command_meth, *args, options)
end

#helpObject



43
44
45
# File 'lib/ing/command.rb', line 43

def help
  with_option_parser {|p| p.help}
end

#instanceObject



29
30
31
# File 'lib/ing/command.rb', line 29

def instance
  @instance ||= build_command
end

#with_option_parser {|p| ... } ⇒ Object

Yields:

  • (p)


47
48
49
50
51
52
# File 'lib/ing/command.rb', line 47

def with_option_parser
  return unless command_class.respond_to?(:specify_options)
  p = self.class.parser.new
  command_class.specify_options(p.parser)
  yield p
end