Class: Generations
- Inherits:
-
Array
- Object
- Array
- Generations
- Defined in:
- lib/life_game_viewer/view/generations.rb
Instance Attribute Summary collapse
-
#current_num ⇒ Object
Returns the value of attribute current_num.
-
#last_num ⇒ Object
readonly
Returns the value of attribute last_num.
Instance Method Summary collapse
- #at_first_generation? ⇒ Boolean
- #at_last_generation? ⇒ Boolean
- #current ⇒ Object
- #ensure_next_in_cache ⇒ Object
- #first ⇒ Object
- #found_last_generation? ⇒ Boolean
-
#initialize(life_model) ⇒ Generations
constructor
A new instance of Generations.
- #last ⇒ Object
- #next ⇒ Object
- #previous ⇒ Object
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_num ⇒ Object
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_num ⇒ Object (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
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
21 22 23 |
# File 'lib/life_game_viewer/view/generations.rb', line 21 def at_last_generation? current_num == @last_num end |
#current ⇒ Object
13 14 15 |
# File 'lib/life_game_viewer/view/generations.rb', line 13 def current self[current_num] end |
#ensure_next_in_cache ⇒ Object
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 |
#first ⇒ Object
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
17 18 19 |
# File 'lib/life_game_viewer/view/generations.rb', line 17 def found_last_generation? !!@last_num end |
#last ⇒ Object
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 |
#next ⇒ Object
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 |
#previous ⇒ Object
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 |