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
- .config ⇒ FiscalYear::Config readonly
Class Method Summary collapse
-
.configure {|config| ... } ⇒ Object
configure start month of fiscal year.
-
.cross_year? ⇒ Boolean
True if the fiscal year is crossed year.
-
.cross_year_month?(month) ⇒ Boolean
If the fiscal year start from 4, the month 1, 2, 3 are crossed year.
-
.cross_year_months ⇒ Array<Integer>
The months of the crossed year.
-
.decrease_year_by_month(year, month) ⇒ Integer
decrease the calendar year by month, if the month is crossed year.
-
.halfs ⇒ Array(Array<Integer>, Array<Integer>)
The first half and the second half of the fiscal year.
-
.increase_year_by_month(year, month) ⇒ Integer
increate the calendar year by month, if the month is crossed year.
-
.months ⇒ Array<Integer>
The months of the fiscal year.
-
.passed_month_count_by(date) ⇒ Integer
start by 0.
-
.passed_month_count_by_month(month) ⇒ Integer
start by 0.
-
.quarters ⇒ Array(Array<Integer>, Array<Integer>, Array<Integer>, Array<Integer>)
The quarters of the fiscal year.
-
.range_by(date) ⇒ Range<Date>
The range of the fiscal year between beginning and end by the date.
Class Attribute Details
.config ⇒ FiscalYear::Config (readonly)
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.
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.
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.
36 37 38 |
# File 'lib/fiscal_year.rb', line 36 def cross_year_month?(month) cross_year_months.include?(month) end |
.cross_year_months ⇒ Array<Integer>
Returns 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.
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 |
.halfs ⇒ Array(Array<Integer>, Array<Integer>)
Returns 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.
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 |
.months ⇒ Array<Integer>
Returns 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.
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.
124 125 126 |
# File 'lib/fiscal_year.rb', line 124 def passed_month_count_by_month(month) months.find_index(month) || raise(StandardError) end |
.quarters ⇒ Array(Array<Integer>, Array<Integer>, Array<Integer>, Array<Integer>)
Returns 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.
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 |