Class: Date

Inherits:
Object
  • Object
show all
Defined in:
lib/active_support/fiscal_dates/date.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.calendar_year_ranges_between(start_date, end_date) ⇒ Object



23
24
25
# File 'lib/active_support/fiscal_dates/date.rb', line 23

def self.calendar_year_ranges_between(start_date, end_date)
  (start_date.year..end_date.year).map { |year| Date.new(year).calendar_year_range }
end

.current_fiscal_yearObject



2
3
4
5
6
# File 'lib/active_support/fiscal_dates/date.rb', line 2

def self.current_fiscal_year
  # Returns "2015" for FY 2014-15
  today = Date.today
  today.month < 7 ? today.year : today.year + 1
end

.current_fiscal_year_rangeObject



8
9
10
11
12
13
14
15
# File 'lib/active_support/fiscal_dates/date.rb', line 8

def self.current_fiscal_year_range
  today = Date.today
  if today.month < 7
    Date.new(today.year - 1, 7)..Date.new(today.year, 6, 30)
  else
    Date.new(today.year, 7)..Date.new(today.year + 1, 6, 30)
  end
end

.fiscal_year_label(year) ⇒ Object



45
46
47
48
49
# File 'lib/active_support/fiscal_dates/date.rb', line 45

def self.fiscal_year_label(year)
  # Output example: [["2012-13", "2013"], ["2013-14", "2014"]]

  "#{year - 1}-#{year.to_s[-2, 2]}"
end

.fiscal_year_ranges_between(start_date, end_date) ⇒ Object



17
18
19
20
21
# File 'lib/active_support/fiscal_dates/date.rb', line 17

def self.fiscal_year_ranges_between(start_date, end_date)
  (start_date.fiscal_year_first_day.year..end_date.fiscal_year_first_day.year).map do |year|
    Date.new(year, 7).fiscal_year_range
  end
end

.fiscal_years_since(start_year) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_support/fiscal_dates/date.rb', line 27

def self.fiscal_years_since(start_year)
  # Includes the current fiscal year
  # Output gets set in .fiscal_year_label
  # Suitable for year select dropdowns in Corporate.

  result = []
  today = Date.today
  current_year = today.year.to_i
  month = today.month.to_i
  end_year = month >= 7 ? current_year + 1 : current_year

  (start_year..end_year).each do |year|
    result << [fiscal_year_label(year), year.to_s]
  end

  result
end

Instance Method Details

#calendar_year_first_dayObject



75
76
77
# File 'lib/active_support/fiscal_dates/date.rb', line 75

def calendar_year_first_day
  Date.new(self.year)
end

#calendar_year_last_dayObject



79
80
81
# File 'lib/active_support/fiscal_dates/date.rb', line 79

def calendar_year_last_day
  Date.new(self.year).end_of_year
end

#calendar_year_rangeObject



83
84
85
# File 'lib/active_support/fiscal_dates/date.rb', line 83

def calendar_year_range
  calendar_year_first_day..calendar_year_last_day
end

#fiscal_yearObject



51
52
53
# File 'lib/active_support/fiscal_dates/date.rb', line 51

def fiscal_year
  self.month < 7 ? self.year : self.year + 1
end

#fiscal_year_first_dayObject



55
56
57
58
59
60
61
# File 'lib/active_support/fiscal_dates/date.rb', line 55

def fiscal_year_first_day
  if self.month < 7
    Date.new(self.year - 1, 7)
  else
    Date.new(self.year, 7)
  end
end

#fiscal_year_last_dayObject



63
64
65
66
67
68
69
# File 'lib/active_support/fiscal_dates/date.rb', line 63

def fiscal_year_last_day
  if self.month < 7
    Date.new(self.year, 6).end_of_month
  else
    Date.new(self.year + 1, 6).end_of_month
  end
end

#fiscal_year_rangeObject



71
72
73
# File 'lib/active_support/fiscal_dates/date.rb', line 71

def fiscal_year_range
  fiscal_year_first_day..fiscal_year_last_day
end