class ScheduledValue::Timespan

Attributes

finish[R]
start[R]

Public Class Methods

new(start: nil, finish: nil) click to toggle source
# File lib/scheduled_value/timespan.rb, line 9
def initialize(start: nil, finish: nil)
  self.start = start
  self.finish = finish

  raise "Finish must be after start" if start && finish && start >= finish
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/scheduled_value/timespan.rb, line 49
def <=>(other)
  case other
  when Timespan then compare_timespan(other)
  when Date, Time, DateTime then compare_datetime(other)
  end
end
attributes() click to toggle source
# File lib/scheduled_value/timespan.rb, line 16
def attributes
  {
    start: start,
    finish: finish
  }
end
contains?(timestamp) click to toggle source
# File lib/scheduled_value/timespan.rb, line 35
def contains?(timestamp)
  return false if start && timestamp < start
  return false if finish && timestamp >= finish

  true
end
finish=(value) click to toggle source
# File lib/scheduled_value/timespan.rb, line 31
def finish=(value)
  @finish = convert_time_value(value)
end
finish_description(format = nil, timezone = nil) click to toggle source
# File lib/scheduled_value/timespan.rb, line 73
def finish_description(format = nil, timezone = nil)
  if finish
    finish_in_zone = timezone ? finish.in_time_zone(timezone) : finish
    "up to #{finish_in_zone.to_s(format)}"
  elsif start
    "indefinitely"
  end
end
inspect() click to toggle source
# File lib/scheduled_value/timespan.rb, line 56
def inspect
  "#<#{self.class.name}: #{self}>"
end
overlaps?(other) click to toggle source
# File lib/scheduled_value/timespan.rb, line 42
def overlaps?(other)
  return false if finish && other.start && other.start >= finish
  return false if start && other.finish && start >= other.finish

  true
end
start=(value) click to toggle source
# File lib/scheduled_value/timespan.rb, line 27
def start=(value)
  @start = convert_time_value(value)
end
start_description(format = nil, timezone = nil) click to toggle source
# File lib/scheduled_value/timespan.rb, line 64
def start_description(format = nil, timezone = nil)
  if start
    start_in_zone = timezone ? start.in_time_zone(timezone) : start
    "from #{start_in_zone.to_s(format)}"
  else
    'anytime'
  end
end
to_hash(*) click to toggle source
# File lib/scheduled_value/timespan.rb, line 23
def to_hash(*)
  attributes
end
to_s(format = nil, timezone = nil) click to toggle source
# File lib/scheduled_value/timespan.rb, line 60
def to_s(format = nil, timezone = nil)
  "#{start_description(format, timezone)} #{finish_description(format, timezone)}"
end

Private Instance Methods

compare_datetime(other) click to toggle source
# File lib/scheduled_value/timespan.rb, line 90
def compare_datetime(other)
  return nil if contains?(other)
  return -1 if finish && other >= finish
  return 1 if start && other < start
  0
end
compare_timespan(other) click to toggle source
# File lib/scheduled_value/timespan.rb, line 83
def compare_timespan(other)
  return 0 if other.start == start && other.finish == finish
  return nil if other.overlaps?(self)
  return -1 if finish && other.start && other.start >= finish
  return 1 if start && other.finish && other.finish <= start
end
convert_time_value(value) click to toggle source
# File lib/scheduled_value/timespan.rb, line 97
def convert_time_value(value)
  case value
  when String then Time.iso8601(value)
  else value
  end
end