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 ⇒ String
Returns a string in the following format:.
-
#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 ⇒ String
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.
23 24 25 |
# File 'lib/acclaim/option/arity.rb', line 23 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.
11 12 13 |
# File 'lib/acclaim/option/arity.rb', line 11 def minimum @minimum end |
#optional ⇒ Object
The number of optional arguments.
14 15 16 |
# File 'lib/acclaim/option/arity.rb', line 14 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
77 78 79 |
# File 'lib/acclaim/option/arity.rb', line 77 def ==(arity) to_a == arity.to_a end |
#bound? ⇒ Boolean
Returns true
if the option must take a finite number of arguments.
43 44 45 |
# File 'lib/acclaim/option/arity.rb', line 43 def bound? not unlimited? end |
#hash ⇒ Object
Equivalent to to_a.hash
.
68 69 70 |
# File 'lib/acclaim/option/arity.rb', line 68 def hash to_a.hash end |
#inspect ⇒ String
Returns a string in the following format:
#<Acclaim::Option::Arity minimum +optional>
105 106 107 |
# File 'lib/acclaim/option/arity.rb', line 105 def inspect "#<#{self.class} #{to_s}>" end |
#none? ⇒ Boolean
Returns true
if the option takes no parameters.
33 34 35 |
# File 'lib/acclaim/option/arity.rb', line 33 def none? only? 0 end |
#only?(n) ⇒ Boolean
Returns true
if the option takes n
and only n
parameters.
28 29 30 |
# File 'lib/acclaim/option/arity.rb', line 28 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 ]
.
57 58 59 |
# File 'lib/acclaim/option/arity.rb', line 57 def to_a [ minimum, optional ] end |
#to_s ⇒ String
Returns a string in the following format:
minimum +optional
The value of optional will be ∞ if this arity is not bound.
95 96 97 |
# File 'lib/acclaim/option/arity.rb', line 95 def to_s "#{minimum} +#{unlimited? ? '∞' : 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.
51 52 53 |
# File 'lib/acclaim/option/arity.rb', line 51 def total bound? ? minimum + optional : nil end |
#unlimited? ⇒ Boolean
Returns true
if the option can take an infinite number of arguments.
38 39 40 |
# File 'lib/acclaim/option/arity.rb', line 38 def unlimited? optional < 0 end |