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.

Constant Summary collapse

PATH =
Lexdrill::Config::DRILL_PATH
COUNTER_PATH =
Lexdrill::Config::COUNTER_PATH

Class Method Summary collapse

Class Method Details

.nextObject



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

def self.next
  return nil if words.empty?

  words[take_index(words.size)]
end

.take_index(size) ⇒ Object



20
21
22
23
24
25
# File 'lib/lexdrill/word_list.rb', line 20

def self.take_index(size)
  counter = Lexdrill::Counter.new(COUNTER_PATH)
  index = counter.bounded_value(size)
  counter.increment
  index
end

.wordsObject



9
10
11
12
# File 'lib/lexdrill/word_list.rb', line 9

def self.words
  @words ||= File.exist?(PATH) && File.readlines(PATH).map(&:strip).reject(&:empty?)
  @words ||= []
end