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



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

#minimumObject Also known as: required

The minimum number of arguments.

Since:

  • 0.0.1



9
10
11
# File 'lib/acclaim/option/arity.rb', line 9

def minimum
  @minimum
end

#optionalObject

The number of optional arguments.

Since:

  • 0.0.1



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

Since:

  • 0.0.1



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.

Returns:

  • (Boolean)

Since:

  • 0.0.1



41
42
43
# File 'lib/acclaim/option/arity.rb', line 41

def bound?
  not unlimited?
end

#hashObject

Equivalent to to_a.hash.

Since:

  • 0.0.1



66
67
68
# File 'lib/acclaim/option/arity.rb', line 66

def hash
  to_a.hash
end

#inspectObject

Returns the output of #to_s, enclosed in angle brackets ('<' and '>').

Since:

  • 0.0.1



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.

Returns:

  • (Boolean)

Since:

  • 0.0.1



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.

Returns:

  • (Boolean)

Since:

  • 0.0.1



26
27
28
# File 'lib/acclaim/option/arity.rb', line 26

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



55
56
57
# File 'lib/acclaim/option/arity.rb', line 55

def to_a
  [ minimum, optional ]
end

#to_sObject

Returns a string in the following format:

Arity: minimum +optional

The value of optional will be 'infinite' if #unlimited? is true.

Since:

  • 0.0.1



91
92
93
# File 'lib/acclaim/option/arity.rb', line 91

def to_s
  "Arity: #{minimum} +#{unlimited? ? 'infinite' : 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



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.

Returns:

  • (Boolean)

Since:

  • 0.0.1



36
37
38
# File 'lib/acclaim/option/arity.rb', line 36

def unlimited?
  optional < 0
end