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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#end_dateDate (readonly)

Returns the end date of this period.

Returns:

  • (Date)

    the end date of this period.



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

def end_date
  @end_date
end

#start_dateDate (readonly)

Returns the start date of this period.

Returns:

  • (Date)

    the start date of this period.



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

def start_date
  @start_date
end

Instance Method Details

#<=>(period) ⇒ Fixnum

Test whether the given period is older, newer or equal to this one.

Returns:

  • (Fixnum)

    1 if period is older, -1 if newer, 0 if equal.

Since:

  • 0.0.3



70
71
72
73
74
75
76
77
78
# File 'lib/periods/modules/period.rb', line 70

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

#==(period) ⇒ Boolean

Test whether the given period is equal to this one. A period is considered equal if it has the same start and end date.

Examples:


period1 = Period.new('10.04.2015', '20.05.2015')
period2 = Period.new('10.04.2015', '20.05.2015')
period3 = Period.new('09.04.2015', '20.05.2015')
period1 == period2 # => true
period1 == period3 # => false

Returns:

  • (Boolean)

    true if period is equal, false otherwise.

Since:

  • 0.0.3



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

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

#daysFixnum

Returns number of days in period.

Examples:


Period.new('10.04.2015', '20.05.2015').days # => 41

Returns:

  • (Fixnum)

    the number of days in period.

Since:

  • 0.0.3



127
128
129
# File 'lib/periods/modules/period.rb', line 127

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

#end_yearFixnum

Returns end year of period.

Examples:


Period.new('10.04.2015', '20.05.2016').end_year # => 2016

Returns:

  • (Fixnum)

    end year of period.

Since:

  • 0.0.3



157
158
159
# File 'lib/periods/modules/period.rb', line 157

def end_year
  end_date.year
end

#include?(period) ⇒ Boolean

Test whether the given date or period is included in this one.

Examples:


Period.new('10.04.2015', '20.05.2015').include?('10.04.2015') # => true
Period.new('10.04.2015', '20.05.2015').include?('20.05.2015') # => true
Period.new('10.04.2015', '20.05.2015').include?('09.04.2015') # => false
Period.new('10.04.2015', '20.05.2015').include?('21.05.2015') # => false

Period.new('10.04.2015', '20.05.2015').include?(Period.new('10.04.2015', '20.05.2015')) # => true
Period.new('10.04.2015', '20.05.2015').include?(Period.new('15.04.2015', '15.05.2015')) # => true
Period.new('10.04.2015', '20.05.2015').include?(Period.new('09.04.2015', '20.05.2015')) # => false
Period.new('10.04.2015', '20.05.2015').include?(Period.new('10.04.2015', '21.05.2015')) # => false

Returns:

  • (Boolean)

    true if date or period is included, false otherwise.

Since:

  • 0.0.3



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/periods/modules/period.rb', line 180

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 = nil) ⇒ Object

Initialize a new period with its start and end date.

Examples:


period = Period.new(Date.new(2015,4,10), Date.new(2015,5,20))
period = Period.new('10.04.2015', '20.05.2015')
period = Period.new(Period.new('10.04.2015', '20.05.2015'))


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/periods/modules/period.rb', line 31

def initialize(start_date, end_date = nil)
  if start_date.is_a?(Period)
    @start_date = start_date.start_date
    @end_date = start_date.end_date
  elsif end_date.nil?
    raise ArgumentError, "Period is missing end_date, e.g. Period.new(start_date, end_date)."
  else
    @start_date = Date.parse(start_date.to_s)
    @end_date = Date.parse(end_date.to_s)
  end
end

#nextPeriod

Returns next period.

Examples:


period = Period.new('10.04.2015', '20.05.2015')
period.next # => 21.05.2015 - 30.06.2015

Returns:

  • (Period)

    the next period.

See Also:

  • Periods::Modules::Period#previous

Since:

  • 0.0.3



94
95
96
# File 'lib/periods/modules/period.rb', line 94

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

#previousPeriod

Returns previous period.

Examples:


period = Period.new('10.04.2015', '20.05.2015')
period.previous # => 28.02.2015 - 09.04.2015

Returns:

  • (Period)

    the previous period.

See Also:

  • Periods::Modules::Period#next

Since:

  • 0.0.3



112
113
114
# File 'lib/periods/modules/period.rb', line 112

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

#start_yearFixnum

Returns start year of period.

Examples:


Period.new('10.04.2015', '20.05.2016').start_year # => 2015

Returns:

  • (Fixnum)

    start year of period.

Since:

  • 0.0.3



142
143
144
# File 'lib/periods/modules/period.rb', line 142

def start_year
  start_date.year
end

#to_sString

Return string representation of period.

Examples:


Period.new('10.04.2015', '20.05.2015').to_s # => '10.04.2015 -  20.05.2015'

Returns:

  • (String)

    the string representation of period.

Since:

  • 0.0.3



204
205
206
# File 'lib/periods/modules/period.rb', line 204

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