class TimeFrame

The time frame class provides an specialized and enhanced range for time values.

gem version

Constants

EMPTY
VERSION

Attributes

max[R]
max_float[R]
min[R]
min_float[R]

Public Class Methods

covering_time_frame_for(time_frames) click to toggle source
# File lib/time_frame/time_frame.rb, line 129
def self.covering_time_frame_for(time_frames)
  CoveredFrame.new(time_frames).frame
end
each_overlap(frames1, frames2) { |first, second| ... } click to toggle source
# File lib/time_frame/time_frame.rb, line 133
def self.each_overlap(frames1, frames2)
  Overlaps.new(frames1, frames2).each do |first, second|
    yield first, second
  end
end
intersection(time_frames) click to toggle source
# File lib/time_frame/time_frame.rb, line 88
def self.intersection(time_frames)
  time_frames.reduce(time_frames.first) do |intersection, time_frame|
    intersection & time_frame
  end
end
new(args) click to toggle source
# File lib/time_frame/time_frame.rb, line 13
def initialize(args)
  @min = args.fetch(:min)
  @max = args.fetch(:max) { @min + args.fetch(:duration) }
  check_bounds
  @max_float = @max.to_f
  @min_float = @min.to_f
end
union(time_frames, options = {}) click to toggle source
# File lib/time_frame/time_frame.rb, line 84
def self.union(time_frames, options = {})
  Uniter.new(time_frames, options).unite
end

Public Instance Methods

&(other) click to toggle source
# File lib/time_frame/time_frame.rb, line 100
def &(other)
  return EMPTY if other.empty?
  new_min = [min, other.min].max
  new_max = [max, other.max].min
  new_min <= new_max ? TimeFrame.new(min: new_min, max: new_max) : EMPTY
end
<=>(other) click to toggle source
# File lib/time_frame/time_frame.rb, line 30
def <=>(other)
  [@min_float, @max_float] <=> [other.min_float, other.max_float]
end
==(other) click to toggle source
# File lib/time_frame/time_frame.rb, line 25
def ==(other)
  @min_float == other.min_float &&
    @max_float == other.max_float
end
Also aliased as: eql?
after?(item) click to toggle source
# File lib/time_frame/time_frame.rb, line 59
def after?(item)
  case
  when item.is_a?(TimeFrame)
    fail_if_empty item
    item.max_float < min_float
  else
    item.to_f < min_float
  end
end
before?(item) click to toggle source
# File lib/time_frame/time_frame.rb, line 49
def before?(item)
  case
  when item.is_a?(TimeFrame)
    fail_if_empty item
    item.min_float > max_float
  else
    item.to_f > max_float
  end
end
cover?(element) click to toggle source
# File lib/time_frame/time_frame.rb, line 40
def cover?(element)
  if element.is_a?(TimeFrame)
    element.empty? ||
      @min_float <= element.min_float && element.max_float <= max_float
  else
    min_float <= element.to_f && element.to_f <= max_float
  end
end
duration() click to toggle source
# File lib/time_frame/time_frame.rb, line 21
def duration
  @duration ||= (@max_float - @min_float)
end
empty?() click to toggle source
# File lib/time_frame/time_frame.rb, line 80
def empty?
  false
end
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/time_frame/time_frame.rb, line 36
def hash
  [min, max].hash
end
inspect() click to toggle source
# File lib/time_frame/time_frame.rb, line 139
def inspect
  "#{min}..#{max}"
end
overlaps?(other) click to toggle source

Returns true if the interior intersect.

# File lib/time_frame/time_frame.rb, line 95
def overlaps?(other)
  return false if other.duration == 0
  other.max_float > min_float && other.min_float < max_float
end
shift_by(duration) click to toggle source
# File lib/time_frame/time_frame.rb, line 107
def shift_by(duration)
  TimeFrame.new(min: @min + duration, duration: self.duration)
end
shift_to(time) click to toggle source
# File lib/time_frame/time_frame.rb, line 111
def shift_to(time)
  TimeFrame.new(min: time, duration: duration)
end
split_by_interval(interval) click to toggle source
# File lib/time_frame/time_frame.rb, line 125
def split_by_interval(interval)
  Splitter.new(self).split_by interval
end
time_between(item) click to toggle source
# File lib/time_frame/time_frame.rb, line 69
def time_between(item)
  case
  when item.is_a?(TimeFrame)
    time_between_time_frame(item)
  when cover?(item)
    0
  else
    time_between_float(item.to_f)
  end
end
without(*args) click to toggle source
# File lib/time_frame/time_frame.rb, line 115
def without(*args)
  frames = args.select { |frame| overlaps?(frame) }
  frames = TimeFrame.union(frames)

  frames.reduce([self]) do |result, frame_to_exclude|
    last_frame = result.pop
    result + last_frame.without_frame(frame_to_exclude)
  end
end

Protected Instance Methods

without_frame(other) click to toggle source
# File lib/time_frame/time_frame.rb, line 147
def without_frame(other)
  result = []

  if other.min_float > min_float
    result << TimeFrame.new(min: min, max: other.min)
  end

  if other.max_float < max_float
    result << TimeFrame.new(min: other.max, max: max)
  end

  result
end

Private Instance Methods

check_bounds() click to toggle source
# File lib/time_frame/time_frame.rb, line 168
def check_bounds
  fail ArgumentError, 'min is greater than max.' if min > max
end
fail_if_empty(item) click to toggle source
# File lib/time_frame/time_frame.rb, line 163
def fail_if_empty(item)
  fail ArgumentError, 'time frame is empty' if item.respond_to?(:empty?) &&
                                               item.empty?
end
time_between_float(float_value) click to toggle source
# File lib/time_frame/time_frame.rb, line 177
def time_between_float(float_value)
  [(float_value - min_float).abs, (float_value - max_float).abs].min
end
time_between_time_frame(time_frame) click to toggle source
# File lib/time_frame/time_frame.rb, line 172
def time_between_time_frame(time_frame)
  fail_if_empty time_frame
  [time_between(time_frame.min), time_between(time_frame.max)].min_by(&:abs)
end