Class: Lexdrill::WordList

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

Overview

Reads vocabulary words/phrases from the words file, one per line, and advances a persisted counter to show "the current word" each time. On a fresh install, seeds the file 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.path("words")
COUNTER_PATH =
Lexdrill::Config.path("counter")

Class Method Summary collapse

Class Method Details

.active_wordsObject



34
35
36
# File 'lib/lexdrill/word_list.rb', line 34

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

.nextObject



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

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



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

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



12
13
14
15
16
# File 'lib/lexdrill/word_list.rb', line 12

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