Module: FiscalYear

Defined in:
lib/fiscal_year.rb,
lib/fiscal_year/half.rb,
lib/fiscal_year/config.rb,
lib/fiscal_year/quarter.rb,
lib/fiscal_year/version.rb,
lib/fiscal_year/year_to_date.rb,
lib/fiscal_year/invalid_start_month_error.rb

Defined Under Namespace

Classes: Config, Half, InvalidStartMonthError, Quarter, YearToDate

Constant Summary collapse

VERSION =
"1.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configFiscalYear::Config (readonly)

Returns:



18
19
20
# File 'lib/fiscal_year.rb', line 18

def config
  @config
end

Class Method Details

.configure {|config| ... } ⇒ Object

configure start month of fiscal year.

Examples:

FiscalYear.configure do |config|
  config.start_month = 6
end

Yield Parameters:



27
28
29
# File 'lib/fiscal_year.rb', line 27

def configure
  yield(@config) if block_given?
end

.cross_year?Boolean

Returns true if the fiscal year is crossed year.

Returns:

  • (Boolean)

    true if the fiscal year is crossed year.



41
42
43
# File 'lib/fiscal_year.rb', line 41

def cross_year?
  months.rindex(1) != 0
end

.cross_year_month?(month) ⇒ Boolean

If the fiscal year start from 4, the month 1, 2, 3 are crossed year.

Determines if the month passed is beyond the year, By relative to the beginning month of the fiscal year.

Parameters:

  • month (Integer)

    the month to check, 1-12.

Returns:

  • (Boolean)

    true if the month is crossed year on calendar year.



36
37
38
# File 'lib/fiscal_year.rb', line 36

def cross_year_month?(month)
  cross_year_months.include?(month)
end

.cross_year_monthsArray<Integer>

Returns the months of the crossed year.

Returns:

  • (Array<Integer>)

    the months of the crossed year.



51
52
53
54
55
56
57
# File 'lib/fiscal_year.rb', line 51

def cross_year_months
  return [] if @config.start_month == 1

  rindex = months.rindex(1).to_i

  months.slice(rindex, months.length) || raise(StandardError)
end

.decrease_year_by_month(year, month) ⇒ Integer

decrease the calendar year by month, if the month is crossed year.

for example, in case of the fiscal year start from 4, you want to obtain fiscal year from the month 1, 2, 3.

Parameters:

  • year (Integer)

    the calendar year

  • month (Integer)

    the month

Returns:

  • (Integer)

    year



93
94
95
96
97
98
99
# File 'lib/fiscal_year.rb', line 93

def decrease_year_by_month(year, month)
  if FiscalYear.cross_year_month?(month)
    year - 1
  else
    year
  end
end

.halfsArray(Array<Integer>, Array<Integer>)

Returns the first half and the second half of the fiscal year.

Returns:

  • (Array(Array<Integer>, Array<Integer>))

    the first half and the second half of the fiscal year.



60
61
62
63
# File 'lib/fiscal_year.rb', line 60

def halfs
  # @type self: singleton(FiscalYear)
  months.in_groups(2)
end

.increase_year_by_month(year, month) ⇒ Integer

increate the calendar year by month, if the month is crossed year.

for example, in case of the fiscal year start from 4, you want to obtain calendar year of the month 1, 2, 3.

Parameters:

  • year (Integer)

    the calendar year

  • month (Integer)

    the month

Returns:

  • (Integer)

    year



78
79
80
81
82
83
84
# File 'lib/fiscal_year.rb', line 78

def increase_year_by_month(year, month)
  if FiscalYear.cross_year_month?(month)
    year + 1
  else
    year
  end
end

.monthsArray<Integer>

Returns the months of the fiscal year.

Returns:

  • (Array<Integer>)

    the months of the fiscal year.



46
47
48
# File 'lib/fiscal_year.rb', line 46

def months
  (1..12).to_a.tap { |arr| arr.concat(arr.shift(@config.start_month - 1)) }
end

.passed_month_count_by(date) ⇒ Integer

start by 0.

Parameters:

  • date (Date)

    the date

Returns:

  • (Integer)

    the passed month count from the beginning of the fiscal year.

See Also:



116
117
118
# File 'lib/fiscal_year.rb', line 116

def passed_month_count_by(date)
  passed_month_count_by_month(date.month)
end

.passed_month_count_by_month(month) ⇒ Integer

start by 0.

Parameters:

  • month (Integer)

    the month

Returns:

  • (Integer)

    the passed month count from the beginning of the fiscal year.



124
125
126
# File 'lib/fiscal_year.rb', line 124

def passed_month_count_by_month(month)
  months.find_index(month) || raise(StandardError)
end

.quartersArray(Array<Integer>, Array<Integer>, Array<Integer>, Array<Integer>)

Returns the quarters of the fiscal year.

Returns:

  • (Array(Array<Integer>, Array<Integer>, Array<Integer>, Array<Integer>))

    the quarters of the fiscal year.



66
67
68
69
# File 'lib/fiscal_year.rb', line 66

def quarters
  # @type self: singleton(FiscalYear)
  months.in_groups(4)
end

.range_by(date) ⇒ Range<Date>

Returns the range of the fiscal year between beginning and end by the date.

Parameters:

  • date (Date)

    the date

Returns:

  • (Range<Date>)

    the range of the fiscal year between beginning and end by the date.



103
104
105
106
107
108
109
# File 'lib/fiscal_year.rb', line 103

def range_by(date)
  year = date.year
  month = date.month
  normalized_year = decrease_year_by_month(year, month)

  FiscalYear::Half.first_range_by(normalized_year).first..FiscalYear::Half.second_range_by(normalized_year).last
end