Class: Blackcal::TimeRange

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

Overview

Time range

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_time, finish_time = nil) ⇒ TimeRange

Initialize time range

Parameters:

  • start_time (Time, nil)
  • finish_time (Time, nil) (defaults to: nil)

    optional



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

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

Instance Attribute Details

#finishObject (readonly)

Returns the value of attribute finish.



8
9
10
# File 'lib/blackcal/range/time_range.rb', line 8

def finish
  @finish
end

#startObject (readonly)

Returns the value of attribute start.



8
9
10
# File 'lib/blackcal/range/time_range.rb', line 8

def start
  @start
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
# File 'lib/blackcal/range/time_range.rb', line 20

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

  return true if finish < timestamp
  return true if start > timestamp

  false
end

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

Iterate over range

Parameters:

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

    :hour our :min



45
46
47
# File 'lib/blackcal/range/time_range.rb', line 45

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

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

Returns range as array

Parameters:

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

    :hour our :min

Returns:

  • (Array<Array<Integer>>)

    times



34
35
36
37
38
39
40
41
# File 'lib/blackcal/range/time_range.rb', line 34

def to_a(resolution: :hour)
  resolution_multiplier = resolution == :hour ? 60 * 60 : 60
  time_units = ((start - finish) / resolution_multiplier).abs.to_i

  Array.new(time_units) do |time_unit|
    start + (time_unit * resolution_multiplier)
  end
end