class TimeFrame::Uniter

Creates a union of many time_frame's. You can request a sorted collection by the min Time value.

Public Class Methods

new(time_frames, options = {}) click to toggle source
# File lib/time_frame/time_frame_uniter.rb, line 6
def initialize(time_frames, options = {})
  @time_frames = time_frames.reject(&:empty?)
  @sorted = options[:sorted]
end

Public Instance Methods

unite() click to toggle source
# File lib/time_frame/time_frame_uniter.rb, line 11
def unite
  frames = @sorted ? @time_frames : @time_frames.sort_by(&:min)
  frames.each_with_object([]) do |next_time_frame, result|
    last_time_frame = result.last
    if last_time_frame && last_time_frame.cover?(next_time_frame.min)
      max = [last_time_frame.max, next_time_frame.max].max
      result[-1] = TimeFrame.new(min: last_time_frame.min, max: max)
    else
      result << next_time_frame
    end
  end
end