Class: Spoonerize::Bumper

Inherits:
Object
  • Object
show all
Defined in:
lib/spoonerize/bumper.rb

Overview

Class that handles bumping an index.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_value, max_value, reverse = false) ⇒ Bumper

Sets the bumper relative to the current index of words array. The value is automatically bumped once when instantiated. If on the last element of the words array, sets the bumper to 0.

Parameters:

  • initial_value (Integer)
  • max_value (Integer)
  • reverse (Boolean) (defaults to: false)

    Flip the order



27
28
29
30
31
# File 'lib/spoonerize/bumper.rb', line 27

def initialize(initial_value, max_value, reverse = false)
  @max_value = max_value
  @reverse = reverse
  @value = bump_value(initial_value)
end

Instance Attribute Details

#max_valueInteger (readonly)

The maximum before index wraps back to zero.

Returns:

  • (Integer)


15
16
17
# File 'lib/spoonerize/bumper.rb', line 15

def max_value
  @max_value
end

#valueInteger (readonly)

The number after being bumped.

Returns:

  • (Integer)


9
10
11
# File 'lib/spoonerize/bumper.rb', line 9

def value
  @value
end

Instance Method Details

#bumpInteger

Increments/Decrements the bumper. If on the last element of the words array, sets the bumper to 0. We don’t need to worry about resetting the value to 0 when going in reverse, because when the array index is negative, it reads from the end of the array, which is already what we want.

Returns:

  • (Integer)


41
42
43
# File 'lib/spoonerize/bumper.rb', line 41

def bump
  @value = bump_value(value)
end

#reverse?Boolean

Should we decrement instead of increment?

Returns:

  • (Boolean)


49
50
51
# File 'lib/spoonerize/bumper.rb', line 49

def reverse?
  @reverse
end