fiscal_year 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0bd01c4ef4840fe76c0c0b183751fb430c3eea13947fdb306ef60ef51f07971d
4
- data.tar.gz: a1073241fe4d94bec00779aef5ab57a57f23e6439f2d32e7a61a645a3f8d1bdf
3
+ metadata.gz: 284c5184ce2fd9503b81a6de26e657eacba6b917d8544e88ee37b771f9316b82
4
+ data.tar.gz: 072003b1e2b38f9c8e9e6ec6ce3fe549acd9fc06b9b403065e1605bf73bc0ba2
5
5
  SHA512:
6
- metadata.gz: d8efaaecee78bcbdd07200d5d26bc77f9946df6eb2d06804e0ff2765f88928ec439daba62a97487de9679565e86020d3f9225e440629944f9815a01aeec70454
7
- data.tar.gz: 0d0727ca3cef12d4e2a9a3d1d692e75a3172813eb2bde55b39635d8b52be7afc88b3cda2bd2e16e61bafc56a506efd45228a8ae15f8d9be35092fb9263da1c17
6
+ metadata.gz: bae3fd2e63eb14fc7954e40e7e98e4861902b47b1f47a6f8038351373f899767aae0a70133a0d6fae274edefa9872596fa11ce23a81001e055b9b200ddb9245d
7
+ data.tar.gz: c3903b8302eb1f86ff53bc7ef74a2f43822e9a83eb2399491da321ab7c5af9a334bf0abd8fcb39db7fc828d451815d204d6baa6d43515768bcf1b8f222e48645
data/.rubocop.yml CHANGED
@@ -11,3 +11,10 @@ Style/StringLiteralsInInterpolation:
11
11
 
12
12
  Layout/LineLength:
13
13
  Max: 120
14
+
15
+ Style/Documentation:
16
+ Enabled: false
17
+
18
+ Metrics/BlockLength:
19
+ Exclude:
20
+ - 'spec/**/*'
data/CHANGELOG.md CHANGED
@@ -1,4 +1,8 @@
1
- ## [Unreleased]
1
+ ## [0.2.0] - 2023-03-16
2
+ - add test
3
+ - fit to rubocop
4
+
5
+ - fix some methods
2
6
 
3
7
  ## [0.1.0] - 2023-03-14
4
8
 
data/CODE_OF_CONDUCT.md CHANGED
@@ -39,7 +39,7 @@ This Code of Conduct applies within all community spaces, and also applies when
39
39
 
40
40
  ## Enforcement
41
41
 
42
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at noodles@aznyn.jp. All complaints will be reviewed and investigated promptly and fairly.
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at tk.music.jpn@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
43
 
44
44
  All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
45
 
