Class: Rain::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/matrix/cursor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCursor

Returns a new instance of Cursor.



7
8
9
10
11
# File 'lib/matrix/cursor.rb', line 7

def initialize
  @index = -1
  @first_update = now
  @last_update = now
end

Instance Attribute Details

#first_updateObject

Returns the value of attribute first_update.



5
6
7
# File 'lib/matrix/cursor.rb', line 5

def first_update
  @first_update
end

#indexObject

Returns the value of attribute index.



5
6
7
# File 'lib/matrix/cursor.rb', line 5

def index
  @index
end

#last_updateObject

Returns the value of attribute last_update.



5
6
7
# File 'lib/matrix/cursor.rb', line 5

def last_update
  @last_update
end

Instance Method Details

#increase_index(loop_count:) ⇒ Object



34
35
36
37
# File 'lib/matrix/cursor.rb', line 34

def increase_index(loop_count:)
  @index += 1
  @index = 0 if @index >= loop_count
end

#increment(delays:, inputs:, duration: nil) {|index| ... } ⇒ Object

Unit tests use “duration” to skip forward in time, while feature tests and the real world use old fashioned linear time.

Yields:



14
15
16
17
18
19
20
21
22
23
# File 'lib/matrix/cursor.rb', line 14

def increment(delays:, inputs:, duration: nil)
  next_index = next_index(loop_count: inputs.count)

  return unless delays[next_index] && (duration || now - @last_update) >= delays[next_index]

  @index = next_index
  @last_update = now

  yield index
end

#iterate(inputs:, loop_count:) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/matrix/cursor.rb', line 25

def iterate(inputs:, loop_count:)
  inputs.each do |input|
    @index += 1
    @index = 0 if index >= loop_count

    yield index, input
  end
end

#next_index(loop_count:) ⇒ Object



39
40
41
42
43
# File 'lib/matrix/cursor.rb', line 39

def next_index(loop_count:)
  next_index = @index + 1
  next_index = 0 if next_index >= loop_count
  next_index
end