class Ame::Argument

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. @api developer

Attributes

description[R]

@return [String] The description of the receiver

name[R]

@return [String] The name of the receiver

Public Class Methods

new(name, type, description, &validate) click to toggle source

@api internal @param [String] name @param [::Class] type @param [String] description @yield [?] @yieldparam [Hash<String, Object>] options @yieldparam [Array<String>] processed @yieldparam [Object] argument @raise [ArgumentError] If TYPE isn’t one that Ame knows how to parse

# File lib/ame-1.0/argument.rb, line 16
def initialize(name, type, description, &validate)
  @name, @description, @validate = name, description, validate || proc{ |_, _, a| a }
  @type = Ame::Types[[type, String].reject(&:nil?).first]
end

Public Instance Methods

process(options, processed, arguments) click to toggle source

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). @api internal @param [Hash<String, Object>] options @param [Array<String>] processed @param [Array<String>] arguments @raise [MissingArgument] If a required argument is missing @raise [MalformedArgument] If an argument can’t be parsed @return [Object]

# File lib/ame-1.0/argument.rb, line 43
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_s() click to toggle source

@return [String] The upcasing of the {#name} of the receiver

# File lib/ame-1.0/argument.rb, line 28
def to_s
  @to_s ||= name.upcase
end

Private Instance Methods

parse(arguments) click to toggle source

Returns the parsed value of the result of ARGUMENTS#shift Should be overridden by subclasses that want different behaviour for missing arguments. @api internal @param [Array<String>] arguments @return [Object] The parsed value of the result of ARGUMENTS#shift @raise [MissingArgument] If ARGUMENTS#empty? @raise [MalformedArgument] If ARGUMENTS#shift is non-nil and can’t be parsed

# File lib/ame-1.0/argument.rb, line 59
def parse(arguments)
  @type.parse(arguments.shift || raise(Ame::MissingArgument, 'missing argument: %s' % self))
end