fiscaly 1.0.0 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0e35eef30151ba7e2bdf12e56c758c48a827de4bf255a23a4121cd31170ee7d5
4
- data.tar.gz: 05a92cbf15bc3e36f166eb7f6939dc9dfa360de24ab4c178d16294e7fc81e677
3
+ metadata.gz: 83bdffbd15520ef156308840e2f87e35521b6aeaea378e36985b1b061f6c74bd
4
+ data.tar.gz: 762a395022ca4cbb3aeae08155cda216f971f553598349e217b882f581f09859
5
5
  SHA512:
6
- metadata.gz: 6f09bfe91be211441b0286e843b9a01cab10cf569af8956a64f15ea2dccd9d949ebf0cc8f13011c917a9af4a7b9440701d6c00c5d5aca661d1f96e9c9d36b347
7
- data.tar.gz: ab18257f45b1060ba5886bc50760bcf860c0405dd3dfad193edd7587ca16fb2c0cad7d1a5ba60fc45081e609f6d3115b342f679585af15f1f5d880600e543bdd
6
+ metadata.gz: 32c6117fc0ee86b5e193d84d08f00f55d6450c1216fc7e6cbc19f5e53b3046fac9c1e81447159267ee5ac0eef33bff010c09ff15b071e4b5b34fd8eded8089c0
7
+ data.tar.gz: 16b76da26db027656d57f87f453b79384213666b5a1fda9b49753ac9c7ea581a8b80fb81afc73417c8eb4edb65764a164121e5d86a8d6123b6e51905e1e08e4a
@@ -3,6 +3,7 @@ rvm:
3
3
  - 2.3
4
4
  - 2.4
5
5
  - 2.5
6
+ - 2.6
6
7
  gemfile:
7
8
  - gemfiles/rails50.gemfile
8
9
  - gemfiles/rails51.gemfile
@@ -0,0 +1,16 @@
1
+ # CHANGELOG
2
+
3
+ ## 1.1.0
4
+
5
+ Features:
6
+
7
+ * Add forward_fyear option.
8
+
9
+ Fixes:
10
+
11
+ * Fix boundary of fhalf and fquarter.
12
+ * Fix for leap day.
13
+
14
+ ## 1.0.0
15
+
16
+ * First release.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Fiscaly
2
2
 
3
- Financial date class for ruby.
3
+ Fiscal date class for ruby.
4
4
 
5
5
  ## Dependencies
6
6
 
@@ -21,38 +21,51 @@ Then execute:
21
21
 
22
22
  ## Usage
23
23
 
24
- Create from date:
24
+ Configure fiscal year:
25
25
 
26
26
  ```ruby
27
- Fiscaly.ymd(2017, 1, 1)
28
- Fiscaly.date(Date.new(2017, 1, 1))
29
- Fiscaly.parse("2017-01-01")
27
+ # set the start month of the fiscal year
28
+ Fiscaly.start_month = 4
29
+
30
+ # set true if the fiscal year is based on the following year
31
+ Fiscaly.forward_fyear = false
30
32
  ```
31
33
 
32
- Create from date having financial year:
34
+ Create from calendar year:
33
35
 
34
36
  ```ruby
35
- Fiscaly.fymd(2017, 1, 1)
36
- Fiscaly.fdate(Date.new(2017, 1, 1))
37
- Fiscaly.fparse("2017-01-01")
37
+ fiscal = Fiscaly.ymd(2017, 1, 1)
38
+ fiscal.date
39
+ #=> 2017-01-01
40
+
41
+ fiscal = Fiscaly.date(Date.new(2017, 1, 1))
42
+ fiscal.date
43
+ #=> 2017-01-01
44
+
45
+ fiscal = Fiscaly.parse("2017-01-01")
46
+ fiscal.date
47
+ #=> 2017-01-01
38
48
  ```
39
49
 
40
- Change the start month of financial year:
50
+ If you want to create from fiscal year, following methods are available:
41
51
 
