class Train::SingleChoice

Constants

CHOICES_SIZE

Attributes

command_context[R]

Public Class Methods

new(command_context) click to toggle source
# File lib/wordword/interactors/train/single_choice.rb, line 12
def initialize(command_context)
  @command_context = command_context
end

Public Instance Methods

call(word, translated_word, words:) click to toggle source
# File lib/wordword/interactors/train/single_choice.rb, line 16
def call(word, translated_word, words:)
  answer = command_context.prompt.select(
    "What is the translation of '#{word}'?",
    choices(translated_word, words),
  )

  if answer == translated_word
    Success(answer)
  else
    Failure(
      I18n.t(
        "train.wrong_answer_entry",
        word: pastel.blue(word),
        correct_answer: pastel.green(translated_word),
        answer: pastel.red(answer),
      ),
    )
  end
end

Private Instance Methods

choices(translated_word, words) click to toggle source
# File lib/wordword/interactors/train/single_choice.rb, line 40
def choices(translated_word, words)
  result = [translated_word]

  choices_size = words.size < CHOICES_SIZE ? words.size : CHOICES_SIZE

  until result.size == choices_size
    sample = words.values.sample
    result << sample unless result.include?(sample)
  end

  result.shuffle
end
pastel() click to toggle source
# File lib/wordword/interactors/train/single_choice.rb, line 53
def pastel
  @pastel ||= Pastel.new
end