Module: Ing::DefaultCommand

Defined in:
lib/ing/default_command.rb

Overview

Use this module to define a default command to execute within a namespace. Convenience methods for a typical case.

@Example:

module Files
  extend Ing::DefaultCommand
  default_command :Export

  class Export
    ...
  end
  class Import
    ...
  end

end

Then from the command line this:

ing files [ARGS]

is equivalent to

ing files:export [ARGS]

and

ing list

will display

ing files  # Default command: export

PLEASE NOTE: extending your module with DefaultCommand will add state to your module: namely class instance variables @default_command and @shell, and also a default specify_options method (which will be overriden by any you define on the underlying module).

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#shellObject

Returns the value of attribute shell.



49
50
51
# File 'lib/ing/default_command.rb', line 49

def shell
  @shell
end

Instance Method Details

#call(*args) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
# File 'lib/ing/default_command.rb', line 56

def call(*args)
  raise ArgumentError, 
        "No default command set for `#{self}`. Did you call `default_command :Default` ?" \
    unless self.default_command
  Ing.execute(self.const_get(default_command, false), *args) do |cmd|
    cmd.shell = self.shell if cmd.respond_to?(:"shell=")
  end
end

#default_command(name = nil) ⇒ Object



44
45
46
47
# File 'lib/ing/default_command.rb', line 44

def default_command(name=nil)
  @default_command = name if name
  @default_command
end

#specify_options(parser) ⇒ Object



51
52
53
54
# File 'lib/ing/default_command.rb', line 51

def specify_options(parser)
  parser.text \
    "Default command: #{Ing::Util.encode_class_names([default_command])}"
end