Class: Blackcal::TimeOfDayRange

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/blackcal/range/time_of_day_range.rb

Overview

Time of day range

Instance Method Summary collapse

Constructor Details

#initialize(start, finish = nil) ⇒ TimeOfDayRange

Initialize time of day range

Parameters:

  • start (TimeOfDay, Time, Integer, nil)
  • finish (TimeOfDay, Time, Integer, nil) (defaults to: nil)


13
14
15
16
# File 'lib/blackcal/range/time_of_day_range.rb', line 13

def initialize(start, finish = nil)
  @start = start
  @finish = finish
end

Instance Method Details

#cover?(timestamp) ⇒ Boolean

Returns true if it covers timestamp

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/blackcal/range/time_of_day_range.rb', line 20

def cover?(timestamp)
  return false if @start.nil? && @finish.nil?

  t1 = TimeOfDay.new(timestamp.hour, timestamp.min)
  if start == finish
    t1 == start
  elsif start > finish
    t1 <= finish || t1 >= start
  else
    t1 >= start && t1 <= finish
  end
end

#each(resolution: :hour, &block) ⇒ Object

Iterate over range

Parameters:

  • resolution (Symbol) (defaults to: :hour)

    :hour our :min



61
62
63
# File 'lib/blackcal/range/time_of_day_range.rb', line 61

def each(resolution: :hour, &block)
  to_a(resolution: resolution).each(&block)
end

#finishTimeOfDay

Return finish hour

Returns:



41
42
43
# File 'lib/blackcal/range/time_of_day_range.rb', line 41

def finish
  @finish_time ||= to_time_of_day(@finish || 0)
end

#startTimeOfDay

Return start hour

Returns:



35
36
37
# File 'lib/blackcal/range/time_of_day_range.rb', line 35

def start
  @start_time ||= to_time_of_day(@start || 0)
end

#to_a(resolution: :hour) ⇒ Array<Array<Integer>>

Returns range as an array

Parameters:

  • resolution (Symbol) (defaults to: :hour)

    :hour our :min

Returns:

  • (Array<Array<Integer>>)

    times



48
49
50
51
52
53
54
55
56
57
# File 'lib/blackcal/range/time_of_day_range.rb', line 48

def to_a(resolution: :hour)
  return [] if @start.nil? && @finish.nil?

  if finish < start
    to_time_array(start, TimeOfDay.new(23), resolution) +
      to_time_array(TimeOfDay.new(0), finish, resolution)
  else
    to_time_array(start, finish, resolution)
  end
end