Class: Lexdrill::WordList

Inherits:
Object
  • Object
show all
Defined in:
lib/lexdrill/word_list.rb

Overview

Reads vocabulary words/phrases from a .drill.txt file, one per line, and advances a persisted counter to show "the current word" each time. On a fresh install with no project-local or LEXDRILL_PATH-overridden list, seeds ~/.drill.txt with a default starter list instead of coming up empty. Words that have graduated (see Lexdrill::Stats) are excluded from .next but remain in .words for list/stats to report.

Constant Summary collapse

PATH =
Lexdrill::Config::DRILL_PATH
COUNTER_PATH =
Lexdrill::Config::COUNTER_PATH
HOME_PATH =
Lexdrill::Config.home_drill_path

Class Method Summary collapse

Class Method Details

.active_wordsObject



36
37
38
# File 'lib/lexdrill/word_list.rb', line 36

def self.active_words
  words.reject { |word| Lexdrill::Stats.graduated?(word) }
end

.nextObject



27
28
29
30
31
32
33
34
# File 'lib/lexdrill/word_list.rb', line 27

def self.next
  active = active_words
  return nil if active.empty?

  word = Lexdrill::Beat.rand? ? active.sample : active[take_index(active.size)]
  Lexdrill::Stats.record(word)
  word
end

.take_index(size) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/lexdrill/word_list.rb', line 40

def self.take_index(size)
  counter = Lexdrill::Counter.new(COUNTER_PATH)
  cycle_length = Lexdrill::Beat.cycle_length(size)
  step = counter.bounded_value(cycle_length)
  counter.increment
  Lexdrill::Beat.index_for(size, step)
end

.wordsObject



14
15
16
17
18
# File 'lib/lexdrill/word_list.rb', line 14

def self.words
  seed_default_at_home
  @words ||= File.exist?(PATH) && File.readlines(PATH, encoding: "UTF-8").map(&:strip).reject(&:empty?)
  @words ||= []
end