Class: TimeSpan

Inherits:
Object
  • Object
show all
Defined in:
lib/time_span.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ TimeSpan

Returns a new instance of TimeSpan.



6
7
8
9
10
11
12
13
14
15
# File 'lib/time_span.rb', line 6

def initialize(args={})
  @start_at = args[:start_at]
  
  if @end_at = args[:end_at]
    @end_at, @start_at = @start_at, @end_at if @start_at > @end_at
    @duration = @end_at - @start_at
  elsif @duration = args[:duration]
    @end_at = @start_at + @duration
  end
end

Instance Attribute Details

#durationObject

Returns the value of attribute duration.



4
5
6
# File 'lib/time_span.rb', line 4

def duration
  @duration
end

#end_atObject

Returns the value of attribute end_at.



4
5
6
# File 'lib/time_span.rb', line 4

def end_at
  @end_at
end

#start_atObject

Returns the value of attribute start_at.



4
5
6
# File 'lib/time_span.rb', line 4

def start_at
  @start_at
end

Instance Method Details

#enveloped_by?(args, include_end = true) ⇒ Boolean

TODO

Returns:

  • (Boolean)


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

def enveloped_by?(args, include_end=true)
  case
  when args.is_a?(TimeSpan)
  when args.is_a?(Range)
  when args.is_a?(Array)
  end
end

#envelopes?(t, include_end = true) ⇒ Boolean

Returns:

  • (Boolean)


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

def envelopes?(t, include_end=true)
  t >= @start_at && ( t < @end_at || (include_end && t <= @end_at) )
end

#firstObject



17
18
19
# File 'lib/time_span.rb', line 17

def first
  @start_at
end

#lastObject



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

def last
  @end_at
end

#overlap(t, include_end = true) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/time_span.rb', line 51

def overlap(t, include_end=true)
  return 0 unless self.overlaps?(t, include_end)
  e = t.end_at > @end_at ? @end_at : t.end_at
  s = t.start_at < @start_at ? @start_at : t.start_at
  o = e-s
  !include_end && o <= 1 ? 0.0 : o
end

#overlap_by(t, include_end = true) ⇒ Object

ratio: overlap/duration



47
48
49
# File 'lib/time_span.rb', line 47

def overlap_by(t, include_end=true)
  self.overlap(t, include_end)/@duration.to_f
end

#overlaps?(t, include_end = true) ⇒ Boolean

Returns:

  • (Boolean)


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

def overlaps?(t, include_end=true)
  if include_end
    t.end_at >= @start_at && t.start_at <= @end_at
  else
    t.end_at > @start_at && t.start_at < @end_at
  end
end