fiscal_year 0.7.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/lib/fiscal_year/config.rb +42 -1
- data/lib/fiscal_year/half.rb +29 -13
- data/lib/fiscal_year/invalid_start_month_error.rb +9 -0
- data/lib/fiscal_year/quarter.rb +18 -5
- data/lib/fiscal_year/version.rb +1 -1
- data/lib/fiscal_year/year_to_date.rb +1 -1
- data/lib/fiscal_year.rb +18 -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,13 @@
|
|
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
|
+
|
1
11
|
## [0.7.0] - 2024-07-11
|
2
12
|
- add YARD comment.
|
3
13
|
- fix `FiscalYear::YearToDate.quarter_range_by` logic.
|
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
@@ -5,18 +5,12 @@ module FiscalYear
|
|
5
5
|
class << self
|
6
6
|
# @return [Array<Integer>] the first half of the fiscal year.
|
7
7
|
def first
|
8
|
-
|
9
|
-
return first if first.is_a? Array
|
10
|
-
|
11
|
-
[]
|
8
|
+
FiscalYear.halfs.first || raise(StandardError)
|
12
9
|
end
|
13
10
|
|
14
11
|
# @return [Array<Integer>] the second half of the fiscal year.
|
15
12
|
def second
|
16
|
-
|
17
|
-
return second if second.is_a? Array
|
18
|
-
|
19
|
-
[]
|
13
|
+
FiscalYear.halfs.second || raise(StandardError)
|
20
14
|
end
|
21
15
|
|
22
16
|
# @param month [Integer] the month
|
@@ -31,14 +25,19 @@ module FiscalYear
|
|
31
25
|
!first?(month)
|
32
26
|
end
|
33
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
34
|
# @param year [Integer] the calendar year
|
35
35
|
# @return [Range<Date>] the range of the first half of the fiscal year.
|
36
36
|
def first_range_by(year)
|
37
37
|
# care Date#parse 2 digit year auto complete.
|
38
38
|
# 99 + 1 = 100, but expect 2000 this context.
|
39
39
|
year = 1999 if year == 99
|
40
|
-
end_month = first.last
|
41
|
-
raise StandardError if end_month.nil?
|
40
|
+
end_month = first.last || raise(StandardError)
|
42
41
|
|
43
42
|
end_year = FiscalYear.increase_year_by_month(year, end_month)
|
44
43
|
|
@@ -51,9 +50,8 @@ module FiscalYear
|
|
51
50
|
# care Date#parse 2 digit year auto complete.
|
52
51
|
# 99 + 1 = 100, but expect 2000 this context.
|
53
52
|
year = 1999 if year == 99
|
54
|
-
first_month = second.first
|
55
|
-
end_month = second.last
|
56
|
-
raise StandardError if first_month.nil? || end_month.nil?
|
53
|
+
first_month = second.first || raise(StandardError)
|
54
|
+
end_month = second.last || raise(StandardError)
|
57
55
|
|
58
56
|
start_year = FiscalYear.increase_year_by_month(year, first_month)
|
59
57
|
end_year = FiscalYear.increase_year_by_month(year, end_month)
|
@@ -77,6 +75,24 @@ module FiscalYear
|
|
77
75
|
def cross_year_in_half?(half)
|
78
76
|
FiscalYear.cross_year? && half.any? { |month| month == 12 }
|
79
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
|
80
96
|
end
|
81
97
|
end
|
82
98
|
end
|
data/lib/fiscal_year/quarter.rb
CHANGED
@@ -45,12 +45,9 @@ module FiscalYear
|
|
45
45
|
end
|
46
46
|
|
47
47
|
# @param month [Integer] the month
|
48
|
-
# @return [Array<Integer>] the quarter
|
48
|
+
# @return [Array<Integer>] the quarter months by the month.
|
49
49
|
def months(month)
|
50
|
-
|
51
|
-
raise ::StandardError if months.nil?
|
52
|
-
|
53
|
-
months
|
50
|
+
FiscalYear.quarters.find { |a| a.include?(month) } || raise(StandardError)
|
54
51
|
end
|
55
52
|
|
56
53
|
# @param date [Date] the date
|
@@ -76,6 +73,22 @@ module FiscalYear
|
|
76
73
|
def cross_year_in_quarter?(quarter)
|
77
74
|
FiscalYear.cross_year? && quarter.any? { |month| month == 12 }
|
78
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
|
79
92
|
end
|
80
93
|
end
|
81
94
|
end
|
data/lib/fiscal_year/version.rb
CHANGED
@@ -75,7 +75,7 @@ module FiscalYear
|
|
75
75
|
# => Thu, 01 Jul 2021..Tue, 31 Aug 2021
|
76
76
|
#
|
77
77
|
# FiscalYear::YearToDate.quarter_range_by(Date.parse("2021-01-01"))
|
78
|
-
# =>
|
78
|
+
# => Fri, 01 Jan 2021..Sun, 31 Jan 2021
|
79
79
|
def quarter_range_by(date)
|
80
80
|
year = date.year
|
81
81
|
month = date.month
|
data/lib/fiscal_year.rb
CHANGED
@@ -53,10 +53,7 @@ module FiscalYear
|
|
53
53
|
|
54
54
|
rindex = months.rindex(1).to_i
|
55
55
|
|
56
|
-
|
57
|
-
raise StandardError if m.nil?
|
58
|
-
|
59
|
-
m
|
56
|
+
months.slice(rindex, months.length) || raise(StandardError)
|
60
57
|
end
|
61
58
|
|
62
59
|
# @return [Array(Array<Integer>, Array<Integer>)] the first half and the second half of the fiscal year.
|
@@ -110,5 +107,22 @@ module FiscalYear
|
|
110
107
|
|
111
108
|
FiscalYear::Half.first_range_by(normalized_year).first..FiscalYear::Half.second_range_by(normalized_year).last
|
112
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
|
113
127
|
end
|
114
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-07-
|
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
|