h-lame (owner)

Revisions

gist: 202246 Download_button fork
public
Public Clone URL: git://gist.github.com/202246.git
Embed All Files: show embed
schedule class #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Schedule
  
  def initialize(service)
    @service = service
  end
  
  attr_accessor :schedule
  
  def day
    (1..23).map { [] }
  end
    
  def week
    (1..7).map { day }
  end
    
  def today
    todays_broadcasts = @service.broadcasts.today
    @schedule = Day.fill(self.day, todays_broadcasts)
  end
 
  module Day
    
    def self.fill(schedule, broadcasts)
      broadcasts.each do |broadcast|
        first_timeslot = (broadcast.start_time < Date.today.midnight) ? 0 : broadcast.start_time.hour
        last_timeslot = (broadcast.end_time > Date.tomorrow.midnight) ? 23 : broadcast.end_time.hour
        last_timeslot -= 1 if (broadcast.end_time.min == 0 && broadcast.end_time.sec == 0)
        (first_timeslot..last_timeslot).each do |slot|
          schedule[slot] << broadcast
        end
      end
      schedule
    end
    
  end
  
end