Class: Period

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(starts_at, ends_at) ⇒ Period

Returns a new instance of Period.


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

def initialize(starts_at, ends_at)

  @starts_at = starts_at
  @ends_at = ends_at
end

Instance Attribute Details

#ends_atObject (readonly)

Returns the value of attribute ends_at


3
4
5
# File 'lib/period.rb', line 3

def ends_at
  @ends_at
end

#starts_atObject (readonly)

Returns the value of attribute starts_at


3
4
5
# File 'lib/period.rb', line 3

def starts_at
  @starts_at
end

Class Method Details

.parse(start_str, end_str) ⇒ Object


16
17
18
# File 'lib/period.rb', line 16

def self.parse(start_str, end_str)
  Period.new(Time.parse(start_str), Time.parse(end_str))
end

Instance Method Details

#+(offset) ⇒ Object


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

def +(offset)
  Period.new(starts_at + offset, ends_at + offset)
end

#==(other) ⇒ Object


12
13
14
# File 'lib/period.rb', line 12

def ==(other)
  @starts_at == other.starts_at && @ends_at == other.ends_at
end

#cover?(other_period) ⇒ Boolean Also known as: intersect?, overlap?

Checks if other period overlaps with this period.

Returns true when either other_period.starts_at or other_period.ends_at is included this period.

Returns:

  • (Boolean)

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

def cover?(other_period)
  inside?(other_period.starts_at) || inside?(other_period.ends_at)
end

#include?(other_period) ⇒ Boolean

Checks if other period is inside this period.

This means that both starts_at and ends_at of other period have to be included in this period.

Returns:

  • (Boolean)

26
27
28
# File 'lib/period.rb', line 26

def include?(other_period)
  inside?(other_period.starts_at) && inside?(other_period.ends_at)
end