Class: Timeframes::Frame

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/timeframes/frame.rb

Defined Under Namespace

Classes: InvalidDateError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, stop) ⇒ Frame

Returns a new instance of Frame.



8
9
10
11
12
13
14
# File 'lib/timeframes/frame.rb', line 8

def initialize(start, stop)
  @start = start.is_a?(DateTime) ? start : DateTime.parse(start)
  @stop = stop.is_a?(DateTime) ? stop : DateTime.parse(stop)
  if @stop < @start
    raise InvalidDateError, "Stop cannot be before start"
  end
end

Instance Attribute Details

#startObject (readonly)

Returns the value of attribute start.



3
4
5
# File 'lib/timeframes/frame.rb', line 3

def start
  @start
end

#stopObject (readonly)

Returns the value of attribute stop.



3
4
5
# File 'lib/timeframes/frame.rb', line 3

def stop
  @stop
end

Instance Method Details

#<=>(other) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/timeframes/frame.rb', line 36

def <=>(other)
  if other.respond_to?(:start) && other.respond_to?(:stop)
    if @start.to_i < other.start.to_i
      -1
    elsif self == other
      0
    else
      1
    end
  else
    raise NoMethodError
  end
end

#==(other) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/timeframes/frame.rb', line 24

def ==(other)
  if other.respond_to?(:start) && other.respond_to?(:stop)
    if other.start == @start && other.stop == @stop
      true
    else
      false
    end
  else
    raise NoMethodError
  end
end

#duration(format = :seconds) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/timeframes/frame.rb', line 50

def duration(format = :seconds)
  unformatted_duration = @stop.to_i - @start.to_i
  case format
  when :seconds
    duration_divisor = 1
  when :minutes
    duration_divisor =  60.0
  when :hours
    duration_divisor = 3600.0
  when :days
    duration_divisor = 86400.0
  else
    return "unknown format type."
  end
  return unformatted_duration / duration_divisor
end

#to_sObject



16
17
18
19
20
21
22
# File 'lib/timeframes/frame.rb', line 16

def to_s
  if @stop - @start < 24.hours
    @start.strftime("%m/%d/%Y %I:%M:%S %p") + " - " + @stop.strftime("%I:%M:%S %p")
  else
    @start.strftime("%m/%d/%Y %I:%M:%S %p") + " - " + @stop.strftime("%m/%d/%Y %I:%M:%S %p")
  end
end