Class: Ame::Method::Undefined

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

Overview

A Ame::Method in its undefined state. This class is used to construct the method before it gets defined, setting up a description, specifying that options_must_precede_arguments, adding flags, toggles, switches, options, multioptions, arguments, optional arguments, splats, spluses, and finally defining it.

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Undefined

Sets up an as yet undefined method on KLASS.

Parameters:



13
14
15
16
17
18
# File 'lib/ame-1.0/method/undefined.rb', line 13

def initialize(klass)
  @class = klass
  @description = nil
  @options = Ame::Options::Undefined.new
  @arguments = Ame::Arguments::Undefined.new
end

Instance Method Details

#argument(name, type, description) {|?| ... } ⇒ self

Delegates Class.argument to Arguments::Undefined#argument.

Parameters:

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

Yields:

  • (?)

Yield Parameters:

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

Returns:

  • (self)

Raises:

  • (ArgumentError)

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

  • (ArgumentError)

    If an optional argument has been defined

  • (ArgumentError)

    If a splat or splus argument has been defined



109
110
111
112
# File 'lib/ame-1.0/method/undefined.rb', line 109

def argument(name, type, description, &validate)
  @arguments.argument(name, type, description, &validate)
  self
end

#arguments?Boolean

Returns True if any arguments have been defined on the receiver.

Returns:

  • (Boolean)

    True if any arguments have been defined on the receiver



176
177
178
# File 'lib/ame-1.0/method/undefined.rb', line 176

def arguments?
  not @arguments.empty?
end

#define(ruby_name) ⇒ Method

Returns The method RUBY_NAME after adding a “help” flag that’ll display help via Class.help#method and raise AbortAllProcessing.

Returns:



161
162
163
164
165
166
167
# File 'lib/ame-1.0/method/undefined.rb', line 161

def define(ruby_name)
  flag '', 'help', nil, 'Display help for this method' do
    @class.help.method @class.methods[Ame::Method.name(ruby_name)]
    throw Ame::AbortAllProcessing
  end unless @options.include? 'help'
  Ame::Method.new(ruby_name, @class, @description, @options.define, @arguments.define)
end

#description(description = nil) ⇒ String

Sets the DESCRIPTION of the method, or returns it if DESCRIPTION is nil. The description is used in help output and similar circumstances.

Parameters:

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

Returns:

  • (String)


25
26
27
28
29
# File 'lib/ame-1.0/method/undefined.rb', line 25

def description(description = nil)
  return @description unless description
  @description = description
  self
end

#flag(short, long, default, description) {|?| ... } ⇒ self

Delegates Class.flag to Options::Undefined#flag.

Parameters:

  • short (String)
  • long (String)
  • default (Boolean)
  • description (String)

Yields:

  • (?)

Yield Parameters:

  • options (Hash<String, Object>)
  • value (Boolean)

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If SHORT or LONG have already been defined

  • (ArgumentError)

    If SHORT and LONG are #strip#empty?

  • (ArgumentError)

    If SHORT#strip#length > 1



47
48
49
50
# File 'lib/ame-1.0/method/undefined.rb', line 47

def flag(short, long, default, description, &validate)
  @options.flag short, long, default, description, &validate
  self
end

#multioption(short, long, argument, type, description) {|?| ... } ⇒ self

Delegates Class.multioption to Options::Undefined#multioption.

Parameters:

  • type (::Class)
  • argument (String)
  • default (Object)
  • argument_default (Object)
  • short (String)
  • long (String)
  • default (Boolean)
  • description (String)

Yields:

  • (?)

Yield Parameters:

  • options (Hash<String, Object>)
  • value (Object)

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If SHORT or LONG have already been defined

  • (ArgumentError)

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

  • (ArgumentError)

    If SHORT and LONG are #strip#empty?

  • (ArgumentError)

    If SHORT#strip#length > 1



95
96
97
98
# File 'lib/ame-1.0/method/undefined.rb', line 95

def multioption(short, long, argument, type, description, &validate)
  @options.multioption short, long, argument, type, description, &validate
  self
end

#option(short, long, argument, default, description) {|?| ... } ⇒ self

Delegates Class.option to Options::Undefined#option.

Parameters:

  • argument (String)
  • default (Object)
  • argument_default (Object)
  • short (String)
  • long (String)
  • default (Boolean)
  • description (String)

Yields:

  • (?)

Yield Parameters:

  • options (Hash<String, Object>)
  • value (Object)

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If SHORT or LONG have already been defined

  • (ArgumentError)

    If the type of DEFAULT isn’t one that Ame knows how to parse

  • (ArgumentError)

    If SHORT and LONG are #strip#empty?

  • (ArgumentError)

    If SHORT#strip#length > 1



