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



29
30
31
32
33
# File 'lib/spoonerize/bumper.rb', line 29

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)


17
18
19
# File 'lib/spoonerize/bumper.rb', line 17

def max_value
  @max_value
end

#valueInteger (readonly)

The number after being bumped.

Returns:

  • (Integer)


11
12
13
# File 'lib/spoonerize/bumper.rb', line 11

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)


43
44
45
# File 'lib/spoonerize/bumper.rb', line 43

def bump
  @value = bump_value(value)
end

#reverse?Boolean

Should we decrement instead of increment?

Returns:

  • (Boolean)


51
52
53
# File 'lib/spoonerize/bumper.rb', line 51

def reverse?
  @reverse
end