Class: Aenea::TimeWindow

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

Defined Under Namespace

Classes: Type

Constant Summary collapse

SEPARATOR =
' - '

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_tod, end_tod_or_window_in_seconds) ⇒ TimeWindow

Returns a new instance of TimeWindow.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/aenea/time_window.rb', line 7

def initialize(start_tod, end_tod_or_window_in_seconds)
  @starting = start_tod.dup

  if end_tod_or_window_in_seconds.is_a?(Tod::TimeOfDay)
    @ending = end_tod_or_window_in_seconds.dup
  else
    @ending = @starting + end_tod_or_window_in_seconds # number of seconds
  end

  if ending < starting
    raise ArgumentError.new("invalid window #{starting} - #{ending}")
  end

  self.freeze
end

Instance Attribute Details

#endingObject (readonly)

Returns the value of attribute ending.



5
6
7
# File 'lib/aenea/time_window.rb', line 5

def ending
  @ending
end

#startingObject (readonly)

Returns the value of attribute starting.



5
6
7
# File 'lib/aenea/time_window.rb', line 5

def starting
  @starting
end

Class Method Details

.parse(str) ⇒ Object



23
24
25
# File 'lib/aenea/time_window.rb', line 23

def self.parse(str)
  new(*str.split(SEPARATOR).collect {|s| Tod::TimeOfDay.parse(s) })
end

.parse_tuple(str) ⇒ Object

a row of PL/pgsql return type table(time, time), returned as a varchar using SELECT func_… rather than SELECT * FROM func_… - the returned values are in the form “(11:00:00,13:00:00)”



30
31
32
33
# File 'lib/aenea/time_window.rb', line 30

def self.parse_tuple(str)
  elements = str.sub(/^\(/, '').sub(/\)$/, '').split(',')
  new(*elements.map {|s| Tod::TimeOfDay.parse(s) })
end

Instance Method Details

#<=>(obj) ⇒ Object



39
40
41
42
43
44
# File 'lib/aenea/time_window.rb', line 39

def <=>(obj)
  start_comp = self.starting <=> obj.starting
  return start_comp if start_comp != 0

  self.ending <=> obj.ending
end

#==(obj) ⇒ Object



35
36
37
# File 'lib/aenea/time_window.rb', line 35

def ==(obj)
  self.starting == obj.starting && ending == obj.ending
end

#inspectObject



58
59
60
# File 'lib/aenea/time_window.rb', line 58

def inspect
  "<#{to_s}>"
end

#to_ampmObject



50
51
52
# File 'lib/aenea/time_window.rb', line 50

def to_ampm
  starting.to_ampm + SEPARATOR + ending.to_ampm
end

#to_sObject



46
47
48
# File 'lib/aenea/time_window.rb', line 46

def to_s
  starting.to_s + SEPARATOR + ending.to_s
end

#to_select_valueObject



54
55
56
# File 'lib/aenea/time_window.rb', line 54

def to_select_value
  [to_ampm, to_s]
end