Class: Wordword::Commands::Train

Inherits:
Wordword::Command show all
Defined in:
lib/wordword/commands/train.rb

Instance Method Summary collapse

Methods inherited from Wordword::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(file, options) ⇒ Train

Returns a new instance of Train.



12
13
14
15
# File 'lib/wordword/commands/train.rb', line 12

def initialize(file, options)
  @file = file
  @options = options
end

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wordword/commands/train.rb', line 17

def execute(input: $stdin, output: $stdout)
  read_result = ReadWordTable.new.call(filename: @file)
  if read_result.success?
    words = read_result.success
  else
    output.puts(
      pastel.red(
        I18n.t("errors.#{read_result.failure}"),
      ),
    )
    return
  end

  word_loop.run(
    words,
    loop_depth: @options[:number],
  )
rescue TTY::Reader::InputInterrupt
  prompt.error(
    pastel.red(
      I18n.t("train.interrupted"),
    ),
  )
ensure
  if words && word_loop
    if word_loop.wrong_answers.any?
      prompt.error(I18n.t("train.wrong_answers_intro"))
      word_loop.wrong_answers.each do |wrong_answer_message|
        prompt.say(wrong_answer_message)
      end
    elsif words.any?
      prompt.ok(
        I18n.t("train.everything_correct"),
      )
    end
  end
end