Class: Ame::Class

Inherits:
Object
  • Object
show all
Defined in:
lib/ame-1.0/class.rb

Overview

The superclass of a Ruby class that wants to be able to be invoked from the command line (or with any list of String options and arguments). Subclassed by Root, which should be used as the root of any command-line processing interface. See Root for an example.

Direct Known Subclasses

Root

Class Method Summary collapse

Class Method Details

.basename(basename = nil) ⇒ String

Sets or returns, depending on if BASENAME is nil or not, the basename of the receiver. The basename is the downcased last component of the double-colon-separated name of the class with all camel-cased sub-words separated by dashes.

Examples:

Basename of A::B::CToTheD

class A::B::CToTheD < Ame::Class; end
A::B::CToTheD.basename # ⇒ "c-to-the-d"

Parameters:

  • basename (String, nil) (defaults to: nil)

Returns:

  • (String)


387
388
389
390
391
# File 'lib/ame-1.0/class.rb', line 387

def basename(basename = nil)
  @basename = basename if basename
  return @basename if defined? @basename
  name.split('::').last.scan(/[[:upper:]][[:lower:]]*/).join('-').downcase
end

.call(method, arguments = nil, options = nil) ⇒ self

Call METHOD with ARGUMENTS and OPTIONS on a new instance of the receiver. This method catches AbortProcessing. Options are arguments that begin with ‘-` or `–`. If options_must_precede_arguments has been called on the receiver, then options must precede arguments. Either way, a `–` argument will always end the processing of options and begin processing of arguments instead.

Parameters:

  • method (#to_sym)
  • arguments (Array) (defaults to: nil)
  • options (Hash<String, Object>) (defaults to: nil)

Returns:

  • (self)

Raises:



42
43
44
45
46
47
# File 'lib/ame-1.0/class.rb', line 42

def call(method, arguments = nil, options = nil)
  catch Ame::AbortProcessing do
    methods[method].call new, arguments, options
  end
  self
end

.description(description = nil) ⇒ String

Sets the DESCRIPTION of the method about to be defined, or returns it if DESCRIPTION is nil. The description is used in help output and similar circumstances. A description can only be given to a public method, as only public methods can be used as Ame methods.

Examples:

Set The Description of the Format-patch Method

class Git::CLI::Git::FormatPatch < Ame::Class
  description 'Prepare patches for e-mail submission'
  def format_patch

Parameters:

  • description (String, nil) (defaults to: nil)

Returns:

  • (String)


59
60
61
62
# File 'lib/ame-1.0/class.rb', line 59

def description(description = nil)
  return method.description(description) if description
  defined?(@description) ? @description : ''
end

.fullnameString

Returns The full name of the space-separated concatenation of the basenames of the receiver and its parents.

Examples:

Fullname of A::B::CToTheD

class A::B::CToTheD < Ame::Class; end
A::B::CToTheD.fullname # ⇒ "a b c-to-the-d"

Returns:

  • (String)

    The full name of the space-separated concatenation of the basenames of the receiver and its parents



399
400
401
402
403
404
405
406
407
# File 'lib/ame-1.0/class.rb', line 399

def fullname
  [].tap{ |names|
    klass = self
    until klass.nil? or klass.basename.empty?
      names << klass.basename
      klass = klass.parent
    end
  }.reverse.join(' ')
end

.help(help = nil) ⇒ #method, ...

Sets the HELP object to use for displaying usage information, or returns it if HELP is nil. The default is to delegate the request to the parent.

Parameters:

  • help (#method, #dispatch, #error, #version) (defaults to: nil)

Returns:

  • (#method, #dispatch, #error, #version)


372
373
374
375
# File 'lib/ame-1.0/class.rb', line 372

def help(help = nil)
  return @help = help if help
  @help ||= Ame::Help::Delegate.new(parent.help)
end

.methodsMethods

Returns The methods defined on the receiver.

Returns:

  • (Methods)

    The methods defined on the receiver



411
412
413
# File 'lib/ame-1.0/class.rb', line 411

def methods
  @methods ||= Ame::Methods.new
end

.process(method, arguments = []) ⇒ self

Process ARGUMENTS as a list of options and arguments, then call METHOD with the results of this processing on a new instance of the receiver. This method catches AbortProcessing. Options are arguments that begin with ‘-` or `–`. If options_must_precede_arguments has been called on the receiver, then options must precede arguments. Either way, a `–` argument will always end the processing of options and begin processing of arguments instead.

Parameters:

  • method (#to_sym)
  • arguments (Array<String>) (defaults to: [])

Returns:

  • (self)

Raises:



22
23
24
25
26
27
# File 'lib/ame-1.0/class.rb', line 22

def process(method, arguments = [])
  catch Ame::AbortProcessing do
    methods[method].process new, arguments
  end
  self
end