Class: TimeFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/time_frame/version.rb,
lib/time_frame/empty.rb,
lib/time_frame/time_frame.rb,
lib/time_frame/time_frame_uniter.rb,
lib/time_frame/time_frame_covered.rb,
lib/time_frame/time_frame_overlaps.rb,
lib/time_frame/time_frame_splitter.rb

Overview

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

Direct Known Subclasses

Empty

Defined Under Namespace

Classes: CoveredFrame, Empty, Overlaps, Splitter, Uniter

Constant Summary collapse

VERSION =
'0.2.1'
EMPTY =
Empty.instance

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ TimeFrame

Returns a new instance of TimeFrame.



13
14
15
16
17
18
19
# File 'lib/time_frame/time_frame.rb', line 13

def initialize(args)
  min = args.fetch(:min)
  max = args.fetch(:max, nil) || min + args.fetch(:duration)
  check_bounds(max, min)
  @max = max
  @min = min
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



9
10
11
# File 'lib/time_frame/time_frame.rb', line 9

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



9
10
11
# File 'lib/time_frame/time_frame.rb', line 9

def min
  @min
end

Class Method Details

.covering_time_frame_for(time_frames) ⇒ Object



119
120
121
# File 'lib/time_frame/time_frame.rb', line 119

def self.covering_time_frame_for(time_frames)
  CoveredFrame.new(time_frames).frame
end

.each_overlap(frames1, frames2) ⇒ Object



123
124
125
126
127
# File 'lib/time_frame/time_frame.rb', line 123

def self.each_overlap(frames1, frames2)
  Overlaps.new(frames1, frames2).each do |first, second|
    yield(first, second)
  end
end

.intersection(time_frames) ⇒ Object



78
79
80
81
82
# File 'lib/time_frame/time_frame.rb', line 78

def self.intersection(time_frames)
  time_frames.reduce(time_frames.first) do |intersection, time_frame|
    intersection & time_frame
  end
end

.union(time_frames, options = {}) ⇒ Object



74
75
76
# File 'lib/time_frame/time_frame.rb', line 74

def self.union(time_frames, options = {})
  Uniter.new(time_frames, options).unite
end

Instance Method Details

#&(other) ⇒ Object



90
91
92
93
94
95
# File 'lib/time_frame/time_frame.rb', line 90

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) ⇒ Object



25
26
27
28
# File 'lib/time_frame/time_frame.rb', line 25

def ==(other)
  min == other.min &&
    max == other.max
end

#after?(item) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
# File 'lib/time_frame/time_frame.rb', line 48

def after?(item)
  case
  when rangy?(item)
    fail_if_empty item
    item.max < min
  else
    item < min
  end
end

#before?(item) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
# File 'lib/time_frame/time_frame.rb', line 38

def before?(item)
  case
  when rangy?(item)
    fail_if_empty item
    item.min > max
  else
    item > max
  end
end

#cover?(element) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/time_frame/time_frame.rb', line 30

def cover?(element)
  if rangy?(element)
    element.empty? || min <= element.min && element.max <= max
  else
    min <= element && element <= max
  end
end

#durationObject



21
22
23
# File 'lib/time_frame/time_frame.rb', line 21

def duration
  (max - min).seconds
end

#empty?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/time_frame/time_frame.rb', line 70

def empty?
  false
end

#inspectObject



129
130
131
# File 'lib/time_frame/time_frame.rb', line 129

def inspect
  "#{min}..#{max}"
end

#overlaps?(other) ⇒ Boolean

Returns true if the interior intersect.

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/time_frame/time_frame.rb', line 85

def overlaps?(other)
  return false if other.duration == 0
  other.max > min && other.min < max
end

#shift_by(duration) ⇒ Object



97
98
99
# File 'lib/time_frame/time_frame.rb', line 97

def shift_by(duration)
  TimeFrame.new(min: @min + duration, duration: self.duration)
end

#shift_to(time) ⇒ Object



101
102
103
# File 'lib/time_frame/time_frame.rb', line 101

def shift_to(time)
  TimeFrame.new(min: time, duration: duration)
end

#split_by_interval(interval) ⇒ Object



115
116
117
# File 'lib/time_frame/time_frame.rb', line 115

def split_by_interval(interval)
  Splitter.new(self).split_by interval
end

#time_between(item) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/time_frame/time_frame.rb', line 58

def time_between(item)
  case
  when rangy?(item)
    fail_if_empty item
    [time_between(item.min), time_between(item.max)].min_by(&:abs)
  when cover?(item)
    0
  else
    [(item - min).abs, (item - max).abs].min
  end
end

#without(*args) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/time_frame/time_frame.rb', line 105

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