Module: Periods::Modules::Period::InstanceMethods

Defined in:
lib/periods/modules/period.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#end_dateObject (readonly)

Returns the value of attribute end_date.



13
14
15
# File 'lib/periods/modules/period.rb', line 13

def end_date
  @end_date
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



13
14
15
# File 'lib/periods/modules/period.rb', line 13

def start_date
  @start_date
end

Instance Method Details

#<=>(period) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/periods/modules/period.rb', line 24

def <=>(period)
  if start_date > period.start_date
    1
  elsif start_date < period.start_date
    -1
  else
    0
  end
end

#==(period) ⇒ Object



20
21
22
# File 'lib/periods/modules/period.rb', line 20

def ==(period)
  start_date == period.start_date && end_date == period.end_date
end

#daysObject



42
43
44
# File 'lib/periods/modules/period.rb', line 42

def days
  end_date.yday - start_date.yday + 1
end

#include?(period) ⇒ Boolean



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/periods/modules/period.rb', line 46

def include?(period)
  if period.is_a?(Date)
    start_date <= period && period <= end_date
  elsif period.is_a?(String)
    date = Date.parse(period.to_s)
    start_date <= date && date <= end_date
  elsif period.is_a?(Period)
    start_date <= period.start_date && period.end_date <= end_date
  else
    false
  end
end

#initialize(start_date, end_date) ⇒ Object



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

def initialize(start_date, end_date)
  @start_date = Date.parse(start_date.to_s)
  @end_date = Date.parse(end_date.to_s)
end

#nextObject



34
35
36
# File 'lib/periods/modules/period.rb', line 34

def next
  self.class.new(start_date + days, end_date + days)
end

#previousObject



38
39
40
# File 'lib/periods/modules/period.rb', line 38

def previous
  self.class.new(start_date - days, end_date - days)
end

#to_sObject



59
60
61
# File 'lib/periods/modules/period.rb', line 59

def to_s
  "#{start_date.strftime("%d.%m.%Y")} - #{end_date.strftime("%d.%m.%Y")}"
end