data/README.md CHANGED
@@ -1,9 +1,22 @@
1
+ ![build badge](https://github.com/TsubasaKawajiri/fiscal_year/actions/workflows/main.yml/badge.svg?branch=master)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/39fdb48501c5f53235a9/maintainability)](https://codeclimate.com/github/TsubasaKawajiri/fiscal_year/maintainability)
3
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/39fdb48501c5f53235a9/test_coverage)](https://codeclimate.com/github/TsubasaKawajiri/fiscal_year/test_coverage)
4
+
1
5
  # FiscalYear
2
6
 
7
+ Calculate fiscal year half, quarter, and year to date.
3
8
 
4
9
  ## Installation
5
10
 
6
- TODO
11
+ ```
12
+ gem 'fiscal_year'
13
+ ```
14
+
15
+ then,
16
+
17
+ ```
18
+ bundle install
19
+ ```
7
20
 
8
21
  ## Usage
9
22
 
@@ -12,6 +25,20 @@ require 'fiscal_year'
12
25
 
13
26
  FiscalYear.quarters
14
27
  => [[4, 5, 6], [7, 8, 9], [10, 11, 12], [1, 2, 3]]
28
+
29
+ date = Date.parse('2023/08/01')
30
+
31
+ # get current Half range
32
+ FiscalYear::Half.range_by(date)
33
+ => Sat, 01 Apr 2023..Sat, 30 Sep 2023
34
+
35
+ # get current quarter range
36
+ FiscalYear::Quarter.range_by(date)
37
+ => Sat, 01 Jul 2023..Sat, 30 Sep 2023
38
+
39
+ # get year to date
40
+ FiscalYear::YearToDate.range_by(date)
41
+ => Sat, 01 Apr 2023..Thu, 31 Aug 2023
15
42
  ```
16
43
 
17
44
  configure start month
@@ -31,7 +58,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
31
58
 
32
59
  ## Contributing
33
60
 
34
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fiscal_year. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/fiscal_year/blob/master/CODE_OF_CONDUCT.md).
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/TsubasaKawajiri/fiscal_year. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/TsubasaKawajiri/fiscal_year/blob/master/CODE_OF_CONDUCT.md).
35
62
 
36
63
  ## License
37
64
 
@@ -39,4 +66,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
39
66
 
40
67
  ## Code of Conduct
41
68
 
42
- Everyone interacting in the FiscalYear project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/fiscal_year/blob/master/CODE_OF_CONDUCT.md).
69
+ Everyone interacting in the FiscalYear project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/TsubasaKawajiri/fiscal_year/blob/master/CODE_OF_CONDUCT.md).
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module FiscalYear
2
4
  class Config
3
5
  attr_accessor :start_month
4
6
 
5
7
  def initialize
6
- @start_month = 8
8
+ @start_month = 4
7
9
  end
8
10
  end
9
11
  end
@@ -3,7 +3,6 @@
3
3
  module FiscalYear
4
4
  class Half
5
5
  class << self
6
-
7
6
  def first
8
7
  FiscalYear.halfs.first
9
8
  end
@@ -21,22 +20,44 @@ module FiscalYear
21
20
  end
22
21
 
23
22
  def first_range_by(year)
24
- Date.parse("#{year}/#{first.first}")..Date.parse("#{year}/#{first.last}").end_of_month
23
+ # care Date#parse 2 digit year auto complete.
24
+ # 99 + 1 = 100, but expect 2000 this context.
25
+ year = 1999 if year == 99
26
+ end_year = normalize_year_by_month(year, first.last)
27
+
28
+ Date.parse("#{year}/#{first.first}/01")..Date.parse("#{end_year}/#{first.last}/01").end_of_month
25
29
  end
26
30
 
27
31
  def second_range_by(year)
28
- Date.parse("#{year}/#{second.first}")..Date.parse("#{year + 1}/#{second.last}").end_of_month
32
+ # care Date#parse 2 digit year auto complete.
33
+ # 99 + 1 = 100, but expect 2000 this context.
34
+ year = 1999 if year == 99
35
+ start_year = normalize_year_by_month(year, second.first)
36
+ end_year = normalize_year_by_month(year, second.last)
37
+
38
+ Date.parse("#{start_year}/#{second.first}/01")..Date.parse("#{end_year}/#{second.last}/01").end_of_month
29
39
  end
30
40
 
31
41
  def range_by(date)
32
42
  month = date.month
33
43
  year = date.year
44
+ # if passed crossed year value, normalize to not crossed year value
45
+ year -= 1 if FiscalYear.cross_year_month?(month)
46
+
47
+ first?(month) ? first_range_by(year) : second_range_by(year)
48
+ end
49
+
50
+ def normalize_year_by_month(year, month)
34
51
  if FiscalYear.cross_year_month?(month)
35
- Date.parse("#{year - 1}/#{second.first}")..Date.parse("#{year}/#{second.last}").end_of_month
52
+ year + 1
36
53
  else
37
- first?(month) ? first_range_by(date.year) : second_range_by(date.year)
54
+ year
38
55
  end
39
56
  end
57
+
58
+ def cross_year_in_half?(half)
59
+ FiscalYear.cross_year? && half.any? { |month| month == 12 }
60
+ end
40
61
  end
41
62
  end
42
63
  end
@@ -31,14 +31,19 @@ module FiscalYear
31
31
 
32
32
  def range_by(date)
33
33
  year = date.year
34
- this_quater_months = months(date.month)
34
+ this_quater = months(date.month)
35
+ last_year = cross_year_in_quarter?(this_quater) ? year + 1 : year
35
36
 
36
- Date.parse("#{year}/#{this_quater_months.first}")..Date.parse("#{year}/#{this_quater_months.last}").end_of_month
37
+ Date.parse("#{year}/#{this_quater.first}/01")..Date.parse("#{last_year}/#{this_quater.last}/01").end_of_month
37
38
  end
38
39
 
39
40
  def quater_num(month)
40
41
  (FiscalYear.quarters.rindex(months(month)) + 1)
41
42
  end
43
+
44
+ def cross_year_in_quarter?(quarter)
45
+ FiscalYear.cross_year? && quarter.any? { |month| month == 12 }
46
+ end
42
47
  end
43
48
  end
44
49
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FiscalYear
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -8,9 +8,9 @@ module FiscalYear
8
8
  month = date.month
9
9
 
10
10
  if FiscalYear.cross_year_month?(month)
11
- Date.parse("#{year - 1}/#{FiscalYear.config.start_month}")..date.end_of_month.to_date
11
+ Date.parse("#{year - 1}/#{FiscalYear.config.start_month}/01")..date.end_of_month.to_date
12
12
  else
13
- Date.parse("#{year}/#{FiscalYear.config.start_month}")..date.end_of_month.to_date
13
+ Date.parse("#{year}/#{FiscalYear.config.start_month}/01")..date.end_of_month.to_date
14
14
  end
15
15
  end
16
16
 
@@ -29,24 +29,36 @@ module FiscalYear
29
29
  pair
30
30
  end
31
31
 
32
- def half_range_by(date)
32
+ # TODO: fit to Abc size
33
+ def half_range_by(date) # rubocop:disable Metrics/AbcSize
33
34
  year = date.year
34
35
  month = date.month
35
36
 
36
- return Date.parse("#{year}/#{Half.first.first}")..date.end_of_month.to_date if Half.first?(month)
37
+ if Half.first?(month)
38
+ return (Date.parse("#{FiscalYear.cross_year_month?(month) ? year - 1 : year}/#{Half.first.first}/01")..
39
+ date.end_of_month.to_date
40
+ )
41
+ end
37
42
 
38
- Date.parse("#{FiscalYear.cross_year_month?(month) ? year - 1 : year}/#{Half.second.first}")..date.end_of_month.to_date
43
+ Date.parse(
44
+ "#{FiscalYear::Half.cross_year_in_half?(Half.second) ? year - 1 : year}/#{Half.second.first}"
45
+ )..date.end_of_month.to_date
39
46
  end
40
47
 
41
48
  def quarter_range_by(date)
42
49
  year = date.year
43
50
  month = date.month
44
51
 
45
- %i[first? second? third? fourth?].each do |method_name|
46
- if FiscalYear::Quarter.public_send(method_name, month)
47
- Date.parse("#{year}/#{Quarter.public_send(method_name).first}")..date.end_of_month.to_date
48
- end
49
- end
52
+ quarter_method = %i[first? second? third? fourth?]
53
+ .filter { |method_name| FiscalYear::Quarter.public_send(method_name, month) }
54
+ .first.to_s.gsub("?", "").to_sym
55
+
56
+ quarter = Quarter.public_send(quarter_method)
57
+
58
+ (
59
+ Date.parse("#{FiscalYear.cross_year_month?(month) ? year - 1 : year}/#{quarter.first}/01")..
60
+ date.end_of_month.to_date
61
+ )
50
62
  end
51
63
  end
52
64
  end
data/lib/fiscal_year.rb CHANGED
@@ -2,38 +2,39 @@
2
2
 
3
3
  require_relative "fiscal_year/version"
4
4
 
5
- require 'fiscal_year/half'
6
- require 'fiscal_year/quarter'
7
- require 'fiscal_year/year_to_date'
8
- require 'fiscal_year/config'
5
+ require "fiscal_year/half"
6
+ require "fiscal_year/quarter"
7
+ require "fiscal_year/year_to_date"
8
+ require "fiscal_year/config"
9
9
 
10
- require 'active_support'
11
- require 'active_support/core_ext'
10
+ require "active_support"
11
+ require "active_support/core_ext"
12
12
 
13
13
  module FiscalYear
14
- @@config ||= FiscalYear::Config.new
14
+ @config ||= FiscalYear::Config.new
15
+
15
16
  class << self
16
- def config
17
- @@config
18
- end
19
-
20
- def configure(&block)
21
- yield(@@config) if block_given?
17
+ attr_reader :config
18
+
19
+ def configure
20
+ yield(@config) if block_given?
22
21
  end
23
22
 
24
23
  def cross_year_month?(month)
25
24
  cross_year_months.include?(month)
26
25
  end
27
-
26
+
28
27
  def cross_year?
29
28
  months.rindex(1) != 0
30
29
  end
31
30
 
32
31
  def months
33
- (1..12).to_a.tap { |arr| arr.concat(arr.shift(@@config.start_month - 1)) }
32
+ (1..12).to_a.tap { |arr| arr.concat(arr.shift(@config.start_month - 1)) }
34
33
  end
35
34
 
36
35
  def cross_year_months
36
+ return [] if @config.start_month == 1
37
+
37
38
  months.slice(months.rindex(1), months.length)
38
39
  end
39
40
 
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.1.0
4
+ version: 0.2.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: 2023-03-14 00:00:00.000000000 Z
11
+ date: 2023-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport