Module: Calculations

Defined in:
lib/period_dates/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#available_periodsObject



5
6
7
# File 'lib/period_dates/core_ext.rb', line 5

def available_periods
  ["monthly","quarterly","biannually","semestral"]
end

#current_period_dates(period = 'monthly') ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/period_dates/core_ext.rb', line 61

def current_period_dates(period='monthly')
    bound_dates = []

    if period == 'monthly'
      bound_dates[0] = self.at_beginning_of_month
      bound_dates[1] = self.at_end_of_month
    end

    if period == 'quarterly'
      bound_dates[0] = self.at_beginning_of_quarter
      bound_dates[1] = self.at_end_of_quarter
    end

    if period == 'biannually' or period == 'semestral'
      bound_dates[0] = self.at_beginning_of_semester
      bound_dates[1] = self.at_end_of_semester
    end

    if !available_periods.include?(period)
      raise PeriodDatesIncorrectPeriod, "Period must be one of this: #{available_periods.join(', ')}."
    else
      bound_dates
    end
end

#next_period_dates(period = 'monthly') ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/period_dates/core_ext.rb', line 36

def next_period_dates(period='monthly')
    bound_dates = []

    if period == 'monthly'
      bound_dates[0] = self.next_month.at_beginning_of_month
      bound_dates[1] = self.next_month.at_end_of_month
    end

    if period == 'quarterly'
      bound_dates[0] = self.next_quarter.at_beginning_of_quarter
      bound_dates[1] = self.next_quarter.at_end_of_quarter
    end

    if period == 'biannually' or period == 'semestral'
      bound_dates[0] = self.next_semester.at_beginning_of_semester
      bound_dates[1] = self.next_semester.at_end_of_semester
    end

    if !available_periods.include?(period)
      raise PeriodDatesIncorrectPeriod, "Period must be one of this: #{available_periods.join(', ')}."
    else
      bound_dates
    end
end

#offset_period_dates(offset = 0, period = 'monthly') ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/period_dates/core_ext.rb', line 86

def offset_period_dates(offset=0,period='monthly')
    bound_dates = self.current_period_dates(period)

    if !available_periods.include?(period)
      raise PeriodDatesIncorrectPeriod, "Period must be one of this: #{available_periods.join(', ')}."
    else
      offset.abs.times do |time|
        if offset > 0
          bound_dates = bound_dates[0].next_period_dates(period)
        elsif offset < 0
          bound_dates = bound_dates[0].prev_period_dates(period)
        end
      end

      bound_dates
    end
end

#prev_period_dates(period = 'monthly') ⇒ Object Also known as: last_period_dates



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/period_dates/core_ext.rb', line 9

def prev_period_dates(period='monthly')
    bound_dates = []

    if period == 'monthly'
      bound_dates[0] = self.prev_month.at_beginning_of_month
      bound_dates[1] = self.prev_month.at_end_of_month
    end

    if period == 'quarterly'
      bound_dates[0] = self.prev_quarter.at_beginning_of_quarter
      bound_dates[1] = self.prev_quarter.at_end_of_quarter
    end

    if period == 'biannually' or period == 'semestral'
      bound_dates[0] = self.prev_semester.at_beginning_of_semester
      bound_dates[1] = self.prev_semester.at_end_of_semester
    end

    if !available_periods.include?(period)
      raise PeriodDatesIncorrectPeriod, "Period must be one of this: #{available_periods.join(', ')}."
    else
      bound_dates
    end
end