Class: Generations

Inherits:
Array
  • Object
show all
Defined in:
lib/life_game_viewer/view/generations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(life_model) ⇒ Generations

Returns a new instance of Generations.



6
7
8
9
10
11
# File 'lib/life_game_viewer/view/generations.rb', line 6

def initialize(life_model)
  self << life_model
  @current_num = 0
  @last_num = nil
  ensure_next_in_cache
end

Instance Attribute Details

#current_numObject

Returns the value of attribute current_num.



4
5
6
# File 'lib/life_game_viewer/view/generations.rb', line 4

def current_num
  @current_num
end

#last_numObject (readonly)

Returns the value of attribute last_num.



3
4
5
# File 'lib/life_game_viewer/view/generations.rb', line 3

def last_num
  @last_num
end

Instance Method Details

#at_first_generation?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/life_game_viewer/view/generations.rb', line 25

def at_first_generation?
  current_num == 0
end

#at_last_generation?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/life_game_viewer/view/generations.rb', line 21

def at_last_generation?
  current_num == @last_num
end

#currentObject



13
14
15
# File 'lib/life_game_viewer/view/generations.rb', line 13

def current
  self[current_num]
end

#ensure_next_in_cacheObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/life_game_viewer/view/generations.rb', line 29

def ensure_next_in_cache
  at_end_of_array = (current_num == (size - 1))
  need_to_get_new_model = at_end_of_array && (! found_last_generation?)
  if need_to_get_new_model
    tentative_next = current.next_generation_model
    if tentative_next == current
      @last_num = current_num
    else
      self << tentative_next
    end

  end
end

#firstObject



56
57
58
59
# File 'lib/life_game_viewer/view/generations.rb', line 56

def first
  self.current_num = 0
  current
end

#found_last_generation?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/life_game_viewer/view/generations.rb', line 17

def found_last_generation?
  !!@last_num
end

#lastObject



61
62
63
64
65
66
# File 'lib/life_game_viewer/view/generations.rb', line 61

def last
  until at_last_generation?
    self.next
  end
  current
end

#nextObject



43
44
45
46
47
48
# File 'lib/life_game_viewer/view/generations.rb', line 43

def next
  raise "Next was called when at end of lineage." if at_last_generation?
  self.current_num = current_num + 1
  ensure_next_in_cache
  current
end

#previousObject



50
51
52
53
54
# File 'lib/life_game_viewer/view/generations.rb', line 50

def previous
  raise "Previous was called when at first generation." if at_first_generation?
  self.current_num = current_num - 1
  current
end