Class: Engine::CardState
- Inherits:
-
Struct
- Object
- Struct
- Engine::CardState
- Defined in:
- lib/engine/card_state.rb
Overview
CardStates form a linked list, each pointing to the previous state plus the new data point. The actual factor/interval, streak are calculated recursively based on the previous value, and the new data point.
Constant Summary collapse
- STRATEGY =
AnkiStrategy
- EMPTY =
CardState.new
- START =
{ iteration: 0, streak: 0, factor: 2.5, interval: 1 }
Instance Attribute Summary collapse
-
#data_point ⇒ Object
Returns the value of attribute data_point.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #<<(data_point) ⇒ Object
- #data_points(&blk) ⇒ Object
- #empty? ⇒ Boolean
- #expired?(time) ⇒ Boolean
- #expired_for_seconds(time) ⇒ Object
- #factor ⇒ Object
- #inspect ⇒ Object
- #interval ⇒ Object
- #iteration ⇒ Object
- #last_shown ⇒ Object
- #strategy ⇒ Object
- #streak ⇒ Object
Instance Attribute Details
#data_point ⇒ Object
Returns the value of attribute data_point
6 7 8 |
# File 'lib/engine/card_state.rb', line 6 def data_point @data_point end |
#parent ⇒ Object
Returns the value of attribute parent
6 7 8 |
# File 'lib/engine/card_state.rb', line 6 def parent @parent end |
Instance Method Details
#<<(data_point) ⇒ Object
17 18 19 |
# File 'lib/engine/card_state.rb', line 17 def << data_point self.class.new(self, data_point) end |
#data_points(&blk) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/engine/card_state.rb', line 29 def data_points(&blk) return to_enum(__method__) unless block_given? unless empty? parent.data_points(&blk) yield data_point end end |
#empty? ⇒ Boolean
25 26 27 |
# File 'lib/engine/card_state.rb', line 25 def empty? parent.nil? end |
#expired?(time) ⇒ Boolean
41 42 43 |
# File 'lib/engine/card_state.rb', line 41 def expired?(time) (!empty? && data_point.fail?) || expired_for_seconds(time) > 0 end |
#expired_for_seconds(time) ⇒ Object
45 46 47 |
# File 'lib/engine/card_state.rb', line 45 def expired_for_seconds(time) empty? ? 0 : [time - last_shown - interval * 60, 0].max end |
#factor ⇒ Object
51 |
# File 'lib/engine/card_state.rb', line 51 def factor ; empty? ? START[:factor] : strategy.next_factor end |
#inspect ⇒ Object
54 55 56 |
# File 'lib/engine/card_state.rb', line 54 def inspect "<#{self.class} iteration: %d, streak: %d, factor: %.2f, interval: %.2f>" % [iteration, streak, factor, interval] end |
#interval ⇒ Object
52 |
# File 'lib/engine/card_state.rb', line 52 def interval ; empty? ? START[:interval] : strategy.next_interval end |
#iteration ⇒ Object
49 |
# File 'lib/engine/card_state.rb', line 49 def iteration ; empty? ? START[:iteration] : parent.iteration + 1 end |
#last_shown ⇒ Object
37 38 39 |
# File 'lib/engine/card_state.rb', line 37 def last_shown data_points.to_a.last. end |
#strategy ⇒ Object
21 22 23 |
# File 'lib/engine/card_state.rb', line 21 def strategy STRATEGY.new(parent, data_point) end |
#streak ⇒ Object
50 |
# File 'lib/engine/card_state.rb', line 50 def streak ; empty? ? START[:streak] : strategy.next_streak end |