Class: Ame::Argument

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

Overview

Represents an argument to a Method. It has a #name and a #description. It will be called upon to process an argument before the method this argument is associated with gets called.

Direct Known Subclasses

Optional, Splus

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, description) {|?| ... } ⇒ Argument

Returns a new instance of Argument.

Parameters:

  • name (String)
  • type (::Class)
  • description (String)

Yields:

  • (?)

Yield Parameters:

  • options (Hash<String, Object>)
  • processed (Array<String>)
  • argument (Object)

Raises:

  • (ArgumentError)

    If TYPE isn’t one that Ame knows how to parse



17
18
19
20
# File 'lib/ame-1.0/argument.rb', line 17

def initialize(name, type, description, &validate)
  @name, @description, @validate = name, description, validate || proc{ |_, _, a| a }
  @type = Ame::Types[[type, String].reject(&:nil?).first]
end

Instance Attribute Details

#descriptionString (readonly)

Returns The description of the receiver.

Returns:

  • (String)

    The description of the receiver



26
27
28
# File 'lib/ame-1.0/argument.rb', line 26

def description
  @description
end

#nameString (readonly)

Returns The name of the receiver.

Returns:

  • (String)

    The name of the receiver



23
24
25
# File 'lib/ame-1.0/argument.rb', line 23

def name
  @name
end

Instance Method Details

#process(options, processed, arguments) ⇒ Object

Invokes the optional block passed to the receiver when it was created for additional validation and parsing after optionally parsing one or more of the ARGUMENTS passed to it (see subclasses’ #parse methods for their behaviour).

Parameters:

  • options (Hash<String, Object>)
  • processed (Array<String>)
  • arguments (Array<String>)

Returns:

  • (Object)

Raises:



44
45
46
47
48
# File 'lib/ame-1.0/argument.rb', line 44

def process(options, processed, arguments)
  @validate.call(options, processed, parse(arguments))
rescue Ame::MalformedArgument, ArgumentError, TypeError => e
  raise Ame::MalformedArgument, '%s: %s' % [self, e]
end

#to_sString

Returns The upcasing of the #name of the receiver.

Returns:

  • (String)

    The upcasing of the #name of the receiver



29
30
31
# File 'lib/ame-1.0/argument.rb', line 29

def to_s
  @to_s ||= name.upcase
end