Class: Acclaim::Option::Arity
- Inherits:
-
Object
- Object
- Acclaim::Option::Arity
- Defined in:
- lib/acclaim/option/arity.rb
Overview
Represents the the number of arguments an option can take, both mandatory and optional.
Instance Attribute Summary collapse
-
#minimum ⇒ Object
(also: #required)
The minimum number of arguments.
-
#optional ⇒ Object
The number of optional arguments.
Instance Method Summary collapse
-
#==(arity) ⇒ Object
(also: #eql?, #===)
Converts both
self
andarity
to an array and compares them. -
#bound? ⇒ Boolean
Returns
true
if the option must take a finite number of arguments. -
#hash ⇒ Object
Equivalent to
to_a.hash
. -
#initialize(minimum = 0, optional = 0) ⇒ Arity
constructor
Initializes this arity with a number of required parameters and a number of optional parameters.
-
#inspect ⇒ Object
Returns the output of #to_s, enclosed in angle brackets (
'<'
and'>'
). -
#none? ⇒ Boolean
Returns
true
if the option takes no parameters. -
#only?(n) ⇒ Boolean
Returns
true
if the option takesn
and onlyn
parameters. -
#to_a ⇒ Object
(also: #to_ary, #to_array)
Converts this arity to an array in the form of
[ required, optional ]
. -
#to_s ⇒ Object
Returns a string in the following format:.
-
#total ⇒ Object
Returns the total number of parameters that the option may take, which is the number of mandatory parameters plus the number of optional parameters.
-
#unlimited? ⇒ Boolean
Returns
true
if the option can take an infinite number of arguments.
Constructor Details
#initialize(minimum = 0, optional = 0) ⇒ Arity
Initializes this arity with a number of required parameters and a number of optional parameters. If the latter is less than zero, then it means the option may take infinite parameters, as long as it takes at least minimum
parameters.
21 22 23 |
# File 'lib/acclaim/option/arity.rb', line 21 def initialize(minimum = 0, optional = 0) @minimum, @optional = minimum, optional end |
Instance Attribute Details
#minimum ⇒ Object Also known as: required
The minimum number of arguments.
9 10 11 |
# File 'lib/acclaim/option/arity.rb', line 9 def minimum @minimum end |
#optional ⇒ Object
The number of optional arguments.
12 13 14 |
# File 'lib/acclaim/option/arity.rb', line 12 def optional @optional end |
Instance Method Details
#==(arity) ⇒ Object Also known as: eql?, ===
Converts both self
and arity
to an array and compares them. This is so that comparing directly with an array is possible:
Arity.new(1, 3) == [1, 3]
=> true
75 76 77 |
# File 'lib/acclaim/option/arity.rb', line 75 def ==(arity) to_a == arity.to_a end |
#bound? ⇒ Boolean
Returns true
if the option must take a finite number of arguments.
41 42 43 |
# File 'lib/acclaim/option/arity.rb', line 41 def bound? not unlimited? end |
#hash ⇒ Object
Equivalent to to_a.hash
.
66 67 68 |
# File 'lib/acclaim/option/arity.rb', line 66 def hash to_a.hash end |
#inspect ⇒ Object
Returns the output of #to_s, enclosed in angle brackets ('<'
and '>'
).
97 98 99 |
# File 'lib/acclaim/option/arity.rb', line 97 def inspect "<#{to_s}>" end |
#none? ⇒ Boolean
Returns true
if the option takes no parameters.
31 32 33 |
# File 'lib/acclaim/option/arity.rb', line 31 def none? only? 0 end |
#only?(n) ⇒ Boolean
Returns true
if the option takes n
and only n
parameters.
26 27 28 |
# File 'lib/acclaim/option/arity.rb', line 26 def only?(n) optional.zero? and minimum == n end |
#to_a ⇒ Object Also known as: to_ary, to_array
Converts this arity to an array in the form of [ required, optional ]
.
55 56 57 |
# File 'lib/acclaim/option/arity.rb', line 55 def to_a [ minimum, optional ] end |
#to_s ⇒ Object
Returns a string in the following format:
Arity: minimum +optional
The value of optional
will be 'infinite'
if #unlimited? is true
.
91 92 93 |
# File 'lib/acclaim/option/arity.rb', line 91 def to_s "Arity: #{minimum} +#{unlimited? ? 'infinite' : optional}" end |
#total ⇒ Object
Returns the total number of parameters that the option may take, which is the number of mandatory parameters plus the number of optional parameters. Returns nil
if the option may take an infinite number of parameters.
49 50 51 |
# File 'lib/acclaim/option/arity.rb', line 49 def total bound? ? minimum + optional : nil end |
#unlimited? ⇒ Boolean
Returns true
if the option can take an infinite number of arguments.
36 37 38 |
# File 'lib/acclaim/option/arity.rb', line 36 def unlimited? optional < 0 end |