Class: Acclaim::Option::Arity

Inherits:
Object
  • Object
show all
Defined in:
lib/acclaim/option/arity.rb

Overview

Represents the the number of arguments an option can take, both mandatory and optional.

Since:

  • 0.0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Since:

  • 0.0.1



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

#minimumObject Also known as: required

The minimum number of arguments.

Since:

  • 0.0.1



11
12
13
# File 'lib/acclaim/option/arity.rb', line 11

def minimum
  @minimum
end

#optionalObject

The number of optional arguments.

Since:

  • 0.0.1



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

Since:

  • 0.0.1



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.

Returns:

  • (Boolean)

Since:

  • 0.0.1



43
44
45
# File 'lib/acclaim/option/arity.rb', line 43

def bound?
  not unlimited?
end

#hashObject

Equivalent to to_a.hash.

Since:

  • 0.0.1



68
69
70
# File 'lib/acclaim/option/arity.rb', line 68

def hash
  to_a.hash
end

#inspectString

Returns a string in the following format:

#<Acclaim::Option::Arity minimum +optional>

Returns:

  • (String)

    human-readable representation of this arity object

See Also:

Since:

  • 0.0.1



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.

Returns:

  • (Boolean)

Since:

  • 0.0.1



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.

Returns:

  • (Boolean)

Since:

  • 0.0.1



28
29
30
# File 'lib/acclaim/option/arity.rb', line 28

def only?(n)
  optional.zero? and minimum == n
end

#to_aObject Also known as: to_ary, to_array

Converts this arity to an array in the form of [ required, optional ].

Since:

  • 0.0.1



57
58
59
# File 'lib/acclaim/option/arity.rb', line 57

def to_a
  [ minimum, optional ]
end

#to_sString

Returns a string in the following format:

minimum +optional

The value of optional will be ∞ if this arity is not bound.

Returns:

  • (String)

    string representation of this arity

See Also:

Since:

  • 0.0.1



95
96
97
# File 'lib/acclaim/option/arity.rb', line 95

def to_s
  "#{minimum} +#{unlimited? ? '' : optional}"
end

#totalObject

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.

Since:

  • 0.0.1



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.

Returns:

  • (Boolean)

Since:

  • 0.0.1



38
39
40
# File 'lib/acclaim/option/arity.rb', line 38

def unlimited?
  optional < 0
end