class LanguageCards::UserInterface

Attributes

cards[R]
correct[R]
courses[R]
incorrect[R]
mode[R]

Public Class Methods

new(cards) click to toggle source
# File lib/language_cards/user_interface.rb, line 12
def initialize cards
  @cards = cards
  @courses = process_courses(cards)
  @mode = [:translate, :typing_practice].cycle
end

Public Instance Methods

start() click to toggle source
# File lib/language_cards/user_interface.rb, line 18
def start
  unless ENV['SKIP_SPLASH']
    clear
    CLI.say SPLASH_SCREEN
    sleep 2
  end

  begin
    loop do
      clear

      CLI.say MainMenu.new(opts).render courses: courses, mode: mode

      value = CLI.ask("")

      next mode.next if value =~ /\Am\z/i
      value = value.to_i - 1 rescue next

      last = nil
      if (0..courses.length-1).include? value

        collection = cards[value] # MenuNode
        title = "#{collection.title} (#{humanize mode.peek})"
        collection = collection.mode(mode.peek) # Mode<CardSet> < Game

        game = Game.new(opts)
        timer = Timer.new
        begin # Game Loop
          loop do
            clear
            timer.mark
            CLI.say game.render correct: correct,
                                incorrect: incorrect,
                                title: title,
                                timer: timer,
                                last: last
            result = game.process(collection, collection.mode)
            result[:correct] ? correct! : incorrect!
            last = result[:last]
          end
        rescue SystemExit, Interrupt
        end
      end
    end

  rescue SystemExit, Interrupt
  end
end

Private Instance Methods

correct!() click to toggle source
# File lib/language_cards/user_interface.rb, line 73
def correct!
  @correct = @correct.to_i + 1
end
incorrect!() click to toggle source
# File lib/language_cards/user_interface.rb, line 77
def incorrect!
  @incorrect = @incorrect.to_i + 1
end
opts() click to toggle source
# File lib/language_cards/user_interface.rb, line 69
def opts
  @opts ||= {}
end
process_courses(cards) click to toggle source
# File lib/language_cards/user_interface.rb, line 81
def process_courses(cards)
  courses = cards.flat_map {|i| i.label.join(' - ') }

  if courses.empty?
    opts[:errors] = ["No Flash Cards found for language: #{CARD_LANGUAGE}"]
  end

  courses
end