Class: IceCube::Occurrence

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ice_cube/occurrence.rb

Overview

Wraps start_time and end_time in a single concept concerning the duration. This delegates to the enclosed start_time so it behaves like a normal Time in almost all situations, however:

Without ActiveSupport, it’s necessary to cast the occurrence using #to_time before doing arithmetic, else Time will try to subtract it using #to_i and return a new time instead.

Time.now - Occurrence.new(start_time) # => 1970-01-01 01:00:00
Time.now - Occurrence.new(start_time).to_time # => 3600

When ActiveSupport::Time core extensions are loaded, it’s possible to subtract an Occurrence object directly from a Time to get the difference:

Time.now - Occurrence.new(start_time) # => 3600

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_time, end_time = nil) ⇒ Occurrence

Returns a new instance of Occurrence.



36
37
38
39
40
# File 'lib/ice_cube/occurrence.rb', line 36

def initialize(start_time, end_time=nil)
  @start_time = start_time
  @end_time = end_time || start_time
  __setobj__ @start_time
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



34
35
36
# File 'lib/ice_cube/occurrence.rb', line 34

def end_time
  @end_time
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



34
35
36
# File 'lib/ice_cube/occurrence.rb', line 34

def start_time
  @start_time
end

Class Method Details

.nameObject

Report class name as ‘Time’ to thwart type checking.



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

def self.name
  'Time'
end

Instance Method Details

#comparable_timeObject



66
67
68
# File 'lib/ice_cube/occurrence.rb', line 66

def comparable_time
  start_time
end

#durationObject



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

def duration
  end_time - start_time
end

#intersects?(other) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ice_cube/occurrence.rb', line 47

def intersects? other
  if other.is_a?(Occurrence) || other.is_a?(Range)
    lower_bound_1 = first + 1
    upper_bound_1 = last # exclude end
    lower_bound_2 = other.first + 1
    upper_bound_2 = other.last + 1
    if (lower_bound_2 <=> upper_bound_2) > 0
      false
    elsif (lower_bound_1 <=> upper_bound_1) > 0
      false
    else
      (upper_bound_1 <=> lower_bound_2) >= 0 and
        (upper_bound_2 <=> lower_bound_1) >= 0
    end
  else
    cover? other
  end
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Returns:

  • (Boolean)


42
43
44
# File 'lib/ice_cube/occurrence.rb', line 42

def is_a?(klass)
  klass == ::Time || super
end

#overnight?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/ice_cube/occurrence.rb', line 95

def overnight?
  offset = start_time + 3600 * 24
  midnight = Time.new(offset.year, offset.month, offset.day)
  midnight < end_time
end

#to_rangeObject



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

def to_range
  start_time..end_time
end

#to_s(format = nil) ⇒ Object

Shows both the start and end time if there is a duration. Optional format argument (e.g. :long, :short) supports Rails time formats and is only used when ActiveSupport is available.



86
87
88
89
90
91
92
93
# File 'lib/ice_cube/occurrence.rb', line 86

def to_s(format=nil)
  if format && to_time.public_method(:to_s).arity > 0
    t0, t1 = start_time.to_s(format), end_time.to_s(format)
  else
    t0, t1 = start_time.to_s, end_time.to_s
  end
  duration > 0 ? "#{t0} - #{t1}" : t0
end

#to_timeObject



78
79
80
# File 'lib/ice_cube/occurrence.rb', line 78

def to_time
  start_time
end