Sha256: e4cd5fe2ce9ce26638923f8d6eb524ca62ef8d2e207d183af5b8a3a667395db6

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module ActiveRecall
  class SoftLeitnerSystem
    def self.required_attributes
      REQUIRED_ATTRIBUTES
    end

    def self.right(box:, times_right:, times_wrong:, current_time: Time.current)
      new(
        box: box,
        current_time: current_time,
        times_right: times_right,
        times_wrong: times_wrong
      ).right
    end

    def self.type
      :binary
    end

    def self.wrong(box:, times_right:, times_wrong:, current_time: Time.current)
      new(
        box: box,
        current_time: current_time,
        times_right: times_right,
        times_wrong: times_wrong
      ).wrong
    end

    def initialize(box:, times_right:, times_wrong:, current_time: Time.current)
      @box = box
      @current_time = current_time
      @times_right = times_right
      @times_wrong = times_wrong
    end

    def right
      self.box = [box + 1, DELAYS.count].min

      {
        box: box,
        times_right: times_right + 1,
        times_wrong: times_wrong,
        last_reviewed: current_time,
        next_review: next_review
      }
    end

    def wrong
      self.box = [box - 1, 0].max

      {
        box: box,
        times_right: times_right,
        times_wrong: times_wrong + 1,
        last_reviewed: current_time,
        next_review: next_review
      }
    end

    private

    DELAYS = [3, 7, 14, 30, 60, 120, 240].freeze
    REQUIRED_ATTRIBUTES = [:box, :times_right, :times_wrong].freeze

    attr_accessor :box
    attr_reader :current_time, :times_right, :times_wrong

    def next_review
      (current_time + DELAYS[[DELAYS.count, box].min - 1].days)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_recall-2.1.0 lib/active_recall/algorithms/soft_leitner_system.rb
active_recall-2.0.2 lib/active_recall/algorithms/soft_leitner_system.rb
active_recall-2.0.1 lib/active_recall/algorithms/soft_leitner_system.rb
active_recall-2.0.0 lib/active_recall/algorithms/soft_leitner_system.rb