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.

Parameters:

  • channel (Channel)

    the parent channel

  • programs (Array<Program>) (defaults to: [])

    a list of programs


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)

Returns the channel the schedule belongs to.

Returns:

  • (Channel)

    the channel the schedule belongs to.


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

def channel
  @channel
end

#programsArray<Program> (readonly)

Returns the list of programs in the schedule.

Returns:

  • (Array<Program>)

    the list of programs in the schedule.


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`.

Returns:

  • ((Array<Program>, Program, Array<Program>))

    the previous, the current and the upcoming programs relative to the 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>)

Returns the previous, the current and the upcoming programs relative to the current time.

Returns:

  • ((Array<Program>, Program, Array<Program>))

    the previous, the current and the upcoming programs relative to the current time.


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

def current
  self.at DateTime.now
end