Created
June 14, 2013 21:41
-
-
Save ayoformayo/5785535 to your computer and use it in GitHub Desktop.
Flashcard App V1 and V2.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
module Flashcard | |
class Deck | |
def initialize options = {} | |
@file = options[:deck] || 'default.csv' | |
@deck = load_deck | |
end | |
def list | |
#@deck.inject("") {|memo, card| memo + card.front + "\n" } | |
@deck | |
end | |
def choose_card | |
@deck.sample | |
end | |
def add_card options = {} | |
@deck << Card.new(:front => options[:front], :back => options[:back], :id => unique_id) | |
save | |
end | |
def delete_card id | |
@deck.delete_if { |card| card.id == id } | |
fix_ids | |
save | |
end | |
def save | |
CSV.open(@file, "w") do |csv| | |
csv << ["id","front","back","created_at","tags"] | |
@deck.each do |card| | |
csv << [card.id.to_s,card.front,card.back,card.created_at,card.tags] | |
end | |
end | |
end | |
private | |
def fix_ids | |
@deck.each_with_index do |card, i| | |
card.id = i + 1 | |
end | |
end | |
def unique_id | |
@deck.empty? ? 1 : @deck.sort_by(&:id).last.id + 1 | |
end | |
def load_deck | |
create_file unless File.exist?(@file) | |
CSV.parse(File.open(@file), :headers => true, :header_converters => :symbol).map { |line| Card.new(line.to_hash)} #neccesity? | |
end | |
def create_file | |
p CSV.open(@file, "wb") { |csv| csv << ["id","front","back","created_at","tags"] } | |
end | |
end | |
class Card | |
attr_reader :front, :back, :created_at, :tags | |
attr_accessor :id | |
def initialize options = {} | |
@id = options[:id].to_i | |
@front = options[:front] | |
@back = options[:back] | |
@tags = options[:tags] | |
@created_at = Time.now | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative 'flashcards' | |
class Controller | |
attr_reader :my_deck | |
def initialize | |
@my_view = View.new(self) | |
@my_view.display | |
@my_deck = Flashcard::Deck.new(:deck => @my_view.deck_name) | |
@my_view.start_game | |
end | |
def execute | |
command = @my_view.input | |
case command | |
when "new card" | |
@my_view.view_new_card | |
#check that new ideas are being generated properly | |
@my_deck.add_card(front: @my_view.card_name, back: @my_view.card_definition) | |
when "new deck" | |
@my_view.view_new_deck | |
Flashcard::Deck.new(:deck => @my_view.new_deck_name) | |
when "delete" | |
@my_view.view_delete | |
@my_deck.delete_card(@my_view.deleted_card_id) | |
when "list" | |
puts @my_view.view_list(@my_deck) | |
when "play" | |
@my_view.view_play(@my_deck) | |
when "exit" | |
exit | |
else | |
puts "That option doesn't exist" | |
end | |
end | |
end | |
class View | |
attr_reader :input, :deck_name ,:card_name, :card_definition, :new_deck_name, :deleted_card_id | |
def initialize(controller) | |
@controller = controller | |
end | |
def display | |
puts "Enter the name of the deck" | |
@deck_name = gets.chomp | |
end | |
def start_game | |
puts "What would you like to do?" | |
@input = '' | |
until @input.downcase == 'exit' | |
@input = gets.chomp | |
@controller.execute | |
puts "What would you like to do next?" | |
end | |
end | |
def view_new_card | |
puts "Name the Card" | |
@card_name = gets.chomp | |
puts "Give the definition" | |
@card_definition = gets.chomp | |
end | |
def view_new_deck | |
puts "Name the deck." | |
@new_deck_name = gets.chomp | |
end | |
def view_delete | |
puts "What card do you want to delete?" | |
@deleted_card_id = gets.chomp.to_i | |
end | |
def view_list(a_deck) | |
array = a_deck.list | |
str = '' | |
array.each do |x| | |
str << "#{x.id}. Front: #{x.front}, Back: #{x.back}\n" | |
end | |
str | |
end | |
def view_play(deck_to_play) | |
next_card = deck_to_play.choose_card | |
puts next_card.front | |
puts "What is your guess?" | |
guess = gets.chomp | |
if guess.downcase == next_card.back.downcase | |
puts "Correct" | |
else | |
puts "Wrong again, buddy! The answer is:" | |
puts next_card.back | |
end | |
end | |
end | |
Controller.new | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment