Class: Crono::Interval

Inherits:
Object
  • Object
show all
Defined in:
lib/crono/interval.rb

Overview

Interval describes a period between two specific times of day

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ Interval

Returns a new instance of Interval.



19
20
21
# File 'lib/crono/interval.rb', line 19

def initialize(from, to)
  @from, @to = from, to
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



4
5
6
# File 'lib/crono/interval.rb', line 4

def from
  @from
end

#toObject

Returns the value of attribute to.



4
5
6
# File 'lib/crono/interval.rb', line 4

def to
  @to
end

Class Method Details

.parse(value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/crono/interval.rb', line 6

def self.parse(value)
  from_to =
    case value
    when Array  then value
    when Hash   then value.values_at(:from, :to)
    when String then value.split('-')
    else
      fail "Unknown interval format: #{value.inspect}"
    end
  from, to = from_to.map { |v| TimeOfDay.parse(v) }
  new from, to
end

Instance Method Details

#next_within(time, period) ⇒ Object



32
33
34
35
36
37
# File 'lib/crono/interval.rb', line 32

def next_within(time, period)
  begin
    time = period.since(time)
  end until within? TimeOfDay.parse(time)
  time
end

#to_sObject



39
40
41
# File 'lib/crono/interval.rb', line 39

def to_s
  "#{@from}-#{@to}"
end

#within?(value) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
# File 'lib/crono/interval.rb', line 23

def within?(value)
  tod = ((value.is_a? TimeOfDay) ? value : TimeOfDay.parse(value))
  if @from <= @to
    tod >= @from && tod < @to
  else
    tod >= @from || tod < @to
  end
end