diff --git a/Jays-Game/controller.rb b/Jays-Game/controller.rb deleted file mode 100644 index c043b14..0000000 --- a/Jays-Game/controller.rb +++ /dev/null @@ -1,37 +0,0 @@ -require_relative 'view' -require_relative 'model' - -class GameController - include GameView - - def run! - todoList = List.new - - Print::run_spinner - Print::title_screen - - #Make todoList methods like RESTful endpoints (new/edit/update/delete) - #Think Backbone Model & View - - loop do - Print::menu - case Print::fetch_user_input - when "V" - Print::print_list(todoList.todos) - when "A" - todoList.add_todo(Print::serialize_todo) - when "C" - todoList.complete_todo(Print::completed_id.to_i) - when "D" - todoList.delete_todo(Print::deleted_id.to_i) - when "Q" - puts "We're done" - exit - else - Print::error_message - end - end - end -end - -GameController.new.run! diff --git a/Jays-Game/model.rb b/Jays-Game/model.rb deleted file mode 100644 index 4708f62..0000000 --- a/Jays-Game/model.rb +++ /dev/null @@ -1,57 +0,0 @@ -require 'faker' -require 'pry' - -class Todo - attr_reader :id, :title, :description, :completed - - def initialize args - @id = args[:id] - @title = args[:title] - @description = args[:description] - @completed = false - end - - def mark_completed - @completed = true - end - - def completed? - @completed - end -end - -class List - attr_reader :todos - - def initialize - @primary_id = 0 - @todos = [] - populate_dummy_todos - end - - def add_todo(input) - @todos << Todo.new(input.merge(fetch_id)) - end - - def complete_todo(id) - completed_item = @todos.select { |todo| todo.id == id } - fail "No item matching that id" unless completed_item - completed_item[0].mark_completed - end - - def delete_todo(id) - @todos.delete_if { |n| n.id == id } - end - - def populate_dummy_todos - 5.times do - add_todo(title: Faker::Lorem.word, description: Faker::Lorem.sentence) - end - end - - private - - def fetch_id - {id: @primary_id += 1 } - end -end diff --git a/Jays-Game/view.rb b/Jays-Game/view.rb deleted file mode 100644 index e90ae7c..0000000 --- a/Jays-Game/view.rb +++ /dev/null @@ -1,83 +0,0 @@ -module GameView - - module Print - - class << self - def run_spinner - print "Loading (please wait) " - 5.times { print "."; sleep 1; } - print "\n" - end - - def error_message - puts "That's not a command key. Try again!" - end - - def title_screen -title = < " - gets.chomp - end - end - end -end diff --git a/Peters-Game/controller.rb b/Peters-Game/controller.rb new file mode 100644 index 0000000..76ed2c4 --- /dev/null +++ b/Peters-Game/controller.rb @@ -0,0 +1,44 @@ +require_relative 'view' +require_relative 'model' + +class GameController + include AttendanceView + + def run! + attendanceList = List.new + + Print::run_spinner + Print::title_screen + + loop do + Print::menu + case Print::fetch_user_input + when "VT" + Print::print_list(attendanceList.todays_attendances) + when "VA" + Print::print_list(attendanceList.attendances) + when "AA" + attendanceList.add_attendance(Print::get_attendance_input) + when "MAA" + attendanceList.mark_absent(Print::marked_absent_id.to_i) + when "MAP" + attendanceList.mark_present(Print::marked_present_id.to_i) + when "DA" + attendanceList.delete_attendance(Print::deleted_id("attendance").to_i) + when "VP" + Print::print_list(attendanceList.people) + when "AP" + attendanceList.add_person(Print::serialize_person) + when "DP" + attendanceList.delete_person(Print::deleted_id("person").to_i) + when "Q" + puts "We're done" + exit + else + Print::error_message + end + end + end +end + +GameController.new.run! diff --git a/Peters-Game/model.rb b/Peters-Game/model.rb new file mode 100644 index 0000000..925e92f --- /dev/null +++ b/Peters-Game/model.rb @@ -0,0 +1,136 @@ +require 'faker' +require 'pry' +require 'date' + +class NoPeopleError < StandardError; end + +class Attendance + @@primary_id = 0 + attr_reader :id, :date, :attended + + def initialize args + @id = Attendance.fetch_id + @person = args[:person] + @date = Date.parse args.fetch(:date, Date.today.to_s) + @attended = nil + end + + def to_s + if present? + presence = "\u2705" + elsif absent? + presence = "\u274C" + else + presence = "\u2753" + end + "#{presence} #{@id.to_s.rjust(2)} || #{@date} - #{@person.name}(#{@person.id})" + end + + def person_id + @person.id + end + + def mark_present + @attended = true + end + + def mark_absent + @attended = false + end + + def present? + @attended == true + end + + def absent? + @attended == false + end + + private + + def self.fetch_id + @@primary_id += 1 + end +end + +class Person + @@primary_id = 0 + attr_reader :id, :name, :attendances + + def initialize args + @id = Person.fetch_id + @name = args[:name] + end + + def to_s + "#{id} || #{name}" + end + + private + + def self.fetch_id + @@primary_id += 1 + end +end + +class List + attr_reader :attendances, :people + + def initialize + @attendances = [] + @people = [] + populate_dummy_people + populate_dummy_attendances + end + + def todays_attendances + @attendances.select { |att| att.date == Date.today } + end + + def add_person(input) + @people << Person.new(input) + end + + def add_attendance(input) + input[:person] = person_by_id(input[:person].to_i) + @attendances << Attendance.new(input) + end + + def mark_present(id) + attended_item = @attendances.select { |attendance| attendance.id == id } + fail "No item matching that id" unless attended_item + attended_item[0].mark_present + end + + def mark_absent(id) + absent_item = @attendances.select { |attendance| attendance.id == id } + fail "No item matching that id" unless absent_item + absent_item[0].mark_absent + end + + def person_by_id(id) + @people.find {|person| person.id == id } + end + + def delete_person(id) + @attendances.delete_if { |n| n.person_id == id } + @people.delete_if { |n| n.id == id } + end + + def delete_attendance(id) + @attendances.delete_if { |n| n.id == id } + end + + def populate_dummy_people + 5.times do + add_person(name: Faker::Name.name) + end + end + + def populate_dummy_attendances + raise NoPeopleError, "There are no people" if @people.empty? + 10.times do + add_attendance(person: @people.sample.id, date: Date.today.to_s) + end + end +end diff --git a/Peters-Game/view.rb b/Peters-Game/view.rb new file mode 100644 index 0000000..04cce17 --- /dev/null +++ b/Peters-Game/view.rb @@ -0,0 +1,102 @@ +module AttendanceView + + module Print + + class << self + def run_spinner + print "Loading (please wait) " + 5.times { print "."; sleep 0.5; } + print "\n" + end + + def error_message + puts "That's not a command key. Try again!" + end + + def title_screen +title = <<TITLE + + ********** || ********** + * ATTENDANCE MAGIC * + ********** || ********** + +TITLE + puts title + end + + def menu +menu = <<EOS + + ***** Welcome ***** + - (VT) - View Today's attendance list + - (VA) - View all Attendance + - (AA) - Add Attendance entry + - (MAA) - Mark Attendance Absent + - (MAP) - Mark Attendance Present + - (DA) - Delete Attendance entry + + - (VP) - View all People + - (AP) - Add Person + - (DP) - Delete Person (and Attendance) + + - (Q) - Quit program + ***** ***** + +EOS + puts menu + end + + def print_list(collection) + collection.each {|item| puts item } + end + + def get_attendance_input + serialize_attendance.tap do |obj| + obj[:date] = Date.today.to_s if obj[:date].empty? + end + end + + def serialize_attendance + serialize_item ({ + date: "\nEnter the date(e.g. '2015-05-27'), leave blank for today:", + person: "\nEnter the person's ID:" + }) + end + + def serialize_person + serialize_item ({ + name: "\nEnter the person's NAME:" + }) + end + + def serialize_item(question_hash) + question_hash.tap do |obj| + obj.each do |key,prompt| + obj[key] = fetch_user_input(prompt) + end + end + end + + def deleted_id(id_type) + gimme_id = "\nEnter the #{id_type} id you want to DELETE:" + fetch_user_input(gimme_id) + end + + def marked_present_id + gimme_id = "\nEnter the attendance id you want to mark as PRESENT:" + fetch_user_input(gimme_id) + end + + def marked_absent_id + gimme_id = "\nEnter the attendance id you want to mark as ABSENT:" + fetch_user_input(gimme_id) + end + + def fetch_user_input(question=nil) + puts question if question + print "> " + gets.chomp + end + end + end +end