42
52
  ```ruby
43
- # set globally (non thread-safe)
44
- Fiscaly.start_month = 4
53
+ fiscal = Fiscaly.fymd(2017, 1, 1)
54
+ fiscal.date
55
+ #=> 2018-01-01
45
56
 
46
- # set by argument
47
- Fiscaly.date(date, start_month: 4)
57
+ fiscal = Fiscaly.fdate(Date.new(2017, 1, 1))
58
+ fiscal.date
59
+ #=> 2018-01-01
48
60
 
49
- # set by block
50
- Fiscaly.with_start_month(4) do
51
- ...
52
- end
61
+ fiscal = Fiscaly.fparse("2017-01-01")
62
+ fiscal.date
63
+ #=> 2018-01-01
53
64
  ```
54
65
 
55
- Get range of financial calendar:
66
+ Note that these methods converts the fiscal year to the calendar year but the month and the day are kept as it is.
67
+
68
+ Get the range of financial calendar:
56
69
 
57
70
  ```ruby
58
71
  fiscal = Fiscaly.ymd(2017, 1, 1)
@@ -88,9 +101,23 @@ fiscal.range_of_fquarter
88
101
  fiscal.beginning_of_fquarter(0)
89
102
  #=> 2016-04-01
90
103
  fiscal.end_of_fquarter(0)
91
- #=> 2017-03-31
104
+ #=> 2016-06-30
92
105
  fiscal.range_of_fquarter(0)
93
- #=> 2016-04-01..2017-03-31
106
+ #=> 2016-04-01..2016-06-30
107
+ ```
108
+
109
+ Change the configurations in some context:
110
+
111
+ ```ruby
112
+ # set by argument
113
+ fiscal = Fiscaly.ymd(2017, 10, 1, start_month: 10, forward_fyear: true)
114
+ fiscal.fyear #=> 2018
115
+
116
+ # set by block
117
+ Fiscaly.with(start_month: 10, forward_fyear: true) do
118
+ fiscal = Fiscaly.ymd(2017, 10, 1)
119
+ fiscal.fyear #=> 2018
120
+ end
94
121
  ```
95
122
 
96
123
  Extend ruby's standard `Date` class:
@@ -3,16 +3,19 @@ require 'active_support/core_ext/time'
3
3
  require 'fiscaly/version'
4
4
 
5
5
  class Fiscaly
6
- KEY = :fiscaly_start_month
6
+ KEY = :fiscaly_options
7
7
 
8
- cattr_accessor :start_month
9
- @@start_month = 4
8
+ cattr_accessor :options
9
+ @@options = {
10
+ start_month: 4,
11
+ forward_fyear: false
12
+ }
10
13
 
11
14
  attr_reader :date
12
- attr_reader :start_month
15
+ attr_reader :options
13
16
 
14
- def initialize(date, start_month: nil)
15
- @start_month = start_month || self.class.global_start_month
17
+ def initialize(date, options = {})
18
+ @options = self.class.global_options.merge(options)
16
19
  @date = date
17
20
  end
18
21
 
@@ -24,12 +27,19 @@ class Fiscaly
24
27
  end
25
28
  end
26
29
 
30
+ def start_month
31
+ @options[:start_month]
32
+ end
33
+
34
+ def forward_fyear?
35
+ @options[:forward_fyear]
36
+ end
37
+
27
38
  def fyear
28
- if @date.month >= @start_month
29
- @date.year
30
- else
31
- @date.year - 1
32
- end
39
+ fy = @date.year
40
+ fy -= 1 if @date.month < start_month
41
+ fy += 1 if forward_fyear?
42
+ fy
33
43
  end
34
44
 
35
45
  def fdate
@@ -37,7 +47,8 @@ class Fiscaly
37
47
  end
38
48
 
39
49
  def beginning_of_fyear
40
- Date.new(fyear, @start_month)
50
+ year = forward_fyear? ? fyear - 1 : fyear
51
+ Date.new(year, start_month)
41
52
  end
