Class: Scheduler::Schedule
- Inherits:
-
Object
- Object
- Scheduler::Schedule
- Defined in:
- lib/ruby/scheduler/schedule.rb
Constant Summary collapse
- DAY_LENGTH =
meetings Offsite meeting buffer can extend past the end of the day
{ all_offsite: 9, any_offsite: 8.5, no_offsite: 8 }
Instance Attribute Summary collapse
-
#meetings ⇒ Object
Returns the value of attribute meetings.
Instance Method Summary collapse
-
#all_offsite_meetings? ⇒ Boolean
Check if all meetings are offsite Note: If RoR application, I would use the ActiveRecord scope method Output: - boolean.
-
#any_offsite_meetings? ⇒ Boolean
Check if any meetings are offsite Note: If RoR application, I would use the ActiveRecord scope method Output: - boolean.
-
#initialize(params = {}) ⇒ Schedule
constructor
Initialize a Schedule of Meeting objects.
-
#reschedule ⇒ Object
Setter methods Set a meeting schedule using an optimal meeting order Note: Would consider using Trailblazer to construct a contract and operation.
-
#to_s ⇒ Object
Overriden methods Override ‘to_s` method to print expected message provided in README.
-
#total_meeting_time ⇒ Object
Additional class methods Calculate total meeting time for both onsite and offsite meetings.
-
#valid_meeting_schedule? ⇒ Boolean
Conditional methods Check if any time left after meetings Output: - boolean.
Constructor Details
#initialize(params = {}) ⇒ Schedule
Initialize a Schedule of Meeting objects.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ruby/scheduler/schedule.rb', line 18 def initialize(params = {}) @meetings = [] meetings_arr = params.fetch(:meetings, nil) meetings_arr.each do |meeting| meeting_object = Meeting.new(meeting) @meetings << meeting_object end validate! end |
Instance Attribute Details
#meetings ⇒ Object
Returns the value of attribute meetings.
15 16 17 |
# File 'lib/ruby/scheduler/schedule.rb', line 15 def meetings @meetings end |
Instance Method Details
#all_offsite_meetings? ⇒ Boolean
Check if all meetings are offsite Note: If RoR application, I would use the ActiveRecord scope method Output:
-
boolean
79 80 81 |
# File 'lib/ruby/scheduler/schedule.rb', line 79 def all_offsite_meetings? meetings.all? { |meeting| meeting.offsite? } end |
#any_offsite_meetings? ⇒ Boolean
Check if any meetings are offsite Note: If RoR application, I would use the ActiveRecord scope method Output:
-
boolean
87 88 89 |
# File 'lib/ruby/scheduler/schedule.rb', line 87 def any_offsite_meetings? meetings.any? { |meeting| meeting.offsite? } end |
#reschedule ⇒ Object
Setter methods Set a meeting schedule using an optimal meeting order Note: Would consider using Trailblazer to construct a contract and operation.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ruby/scheduler/schedule.rb', line 50 def reschedule set_meeting_order meetings.first.set_start_time(first_start_time) meetings.each_with_index do |meeting, index| if meeting.start_time.nil? previous_meeting = meetings[index - 1] next_start_time = previous_meeting.next_start_time meeting.set_start_time(next_start_time) end meeting.set_end_time end @meetings = meetings self end |
#to_s ⇒ Object
Overriden methods Override ‘to_s` method to print expected message provided in README. Output:
-
String
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ruby/scheduler/schedule.rb', line 33 def to_s valid = valid_meeting_schedule? ? "Valid Schedule\n" : "Invalid Schedule\n" meeting_str = meetings.reduce(valid) do |str, meeting| str += meeting.start_time&.strftime("%I:%M %p") || 'none' str += ' - ' str += meeting.end_time&.strftime("%I:%M %p") || 'none' str += ' - ' str += meeting.name str += "\n" end meeting_str end |
#total_meeting_time ⇒ Object
Additional class methods Calculate total meeting time for both onsite and offsite meetings
93 94 95 |
# File 'lib/ruby/scheduler/schedule.rb', line 93 def total_meeting_time calculate_total_duration(offsite_meetings, offsite: true) + calculate_total_duration(onsite_meetings, offsite: false) end |
#valid_meeting_schedule? ⇒ Boolean
Conditional methods Check if any time left after meetings Output:
-
boolean
71 72 73 |
# File 'lib/ruby/scheduler/schedule.rb', line 71 def valid_meeting_schedule? time_left_in_day >= 0 end |