Class: Comee::Core::Period

Inherits:
Object
  • Object
show all
Defined in:
app/utils/comee/core/period.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, finish) ⇒ Period

Returns a new instance of Period.



6
7
8
9
# File 'app/utils/comee/core/period.rb', line 6

def initialize(start, finish)
  @start = start
  @finish = finish
end

Instance Attribute Details

#finishObject

Returns the value of attribute finish.



4
5
6
# File 'app/utils/comee/core/period.rb', line 4

def finish
  @finish
end

#startObject

Returns the value of attribute start.



4
5
6
# File 'app/utils/comee/core/period.rb', line 4

def start
  @start
end

Class Method Details

.compute_next_valid_period(period1, period2) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'app/utils/comee/core/period.rb', line 69

def self.compute_next_valid_period(period1, period2)
  return period2 if period1.disjoint?(period2) && period1 < period2

  return nil if period1 > period2 || period1.contains?(period2)

  period = Period.new(period1.finish.advance(days: 1), period2.finish)
  return nil unless period.valid?

  period
end

Instance Method Details

#<(other) ⇒ Object



15
16
17
# File 'app/utils/comee/core/period.rb', line 15

def <(other)
  instance_of?(other.class) && @start < other.start && @finish < other.finish
end

#==(other) ⇒ Object



11
12
13
# File 'app/utils/comee/core/period.rb', line 11

def ==(other)
  self.class == other.class && @start == other.start && @finish == other.finish
end

#>(other) ⇒ Object



19
20
21
# File 'app/utils/comee/core/period.rb', line 19

def >(other)
  instance_of?(other.class) && @start > other.start && @finish > other.finish
end

#contained_by?(period) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'app/utils/comee/core/period.rb', line 51

def contained_by?(period)
  return true if start >= period.start && finish <= period.finish

  false
end

#contains?(period) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'app/utils/comee/core/period.rb', line 45

def contains?(period)
  return true if start <= period.start && finish >= period.finish

  false
end

#current?Boolean

Returns:

  • (Boolean)

Raises:

  • (StandardError)


33
34
35
36
37
# File 'app/utils/comee/core/period.rb', line 33

def current?
  raise(StandardError, "Period must be valid.") unless valid?

  start <= Date.current && finish > Date.current
end

#disjoint?(period) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/utils/comee/core/period.rb', line 57

def disjoint?(period)
  @finish < period.start || period.finish < @start
end

#future?Boolean

Returns:

  • (Boolean)

Raises:

  • (StandardError)


39
40
41
42
43
# File 'app/utils/comee/core/period.rb', line 39

def future?
  raise(StandardError, "Period must be valid.") unless valid?

  start > Date.current
end

#overlaps?(period) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
# File 'app/utils/comee/core/period.rb', line 61

def overlaps?(period)
  return true if period.start <= @start && @start <= period.finish

  return true if @start <= period.start && period.start <= @finish

  false
end

#past?Boolean

Returns:

  • (Boolean)

Raises:

  • (StandardError)


27
28
29
30
31
# File 'app/utils/comee/core/period.rb', line 27

def past?
  raise(StandardError, "Period must be valid.") unless valid?

  finish < Date.current
end

#valid?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/utils/comee/core/period.rb', line 23

def valid?
  finish > start
end