42
53
 
43
54
  def end_of_fyear
@@ -53,7 +64,7 @@ class Fiscaly
53
64
  beginning_of_fyear + (index * 6).months
54
65
  else
55
66
  date = beginning_of_fyear
56
- date += 6.months until @date <= date
67
+ date += 6.months until @date < date
57
68
  date -= 6.months
58
69
  end
59
70
  end
@@ -71,7 +82,7 @@ class Fiscaly
71
82
  beginning_of_fyear + (index * 3).months
72
83
  else
73
84
  date = beginning_of_fyear
74
- date += 3.months until @date <= date
85
+ date += 3.months until @date < date
75
86
  date -= 3.months
76
87
  end
77
88
  end
@@ -107,40 +118,48 @@ class Fiscaly
107
118
  date(Date.new(year, month, day), options)
108
119
  end
109
120
 
110
- def fdate(date, options = {})
111
- self.new(normalize(date, options[:start_month]), options)
121
+ def fdate(fdate, options = {})
122
+ fymd(fdate.year, fdate.month, fdate.day, options)
112
123
  end
113
124
 
114
- def fparse(str, options = {})
115
- fdate(Date.parse(str), options)
125
+ def fparse(fstr, options = {})
126
+ parsed = Date._parse(fstr)
127
+ fymd(parsed[:year], parsed[:mon], parsed[:mday], options)
116
128
  end
117
129
 
118
130
  def fymd(fyear, month = 1, day = 1, options = {})
119
- fdate(Date.new(fyear, month, day), options)
131
+ self.new(normalize(fyear, month, day, options), options)
120
132
  end
121
133
 
122
- def with_start_month(start_month)
134
+ def with(options = {})
123
135
  Thread.current[KEY] ||= []
124
- Thread.current[KEY].push(start_month)
136
+ Thread.current[KEY].push(options)
125
137
  yield
126
138
  ensure
127
139
  Thread.current[KEY].pop
128
140
  Thread.current[KEY] = nil if Thread.current[KEY].empty?
129
141
  end
130
142
 
131
- def global_start_month
132
- (Thread.current[KEY].is_a?(Array) && Thread.current[KEY][-1]) || self.start_month
143
+ def global_options
144
+ @@options.merge((Thread.current[KEY].is_a?(Array) && Thread.current[KEY][-1]) || {})
145
+ end
146
+
147
+ def start_month=(val)
148
+ @@options[:start_month] = val
149
+ end
150
+
151
+ def forward_fyear=(val)
152
+ @@options[:forward_fyear] = val
133
153
  end
134
154
 
135
155
  private
136
156
 
137
- def normalize(date, start_month)
138
- start_month ||= global_start_month
139
- if date.month < start_month
140
- Date.new(date.year + 1, date.month, date.day)
141
- else
142
- date
143
- end
157
+ def normalize(fyear, month, day, options = {})
158
+ options = global_options.merge(options)
159
+ year = fyear
160
+ year += 1 if month < options[:start_month]
161
+ year -= 1 if options[:forward_fyear]
162
+ Date.new(year, month, day)
144
163
  end
145
164
  end
146
165
  end
@@ -1,3 +1,3 @@
1
1
  class Fiscaly
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fiscaly
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshikazu Kaneta
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-10 00:00:00.000000000 Z
11
+ date: 2019-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -76,6 +76,7 @@ files:
76
76
  - ".gitignore"
77
77
  - ".rspec"
78
78
  - ".travis.yml"
79
+ - CHANGELOG.md
79
80
  - Gemfile
80
81
  - LICENSE
81
82
  - README.md
@@ -107,8 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
108
  - !ruby/object:Gem::Version
108
109
  version: '0'
109
110
  requirements: []
110
- rubyforge_project:
111
- rubygems_version: 2.7.6
111
+ rubygems_version: 3.0.1
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: Financial date class for ruby