fiscal_year 0.6.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/lib/fiscal_year/config.rb +42 -1
- data/lib/fiscal_year/half.rb +43 -13
- data/lib/fiscal_year/invalid_start_month_error.rb +9 -0
- data/lib/fiscal_year/quarter.rb +44 -4
- data/lib/fiscal_year/version.rb +1 -1
- data/lib/fiscal_year/year_to_date.rb +51 -28
- data/lib/fiscal_year.rb +52 -4
- data/sig/fiscal_year/config.rbs +4 -0
- data/sig/fiscal_year/half.rbs +3 -0
- data/sig/fiscal_year/invalid_start_month_error.rbs +5 -0
- data/sig/fiscal_year/quarter.rbs +3 -1
- data/sig/fiscal_year.rbs +3 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a66a9750908c6de12be50a3ddac4da7070d3e7b43898151ada109d586e26d983
|
4
|
+
data.tar.gz: 407e156b9e5e8f154c68059a652b62cb0016c8aa901b69686c6d8046aa599a6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89fe574b9f0e5cd38a7b2add1caf81d1fdfec9fcf4a321da81aa11ffba72e25df925a343ba20703b66cece55407e40da6b30b101baa9e4e9e543836d71db6ab1
|
7
|
+
data.tar.gz: 4a67105553886931e1868e47b724b5dc983f507961625a7fb498d45697342ba1fe91aa951320faabf6e1c10c8a0bc47dd856b89675b0f06804077553b1f4f4e8
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
## [1.0.0] - 2024-07-24
|
2
|
+
celebrate 1.0.0 release!! 🎉
|
3
|
+
No Breaking Changes, Some futures.
|
4
|
+
|
5
|
+
- add `passed_month_count_by` and `passed_month_count_by_month` to each FiscalYear, Half, Quarter.
|
6
|
+
- fix wrong example comment on `FiscalYear::YearToDate.quarter_range_by`.
|
7
|
+
- robust `Config.start_month`. you can no longer pass invalid month. now only pass number 1-12, or short symbol like `:jan`, `:dec`.
|
8
|
+
|
9
|
+
Thank you for gave me courage to release, ruby-jp!
|
10
|
+
|
11
|
+
## [0.7.0] - 2024-07-11
|
12
|
+
- add YARD comment.
|
13
|
+
- fix `FiscalYear::YearToDate.quarter_range_by` logic.
|
14
|
+
|
1
15
|
## [0.6.0] - 2024-05-15
|
2
16
|
- [BREAK] Ruby 3.0.x, 2.7.x and 2.6.x has no longer support.
|
3
17
|
- add `FiscalYear.range_by` method
|
data/lib/fiscal_year/config.rb
CHANGED
@@ -1,11 +1,52 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "fiscal_year/invalid_start_month_error"
|
4
|
+
|
3
5
|
module FiscalYear
|
4
6
|
class Config
|
5
|
-
|
7
|
+
attr_reader :start_month
|
8
|
+
|
9
|
+
VALID_START_MONTHS = {
|
10
|
+
jan: 1,
|
11
|
+
feb: 2,
|
12
|
+
mar: 3,
|
13
|
+
apr: 4,
|
14
|
+
may: 5,
|
15
|
+
jun: 6,
|
16
|
+
jul: 7,
|
17
|
+
aug: 8,
|
18
|
+
sep: 9,
|
19
|
+
oct: 10,
|
20
|
+
nov: 11,
|
21
|
+
dec: 12
|
22
|
+
}.freeze
|
6
23
|
|
7
24
|
def initialize
|
8
25
|
@start_month = 4
|
9
26
|
end
|
27
|
+
|
28
|
+
def start_month=(month)
|
29
|
+
valid_start_month?(month)
|
30
|
+
|
31
|
+
@start_month = fetch_start_month(month)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def valid_start_month?(month)
|
37
|
+
if month.is_a? Integer
|
38
|
+
VALID_START_MONTHS.values.include?(month)
|
39
|
+
else
|
40
|
+
VALID_START_MONTHS.keys.include?(month)
|
41
|
+
end || raise(InvalidStartMonthError)
|
42
|
+
end
|
43
|
+
|
44
|
+
def fetch_start_month(month)
|
45
|
+
if month.is_a? Integer
|
46
|
+
VALID_START_MONTHS.rassoc(month)&.last
|
47
|
+
else
|
48
|
+
VALID_START_MONTHS.assoc(month)&.last
|
49
|
+
end || raise(StandardError)
|
50
|
+
end
|
10
51
|
end
|
11
52
|
end
|
data/lib/fiscal_year/half.rb
CHANGED
@@ -3,47 +3,55 @@
|
|
3
3
|
module FiscalYear
|
4
4
|
class Half
|
5
5
|
class << self
|
6
|
+
# @return [Array<Integer>] the first half of the fiscal year.
|
6
7
|
def first
|
7
|
-
|
8
|
-
return first if first.is_a? Array
|
9
|
-
|
10
|
-
[]
|
8
|
+
FiscalYear.halfs.first || raise(StandardError)
|
11
9
|
end
|
12
10
|
|
11
|
+
# @return [Array<Integer>] the second half of the fiscal year.
|
13
12
|
def second
|
14
|
-
|
15
|
-
return second if second.is_a? Array
|
16
|
-
|
17
|
-
[]
|
13
|
+
FiscalYear.halfs.second || raise(StandardError)
|
18
14
|
end
|
19
15
|
|
16
|
+
# @param month [Integer] the month
|
17
|
+
# @return [Boolean] true if the month is in the first half of the fiscal year.
|
20
18
|
def first?(month)
|
21
19
|
FiscalYear.halfs.first.include?(month)
|
22
20
|
end
|
23
21
|
|
22
|
+
# @param month [Integer] the month
|
23
|
+
# @return [Boolean] true if the month is in the second half of the fiscal year.
|
24
24
|
def second?(month)
|
25
25
|
!first?(month)
|
26
26
|
end
|
27
27
|
|
28
|
+
# @param month [Integer] the month
|
29
|
+
# @return [Array<Integer>] the half months by the month.
|
30
|
+
def months(month)
|
31
|
+
first?(month) ? first : second
|
32
|
+
end
|
33
|
+
|
34
|
+
# @param year [Integer] the calendar year
|
35
|
+
# @return [Range<Date>] the range of the first half of the fiscal year.
|
28
36
|
def first_range_by(year)
|
29
37
|
# care Date#parse 2 digit year auto complete.
|
30
38
|
# 99 + 1 = 100, but expect 2000 this context.
|
31
39
|
year = 1999 if year == 99
|
32
|
-
end_month = first.last
|
33
|
-
raise StandardError if end_month.nil?
|
40
|
+
end_month = first.last || raise(StandardError)
|
34
41
|
|
35
42
|
end_year = FiscalYear.increase_year_by_month(year, end_month)
|
36
43
|
|
37
44
|
Date.parse("#{year}/#{first.first}/01")..Date.parse("#{end_year}/#{end_month}/01").end_of_month
|
38
45
|
end
|
39
46
|
|
47
|
+
# @param year [Integer] the calendar year
|
48
|
+
# @return [Range<Date>] the range of the second half of the fiscal year.
|
40
49
|
def second_range_by(year)
|
41
50
|
# care Date#parse 2 digit year auto complete.
|
42
51
|
# 99 + 1 = 100, but expect 2000 this context.
|
43
52
|
year = 1999 if year == 99
|
44
|
-
first_month = second.first
|
45
|
-
end_month = second.last
|
46
|
-
raise StandardError if first_month.nil? || end_month.nil?
|
53
|
+
first_month = second.first || raise(StandardError)
|
54
|
+
end_month = second.last || raise(StandardError)
|
47
55
|
|
48
56
|
start_year = FiscalYear.increase_year_by_month(year, first_month)
|
49
57
|
end_year = FiscalYear.increase_year_by_month(year, end_month)
|
@@ -51,6 +59,8 @@ module FiscalYear
|
|
51
59
|
Date.parse("#{start_year}/#{first_month}/01")..Date.parse("#{end_year}/#{end_month}/01").end_of_month
|
52
60
|
end
|
53
61
|
|
62
|
+
# @param date [Date] the date
|
63
|
+
# @return [Range<Date>] the range of the half of the fiscal year by the date.
|
54
64
|
def range_by(date)
|
55
65
|
month = date.month
|
56
66
|
year = date.year
|
@@ -60,9 +70,29 @@ module FiscalYear
|
|
60
70
|
first?(month) ? first_range_by(year) : second_range_by(year)
|
61
71
|
end
|
62
72
|
|
73
|
+
# @param half [Array<Integer>] the half of the fiscal year
|
74
|
+
# @return [Boolean] true if the any month of half are crossed calendar year.
|
63
75
|
def cross_year_in_half?(half)
|
64
76
|
FiscalYear.cross_year? && half.any? { |month| month == 12 }
|
65
77
|
end
|
78
|
+
|
79
|
+
# start by 0.
|
80
|
+
#
|
81
|
+
# @see passed_month_count_by_month
|
82
|
+
# @param date [Date] the date
|
83
|
+
# @return [Integer] the passed month count from the beginning of the half.
|
84
|
+
def passed_month_count_by(date)
|
85
|
+
passed_month_count_by_month(date.month)
|
86
|
+
end
|
87
|
+
|
88
|
+
# start by 0.
|
89
|
+
#
|
90
|
+
# @see passed_month_count_by_month
|
91
|
+
# @param date [Date] the date
|
92
|
+
# @return [Integer] the passed month count from the beginning of the half.
|
93
|
+
def passed_month_count_by_month(month)
|
94
|
+
months(month).find_index(month) || raise(StandardError)
|
95
|
+
end
|
66
96
|
end
|
67
97
|
end
|
68
98
|
end
|
data/lib/fiscal_year/quarter.rb
CHANGED
@@ -3,35 +3,55 @@
|
|
3
3
|
module FiscalYear
|
4
4
|
class Quarter
|
5
5
|
class << self
|
6
|
+
# @!method first
|
7
|
+
# @return [Array<Integer>] the first quarter of the fiscal year.
|
8
|
+
|
9
|
+
# @!method second
|
10
|
+
# @return [Array<Integer>] the second quarter of the fiscal year.
|
11
|
+
|
12
|
+
# @!method third
|
13
|
+
# @return [Array<Integer>] the third quarter of the fiscal year.
|
14
|
+
|
15
|
+
# @!method fourth
|
16
|
+
# @return [Array<Integer>] the fourth quarter of the fiscal year.
|
6
17
|
%i[first second third fourth].each do |method_name|
|
7
18
|
define_method(method_name) do
|
8
19
|
FiscalYear.quarters.public_send(method_name)
|
9
20
|
end
|
10
21
|
end
|
11
22
|
|
23
|
+
# @param month [Integer] the month
|
24
|
+
# @return [Boolean] true if the month is in the first quarter of the fiscal year.
|
12
25
|
def first?(month)
|
13
26
|
FiscalYear.quarters.first.include?(month)
|
14
27
|
end
|
15
28
|
|
29
|
+
# @param month [Integer] the month
|
30
|
+
# @return [Boolean] true if the month is in the second quarter of the fiscal year.
|
16
31
|
def second?(month)
|
17
32
|
FiscalYear.quarters.second.include?(month)
|
18
33
|
end
|
19
34
|
|
35
|
+
# @param month [Integer] the month
|
36
|
+
# @return [Boolean] true if the month is in the third quarter of the fiscal year.
|
20
37
|
def third?(month)
|
21
38
|
FiscalYear.quarters.third.include?(month)
|
22
39
|
end
|
23
40
|
|
41
|
+
# @param month [Integer] the month
|
42
|
+
# @return [Boolean] true if the month is in the fourth quarter of the fiscal year.
|
24
43
|
def fourth?(month)
|
25
44
|
FiscalYear.quarters.fourth.include?(month)
|
26
45
|
end
|
27
46
|
|
47
|
+
# @param month [Integer] the month
|
48
|
+
# @return [Array<Integer>] the quarter months by the month.
|
28
49
|
def months(month)
|
29
|
-
|
30
|
-
raise ::StandardError if months.nil?
|
31
|
-
|
32
|
-
months
|
50
|
+
FiscalYear.quarters.find { |a| a.include?(month) } || raise(StandardError)
|
33
51
|
end
|
34
52
|
|
53
|
+
# @param date [Date] the date
|
54
|
+
# @return [Range<Date>] the range of the quarter by the date.
|
35
55
|
def range_by(date)
|
36
56
|
year = date.year
|
37
57
|
this_quarter = months(date.month)
|
@@ -40,15 +60,35 @@ module FiscalYear
|
|
40
60
|
Date.parse("#{year}/#{this_quarter.first}/01")..Date.parse("#{last_year}/#{this_quarter.last}/01").end_of_month
|
41
61
|
end
|
42
62
|
|
63
|
+
# @param month [Integer] the month
|
64
|
+
# @return [Integer] the quarter number by the month.
|
43
65
|
def quarter_num(month)
|
44
66
|
rindex = FiscalYear.quarters.rindex(months(month))
|
45
67
|
|
46
68
|
rindex.nil? ? 0 : (rindex + 1)
|
47
69
|
end
|
48
70
|
|
71
|
+
# @param quarter [Array<Integer>] the quarter
|
72
|
+
# @return [Boolean] true if the quarter is crossed calendar year.
|
49
73
|
def cross_year_in_quarter?(quarter)
|
50
74
|
FiscalYear.cross_year? && quarter.any? { |month| month == 12 }
|
51
75
|
end
|
76
|
+
|
77
|
+
# start by 0.
|
78
|
+
#
|
79
|
+
# @param date [Date] the date
|
80
|
+
# @return [Integer] the passed quarter count by the date.
|
81
|
+
def passed_month_count_by(date)
|
82
|
+
passed_month_count_by_month(date.month)
|
83
|
+
end
|
84
|
+
|
85
|
+
# start by 0.
|
86
|
+
#
|
87
|
+
# @param month [Integer] the month
|
88
|
+
# @return [Integer] the passed quarter count by the month.
|
89
|
+
def passed_month_count_by_month(month)
|
90
|
+
months(month).find_index(month) || raise(StandardError)
|
91
|
+
end
|
52
92
|
end
|
53
93
|
end
|
54
94
|
end
|
data/lib/fiscal_year/version.rb
CHANGED
@@ -3,6 +3,14 @@
|
|
3
3
|
module FiscalYear
|
4
4
|
class YearToDate
|
5
5
|
class << self
|
6
|
+
# @param date [Date] the date
|
7
|
+
# @return [Range<Date>] the range of the year to date.
|
8
|
+
# @example
|
9
|
+
# FiscalYear::YearToDate.range_by(Date.parse("2021-08-01"))
|
10
|
+
# => Thu, 01 Apr 2021..Tue, 31 Aug 2021
|
11
|
+
#
|
12
|
+
# FiscalYear::YearToDate.range_by(Date.parse("2021-01-01"))
|
13
|
+
# => Wed, 01 Apr 2020..Sun, 31 Jan 2021
|
6
14
|
def range_by(date)
|
7
15
|
year = date.year
|
8
16
|
month = date.month
|
@@ -14,45 +22,60 @@ module FiscalYear
|
|
14
22
|
end
|
15
23
|
end
|
16
24
|
|
17
|
-
#
|
18
|
-
|
25
|
+
# @param date [Date] the date
|
26
|
+
# @return [Array<Array<Integer>, Array<Integer>>] the passed year and month pairs.
|
27
|
+
# @example
|
28
|
+
# FiscalYear::YearToDate.year_month_pairs(Date.parse("2021-08-01"))
|
29
|
+
# => [[2021, 4], [2021, 5], [2021, 6], [2021, 7], [2021, 8]]
|
30
|
+
#
|
31
|
+
# FiscalYear::YearToDate.year_month_pairs(Date.parse("2021-01-01"))
|
32
|
+
# => [
|
33
|
+
# [2020, 4], [2020, 5], [2020, 6], [2020, 7], [2020, 8], [2020, 9],
|
34
|
+
# [2020, 10], [2020, 11], [2020, 12], [2021, 1]
|
35
|
+
# ]
|
36
|
+
def year_month_pairs(date)
|
19
37
|
month = date.month
|
20
38
|
month_index = FiscalYear.months.index(month)
|
21
39
|
months = FiscalYear.months[(0..month_index)]
|
22
40
|
raise StandardError if months.nil?
|
23
41
|
|
24
42
|
[date.year].product(months).map do |e|
|
25
|
-
|
26
|
-
(FiscalYear.cross_year_month?(e.second) ? e : [e.first - 1, e.second])
|
27
|
-
else
|
28
|
-
e
|
29
|
-
end
|
43
|
+
[FiscalYear.decrease_year_by_month(e.first, e.second), e.second]
|
30
44
|
end
|
31
45
|
end
|
32
46
|
|
33
|
-
#
|
34
|
-
|
47
|
+
# @param date [Date] the date
|
48
|
+
# @return [Range<Date>] the range of the half year to date.
|
49
|
+
# @example
|
50
|
+
# FiscalYear::YearToDate.half_range_by(Date.parse("2021-08-01"))
|
51
|
+
# => Thu, 01 Apr 2021..Tue, 31 Aug 2021
|
52
|
+
#
|
53
|
+
# FiscalYear::YearToDate.half_range_by(Date.parse("2021-01-01"))
|
54
|
+
# => Thu, 01 Oct 2020..Sun, 31 Jan 2021
|
55
|
+
def half_range_by(date)
|
35
56
|
year = date.year
|
36
57
|
month = date.month
|
37
58
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
59
|
+
beginning_year =
|
60
|
+
if Half.first?(month)
|
61
|
+
FiscalYear.cross_year_month?(month) ? year - 1 : year
|
62
|
+
else
|
63
|
+
Half.cross_year_in_half?(Half.second) ? FiscalYear.decrease_year_by_month(year, month) : year
|
64
|
+
end
|
65
|
+
|
66
|
+
half_method = Half.first?(month) ? :first : :second
|
43
67
|
|
44
|
-
Date.parse(
|
45
|
-
"#{
|
46
|
-
if FiscalYear::Half.cross_year_in_half?(Half.second) &&
|
47
|
-
FiscalYear.cross_year_month?(month)
|
48
|
-
year - 1
|
49
|
-
else
|
50
|
-
year
|
51
|
-
end
|
52
|
-
}/#{Half.second.first}"
|
53
|
-
)..date.end_of_month.to_date
|
68
|
+
Date.parse("#{beginning_year}/#{Half.public_send(half_method).first}/01")..date.end_of_month.to_date
|
54
69
|
end
|
55
70
|
|
71
|
+
# @param date [Date] the date
|
72
|
+
# @return [Range<Date>] the range of the quarter to date.
|
73
|
+
# @example
|
74
|
+
# FiscalYear::YearToDate.quarter_range_by(Date.parse("2021-08-01"))
|
75
|
+
# => Thu, 01 Jul 2021..Tue, 31 Aug 2021
|
76
|
+
#
|
77
|
+
# FiscalYear::YearToDate.quarter_range_by(Date.parse("2021-01-01"))
|
78
|
+
# => Fri, 01 Jan 2021..Sun, 31 Jan 2021
|
56
79
|
def quarter_range_by(date)
|
57
80
|
year = date.year
|
58
81
|
month = date.month
|
@@ -63,10 +86,10 @@ module FiscalYear
|
|
63
86
|
|
64
87
|
quarter = Quarter.public_send(quarter_method)
|
65
88
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
)
|
89
|
+
beginning_year =
|
90
|
+
Quarter.cross_year_in_quarter?(quarter) ? FiscalYear.decrease_year_by_month(year, month) : year
|
91
|
+
|
92
|
+
Date.parse("#{beginning_year}/#{quarter.first}/01")..date.end_of_month.to_date
|
70
93
|
end
|
71
94
|
end
|
72
95
|
end
|
data/lib/fiscal_year.rb
CHANGED
@@ -14,45 +14,67 @@ module FiscalYear
|
|
14
14
|
# @type ivar @config: FiscalYear::Config
|
15
15
|
@config ||= FiscalYear::Config.new
|
16
16
|
class << self
|
17
|
+
# @return [FiscalYear::Config]
|
17
18
|
attr_reader :config
|
18
19
|
|
20
|
+
# configure start month of fiscal year.
|
21
|
+
#
|
22
|
+
# @yieldparam config [FiscalYear::Config]
|
23
|
+
# @example
|
24
|
+
# FiscalYear.configure do |config|
|
25
|
+
# config.start_month = 6
|
26
|
+
# end
|
19
27
|
def configure
|
20
28
|
yield(@config) if block_given?
|
21
29
|
end
|
22
30
|
|
31
|
+
# If the fiscal year start from 4, the month 1, 2, 3 are crossed year.
|
32
|
+
#
|
33
|
+
# @param month [Integer] the month to check, 1-12.
|
34
|
+
# @return [Boolean] true if the month is crossed year on calendar year.
|
35
|
+
# Determines if the month passed is beyond the year, By relative to the beginning month of the fiscal year.
|
23
36
|
def cross_year_month?(month)
|
24
37
|
cross_year_months.include?(month)
|
25
38
|
end
|
26
39
|
|
40
|
+
# @return [Boolean] true if the fiscal year is crossed year.
|
27
41
|
def cross_year?
|
28
42
|
months.rindex(1) != 0
|
29
43
|
end
|
30
44
|
|
45
|
+
# @return [Array<Integer>] the months of the fiscal year.
|
31
46
|
def months
|
32
47
|
(1..12).to_a.tap { |arr| arr.concat(arr.shift(@config.start_month - 1)) }
|
33
48
|
end
|
34
49
|
|
50
|
+
# @return [Array<Integer>] the months of the crossed year.
|
35
51
|
def cross_year_months
|
36
52
|
return [] if @config.start_month == 1
|
37
53
|
|
38
54
|
rindex = months.rindex(1).to_i
|
39
55
|
|
40
|
-
|
41
|
-
raise StandardError if m.nil?
|
42
|
-
|
43
|
-
m
|
56
|
+
months.slice(rindex, months.length) || raise(StandardError)
|
44
57
|
end
|
45
58
|
|
59
|
+
# @return [Array(Array<Integer>, Array<Integer>)] the first half and the second half of the fiscal year.
|
46
60
|
def halfs
|
47
61
|
# @type self: singleton(FiscalYear)
|
48
62
|
months.in_groups(2)
|
49
63
|
end
|
50
64
|
|
65
|
+
# @return [Array(Array<Integer>, Array<Integer>, Array<Integer>, Array<Integer>)] the quarters of the fiscal year.
|
51
66
|
def quarters
|
52
67
|
# @type self: singleton(FiscalYear)
|
53
68
|
months.in_groups(4)
|
54
69
|
end
|
55
70
|
|
71
|
+
# increate the calendar year by month, if the month is crossed year.
|
72
|
+
#
|
73
|
+
# for example, in case of the fiscal year start from 4, you want to obtain calendar year of the month 1, 2, 3.
|
74
|
+
#
|
75
|
+
# @param year [Integer] the calendar year
|
76
|
+
# @param month [Integer] the month
|
77
|
+
# @return [Integer] year
|
56
78
|
def increase_year_by_month(year, month)
|
57
79
|
if FiscalYear.cross_year_month?(month)
|
58
80
|
year + 1
|
@@ -61,6 +83,13 @@ module FiscalYear
|
|
61
83
|
end
|
62
84
|
end
|
63
85
|
|
86
|
+
# decrease the calendar year by month, if the month is crossed year.
|
87
|
+
#
|
88
|
+
# for example, in case of the fiscal year start from 4, you want to obtain fiscal year from the month 1, 2, 3.
|
89
|
+
#
|
90
|
+
# @param year [Integer] the calendar year
|
91
|
+
# @param month [Integer] the month
|
92
|
+
# @return [Integer] year
|
64
93
|
def decrease_year_by_month(year, month)
|
65
94
|
if FiscalYear.cross_year_month?(month)
|
66
95
|
year - 1
|
@@ -69,6 +98,8 @@ module FiscalYear
|
|
69
98
|
end
|
70
99
|
end
|
71
100
|
|
101
|
+
# @param date [Date] the date
|
102
|
+
# @return [Range<Date>] the range of the fiscal year between beginning and end by the date.
|
72
103
|
def range_by(date)
|
73
104
|
year = date.year
|
74
105
|
month = date.month
|
@@ -76,5 +107,22 @@ module FiscalYear
|
|
76
107
|
|
77
108
|
FiscalYear::Half.first_range_by(normalized_year).first..FiscalYear::Half.second_range_by(normalized_year).last
|
78
109
|
end
|
110
|
+
|
111
|
+
# start by 0.
|
112
|
+
#
|
113
|
+
# @see passed_month_count_by_month
|
114
|
+
# @param date [Date] the date
|
115
|
+
# @return [Integer] the passed month count from the beginning of the fiscal year.
|
116
|
+
def passed_month_count_by(date)
|
117
|
+
passed_month_count_by_month(date.month)
|
118
|
+
end
|
119
|
+
|
120
|
+
# start by 0.
|
121
|
+
#
|
122
|
+
# @param month [Integer] the month
|
123
|
+
# @return [Integer] the passed month count from the beginning of the fiscal year.
|
124
|
+
def passed_month_count_by_month(month)
|
125
|
+
months.find_index(month) || raise(StandardError)
|
126
|
+
end
|
79
127
|
end
|
80
128
|
end
|
data/sig/fiscal_year/config.rbs
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
module FiscalYear
|
2
2
|
class Config
|
3
3
|
attr_reader start_month: Integer
|
4
|
+
VALID_START_MONTHS: Hash[Symbol, Integer]
|
4
5
|
def initialize: -> Integer
|
6
|
+
def start_month=: (Integer|Symbol) -> bool
|
7
|
+
def valid_start_month?: (Integer|Symbol) -> bool
|
8
|
+
def fetch_start_month: (Integer|Symbol) -> Integer
|
5
9
|
end
|
6
10
|
end
|
data/sig/fiscal_year/half.rbs
CHANGED
@@ -4,9 +4,12 @@ module FiscalYear
|
|
4
4
|
def self.second: () -> Array[Integer] | -> Array[untyped]
|
5
5
|
def self.first?: (Integer) -> bool
|
6
6
|
def self.second?: (Integer) -> bool
|
7
|
+
def self.months: (Integer) -> Array[Integer]
|
7
8
|
def self.first_range_by: (Integer) -> Range[Date]
|
8
9
|
def self.second_range_by: (Integer) -> Range[Date]
|
9
10
|
def self.range_by: (Date) -> Range[Date]
|
10
11
|
def self.cross_year_in_half?: (Array[Integer]) -> bool
|
12
|
+
def self.passed_month_count_by: (Date) -> Integer
|
13
|
+
def self.passed_month_count_by_month: (Integer) -> Integer
|
11
14
|
end
|
12
15
|
end
|
data/sig/fiscal_year/quarter.rbs
CHANGED
@@ -12,5 +12,7 @@ module FiscalYear
|
|
12
12
|
def self.range_by: (Date) -> Range[Date]
|
13
13
|
def self.quarter_num: (Integer) -> Integer
|
14
14
|
def self. cross_year_in_quarter?: (Array[Integer]) -> bool
|
15
|
+
def self.passed_month_count_by: (Date) -> Integer
|
16
|
+
def self.passed_month_count_by_month: (Integer) -> Integer
|
15
17
|
end
|
16
|
-
end
|
18
|
+
end
|
data/sig/fiscal_year.rbs
CHANGED
@@ -12,5 +12,7 @@ module FiscalYear
|
|
12
12
|
def self.quarters: () -> [Array[Integer], Array[Integer], Array[Integer], Array[Integer]]
|
13
13
|
def self.increase_year_by_month: (Integer, Integer) -> Integer
|
14
14
|
def self.decrease_year_by_month: (Integer, Integer) -> Integer
|
15
|
-
def self.range_by: (Date) -> Range
|
15
|
+
def self.range_by: (Date) -> Range[Date]
|
16
|
+
def self.passed_month_count_by: (Date) -> Integer
|
17
|
+
def self.passed_month_count_by_month: (Integer) -> Integer
|
16
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fiscal_year
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tsubasa Kawajiri
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -43,6 +43,7 @@ files:
|
|
43
43
|
- lib/fiscal_year.rb
|
44
44
|
- lib/fiscal_year/config.rb
|
45
45
|
- lib/fiscal_year/half.rb
|
46
|
+
- lib/fiscal_year/invalid_start_month_error.rb
|
46
47
|
- lib/fiscal_year/quarter.rb
|
47
48
|
- lib/fiscal_year/version.rb
|
48
49
|
- lib/fiscal_year/year_to_date.rb
|
@@ -51,6 +52,7 @@ files:
|
|
51
52
|
- sig/fiscal_year.rbs
|
52
53
|
- sig/fiscal_year/config.rbs
|
53
54
|
- sig/fiscal_year/half.rbs
|
55
|
+
- sig/fiscal_year/invalid_start_month_error.rbs
|
54
56
|
- sig/fiscal_year/quarter.rbs
|
55
57
|
- sig/fiscal_year/year_to_date.rbs
|
56
58
|
homepage: https://github.com/TsubasaKawajiri/fiscal_year
|