Skip to content

Instantly share code, notes, and snippets.

@Bodacious
Last active August 29, 2015 14:20
Show Gist options
  • Save Bodacious/84213d687f69a8d9ac36 to your computer and use it in GitHub Desktop.
Save Bodacious/84213d687f69a8d9ac36 to your computer and use it in GitHub Desktop.
Calendar with Event object
require 'time'
require 'date'
class Invitee
end
class Event
attr_accessor :name
attr_accessor :date
attr_accessor :starts_at
attr_accessor :ends_at
attr_accessor :invitees
def self.all
@all ||= []
end
def ends_at=(value)
if starts_at and value > starts_at
@ends_at = value
end
end
# def ends_at_later_than_starts_at?
# if ends_at and starts_at
# ends_at < starts_at
# end
# end
#
end
# Event.all # => []
# Event.all << event
# Event.all # => [event]
def ask_user_for_selection
puts "Options:
1. Press 1 to add an event
2. Press 2 to show events
0. Press 0 to exit"
end
def list_all_events
Event.all.each_with_index do |event,index|
puts "----- Event #{index +1} -----"
puts " Name: #{event.name}"
puts " Date: #{event.date}"
puts " Start time: #{event.starts_at}"
puts " End time: #{event.ends_at}"
puts " invitees #{event.invitees} "
end
end
def add_an_event
new_event = Event.new
new_event.name = ask_for_event_name
new_event.date = ask_for_date
new_event.starts_at = ask_for_start_time
new_event.ends_at = ask_for_end_time until new_event.ends_at
new_event.invitees = ask_for_invitees_list
Event.all << new_event
end
def ask_for_event_name
event_name = ""
while event_name.empty?
puts "what is the event name?"
event_name = gets.chomp
end
return event_name
end
def ask_for_date
puts "ask_for_date"
date = ""
while date !~ /\d\d-\d\d-\d\d\d\d/
puts "what is the date? e.g. 20-04-2014"
date = Date.parse(gets.chomp)
end
return date
end
def ask_for_start_time
begin
puts "what is start time? e.g. 11:00am"
Time.strptime(gets.chomp, "%I:%M%P")
rescue ArgumentError
puts "Remeber to use the format 09:00am"
retry
end
end
def ask_for_end_time
begin
puts "what is the end time? e.g. 10:10pm"
Time.strptime(gets.chomp, "%I:%M%P")
rescue ArgumentError
retry
end
end
#HW- END TIME EMPTY, ENSURE DATE IS LATER THAN OR EQUAL TO FIRST TIME
def ask_for_invitees_list
invite = []
while invite.empty?
puts "Who is attending this event (email addresses seperated by a comma)"
invite = gets.scan(/[a-zA-Z0-9]+@[a-zA-Z0-9]+\.com/)
end
return invite
end
def start!
ask_user_for_selection
case gets.chomp
when "1"
add_an_event and start!
when "2"
list_all_events and start!
when "0"
exit()
else
start!
end
end
start!
@Bodacious
Copy link
Author

@TheDreamShake - Let me know if you get this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment