Class: ScheduledValue::Timespan

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/scheduled_value/timespan.rb

Direct Known Subclasses

TimespanWithValue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start: nil, finish: nil) ⇒ Timespan

Returns a new instance of Timespan.



9
10
11
12
13
14
# 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

Instance Attribute Details

#finishObject

Returns the value of attribute finish.



7
8
9
# File 'lib/scheduled_value/timespan.rb', line 7

def finish
  @finish
end

#startObject

Returns the value of attribute start.



7
8
9
# File 'lib/scheduled_value/timespan.rb', line 7

def start
  @start
end

Instance Method Details

#<=>(other) ⇒ Object



45
46
47
48
49
50
# File 'lib/scheduled_value/timespan.rb', line 45

def <=>(other)
  case other
  when Timespan then compare_timespan(other)
  when Date, Time, DateTime then compare_datetime(other)
  end
end

#attributesObject



16
17
18
19
20
21
# File 'lib/scheduled_value/timespan.rb', line 16

def attributes
  {
    start: start,
    finish: finish
  }
end

#contains?(timestamp) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'lib/scheduled_value/timespan.rb', line 31

def contains?(timestamp)
  return false if start && timestamp < start
  return false if finish && timestamp >= finish

  true
end

#finish_description(format = nil) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/scheduled_value/timespan.rb', line 68

def finish_description(format = nil)
  if finish
    "up to #{finish.to_s(format)}"
  elsif start
    "indefinitely"
  end
end

#inspectObject



52
53
54
# File 'lib/scheduled_value/timespan.rb', line 52

def inspect
  "#<#{self.class.name}: #{self}>"
end

#overlaps?(other) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/scheduled_value/timespan.rb', line 38

def overlaps?(other)
  return false if finish && other.start && other.start >= finish
  return false if start && other.finish && start >= other.finish

  true
end

#start_description(format = nil) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/scheduled_value/timespan.rb', line 60

def start_description(format = nil)
  if start
    "from #{start.to_s(format)}"
  else
    'anytime'
  end
end

#to_s(format = nil) ⇒ Object



56
57
58
# File 'lib/scheduled_value/timespan.rb', line 56

def to_s(format = nil)
  "#{start_description(format)} #{finish_description(format)}"
end