83
84
85
86
# File 'lib/ame-1.0/method/undefined.rb', line 83

def option(short, long, argument, default, description, &validate)
  @options.option short, long, argument, default, description, &validate
  self
end

#option?(option) ⇒ Boolean

Returns True if OPTION has been defined on the receiver.

Parameters:

  • option (String)

Returns:

  • (Boolean)

    True if OPTION has been defined on the receiver



171
172
173
# File 'lib/ame-1.0/method/undefined.rb', line 171

def option?(option)
  @options.include? option
end

#optional(name, default, description) {|?| ... } ⇒ self

Parameters:

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

Yields:

  • (?)

Yield Parameters:

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

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If the type of DEFAULT isn’t one that Ame knows how to parse

  • (ArgumentError)

    If a splat or splus argument has been defined



124
125
126
127
# File 'lib/ame-1.0/method/undefined.rb', line 124

def optional(name, default, description, &validate)
  @arguments = @arguments.optional(name, default, description, &validate)
  self
end

#options_must_precede_argumentsself

Forces options to the method to precede any arguments to be processed correctly.

Returns:

  • (self)


35
36
37
38
# File 'lib/ame-1.0/method/undefined.rb', line 35

def options_must_precede_arguments
  @options.options_must_precede_arguments
  self
end

#splat(name, type, description) {|?| ... } ⇒ self

Delegates Class.argument to Arguments::Undefined#splat.

Parameters:

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

Yields:

  • (?)

Yield Parameters:

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

Returns:

  • (self)

Raises:

  • (ArgumentError)

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

  • (ArgumentError)

    If a splat or splus argument has been defined



138
139
140
141
# File 'lib/ame-1.0/method/undefined.rb', line 138

def splat(name, type, description, &validate)
  @arguments = @arguments.splat(name, type, description, &validate)
  self
end

#splus(name, type, description) {|?| ... } ⇒ self

Delegates Class.argument to Arguments::Undefined#splus.

Parameters:

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

Yields:

  • (?)

Yield Parameters:

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

Returns:

  • (self)

Raises:

  • (ArgumentError)

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

  • (ArgumentError)

    If an optional argument has been defined

  • (ArgumentError)

    If a splat or splus argument has been defined



153
154
155
156
# File 'lib/ame-1.0/method/undefined.rb', line 153

def splus(name, type, description, &validate)
  @arguments = @arguments.splus(name, type, description, &validate)
  self
end

#switch(short, long, argument, default, argument_default, description) {|?| ... } ⇒ self

Delegates Class.switch to Options::Undefined#switch.

Parameters:

  • argument (String)
  • default (Object)
  • argument_default (Object)
  • short (String)
  • long (String)
  • default (Boolean)
  • description (String)

Yields:

  • (?)

Yield Parameters:

  • options (Hash<String, Object>)
  • value (Object)

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If SHORT or LONG have already been defined

  • (ArgumentError)

    If the type of ARGUMENT_DEFAULT or, if ARGUMENT_DEFAULT is nil, DEFAULT isn’t one that Ame knows how to parse

  • (ArgumentError)

    If SHORT and LONG are #strip#empty?

  • (ArgumentError)

    If SHORT#strip#length > 1



71
72
73
74
# File 'lib/ame-1.0/method/undefined.rb', line 71

def switch(short, long, argument, default, argument_default, description, &validate)
  @options.switch short, long, argument, default, argument_default, description, &validate
  self
end

#toggle(short, long, default, description) {|?| ... } ⇒ self

Delegates Class.toggle to Options::Undefined#toggle.

Parameters:

  • short (String)
  • long (String)
  • default (Boolean)
  • description (String)

Yields:

  • (?)

Yield Parameters:

  • options (Hash<String, Object>)
  • value (Boolean)

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If LONG is #strip#empty?

  • (ArgumentError)

    If SHORT or LONG have already been defined

  • (ArgumentError)

    If SHORT and LONG are #strip#empty?

  • (ArgumentError)

    If SHORT#strip#length > 1



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

def toggle(short, long, default, description, &validate)
  @options.toggle short, long, default, description, &validate
  self
end

#valid?Boolean

Returns True if a description has been defined on the receiver.

Returns:

  • (Boolean)

    True if a description has been defined on the receiver



181
182
183
# File 'lib/ame-1.0/method/undefined.rb', line 181

def valid?
  not description.nil?
end