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.

Author:

  • Matheus Afonso Martins Moreira

Since:

  • 0.0.2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(minimum = 0, optional = 0) ⇒ Arity

Initializes an arity with the given number of required and optional parameters. If the latter is less than zero, then it means the option may take infinite parameters, as long as it takes the minimum number of parameters.

Parameters:

  • minimum (Integer) (defaults to: 0)

    the number of mandatory parameters

  • optional (Integer) (defaults to: 0)

    the number of optional parameters

Since:

  • 0.0.2



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

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.2



14
15
16
# File 'lib/acclaim/option/arity.rb', line 14

def minimum
  @minimum
end

#optionalObject

The number of optional arguments.

Since:

  • 0.0.2



17
18
19
# File 'lib/acclaim/option/arity.rb', line 17

def optional
  @optional
end

Instance Method Details

#==(arity) ⇒ Object Also known as: ===

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.2



100
101
102
# File 'lib/acclaim/option/arity.rb', line 100

def ==(arity)
  to_a == arity.to_a
end

#bound?true, false

Whether the option must take a finite number of arguments.

Returns:

  • (true, false)

    whether the maximum number of arguments is limited

See Also:

Since:

  • 0.0.2



61
62
63
# File 'lib/acclaim/option/arity.rb', line 61

def bound?
  not unlimited?
end

#eql?(arity) ⇒ true, false

Same as == but does not compare directly with arrays.

Parameters:

  • arity (Arity)

    the arity to compare this instance to

Returns:

  • (true, false)

    whether this instance is equal to the given arity

Since:

  • 0.0.2



110
111
112
# File 'lib/acclaim/option/arity.rb', line 110

def eql?(arity)
  minimum == arity.minimum and optional == arity.optional
end

#hashInteger

Equivalent to:

to_a.hash

Returns:

  • (Integer)

    hash value for this arity

Since:

  • 0.0.2



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

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.2



132
133
134
# File 'lib/acclaim/option/arity.rb', line 132

def inspect
  "#<#{self.class} #{to_s}>"
end

#only?(n) ⇒ true, false

Whether the option takes n and only n parameters.

Parameters:

  • n (Integer)

    the number of parameters

Returns:

  • (true, false)

    whether the option takes only n parameters

Since:

  • 0.0.2



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

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

#to_aArray<Integer> Also known as: to_ary, to_array

Converts this arity to an array in the form of:

[ required, optional ]

Returns:

  • (Array<Integer>)

    number of required and optional parameters in order

Since:

  • 0.0.2



79
80
81
# File 'lib/acclaim/option/arity.rb', line 79

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.2



122
123
124
# File 'lib/acclaim/option/arity.rb', line 122

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

#totalInteger?

The total number of parameters that the option may take, or nil if the option can take an infinite number of arguments.

Returns:

  • (Integer, nil)

    the total number of parameters or nil if infinite

Since:

  • 0.0.2



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

def total
  if bound? then minimum + optional else nil end
end

#unlimited?true, false

Whether the option can take an unlimited number of arguments.

Returns:

  • (true, false)

    whether the option can take infinite parameters

Since:

  • 0.0.2



53
54
55
# File 'lib/acclaim/option/arity.rb', line 53

def unlimited?
  optional < 0
end

#zero?true, false Also known as: none?

Whether the option takes no parameters.

Returns:

  • (true, false)

    whether the option takes zero parameters

Since:

  • 0.5.1



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

def zero?
  only? 0
end