Class: TVTid::Schedule

Inherits:
Object
  • Object
show all
Defined in:
library/tvtid/schedule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel, programs = []) ⇒ Schedule

Constructs a new schedule for a channel.


12
13
14
15
# File 'library/tvtid/schedule.rb', line 12

def initialize channel, programs = []
  @channel = channel
  @programs = programs
end

Instance Attribute Details

#channelChannel (readonly)


4
5
6
# File 'library/tvtid/schedule.rb', line 4

def channel
  @channel
end

#programsArray<Program> (readonly)


6
7
8
# File 'library/tvtid/schedule.rb', line 6

def programs
  @programs
end

Instance Method Details

#at(time) ⇒ (Array<Program>, Program, Array<Program>)

Returns the previous, current and upcoming programs at a given `time`.


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'library/tvtid/schedule.rb', line 21

def at time
  cur_idx = 0

  @programs.each_with_index do |program, index|
    if program.start_time <= time and program.stop_time >= time
      cur_idx = index
      break
    end
  end

  if cur_idx != 0
    return @programs[0..cur_idx-1], @programs[cur_idx], @programs[cur_idx+1..-1]
  end
end

#current(Array<Program>, Program, Array<Program>)


38
39
40
# File 'library/tvtid/schedule.rb', line 38

def current
  self.at DateTime.now
end