Class: ClockTime::Span

Inherits:
Object
  • Object
show all
Defined in:
lib/clock_time/span.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ Span

Returns a new instance of Span.



12
13
14
15
16
17
18
# File 'lib/clock_time/span.rb', line 12

def initialize(from, to)
  if !from.is_a?(ClockTime) || !to.is_a?(ClockTime)
    raise ArgumentError, 'invalid span'
  end
  @from = from
  @to = to
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



2
3
4
# File 'lib/clock_time/span.rb', line 2

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



2
3
4
# File 'lib/clock_time/span.rb', line 2

def to
  @to
end

Class Method Details

.parse(from_string, to_string) ⇒ Object



5
6
7
8
9
# File 'lib/clock_time/span.rb', line 5

def parse(from_string, to_string)
  from = ClockTime.parse(from_string)
  to = ClockTime.parse(to_string)
  new(from, to)
end

Instance Method Details

#==(span) ⇒ Object



31
32
33
# File 'lib/clock_time/span.rb', line 31

def ==(span)
  from == span.from && to == span.to
end

#durationObject



20
21
22
23
# File 'lib/clock_time/span.rb', line 20

def duration
  return @to - @from + 24.hours if @to < @from
  @to - @from
end

#to_times(date) ⇒ Object



25
26
27
28
29
# File 'lib/clock_time/span.rb', line 25

def to_times(date)
  from_t = from.to_time(date)
  to_t = to.next_time(from_t)
  return [from_t, to_t]
end