Class: Blackcal::TimeRange
- Inherits:
-
Object
- Object
- Blackcal::TimeRange
- Includes:
- Enumerable
- Defined in:
- lib/blackcal/range/time_range.rb
Overview
Time range
Instance Attribute Summary collapse
-
#finish ⇒ Object
readonly
Returns the value of attribute finish.
-
#start ⇒ Object
readonly
Returns the value of attribute start.
Instance Method Summary collapse
-
#cover?(timestamp) ⇒ Boolean
Returns true if it covers timestamp.
-
#each(resolution: :hour, &block) ⇒ Object
Iterate over range.
-
#initialize(start_time, finish_time = nil) ⇒ TimeRange
constructor
Initialize time range.
-
#to_a(resolution: :hour) ⇒ Array<Array<Integer>>
Returns range as array.
Constructor Details
#initialize(start_time, finish_time = nil) ⇒ TimeRange
Initialize time range
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
#finish ⇒ Object (readonly)
Returns the value of attribute finish.
8 9 10 |
# File 'lib/blackcal/range/time_range.rb', line 8 def finish @finish end |
#start ⇒ Object (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
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/blackcal/range/time_range.rb', line 20 def cover?() return false if start.nil? && finish.nil? return start < if finish.nil? return finish > if start.nil? return true if finish < return true if start > false end |
#each(resolution: :hour, &block) ⇒ Object
Iterate over range
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
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 |