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 +4 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +16 -0
- data/README.md +48 -21
- data/lib/fiscaly.rb +49 -30
- data/lib/fiscaly/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83bdffbd15520ef156308840e2f87e35521b6aeaea378e36985b1b061f6c74bd
|
4
|
+
data.tar.gz: 762a395022ca4cbb3aeae08155cda216f971f553598349e217b882f581f09859
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32c6117fc0ee86b5e193d84d08f00f55d6450c1216fc7e6cbc19f5e53b3046fac9c1e81447159267ee5ac0eef33bff010c09ff15b071e4b5b34fd8eded8089c0
|
7
|
+
data.tar.gz: 16b76da26db027656d57f87f453b79384213666b5a1fda9b49753ac9c7ea581a8b80fb81afc73417c8eb4edb65764a164121e5d86a8d6123b6e51905e1e08e4a
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Fiscaly
|
2
2
|
|
3
|
-
|
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
|
-
|
24
|
+
Configure fiscal year:
|
25
25
|
|
26
26
|
```ruby
|
27
|
-
|
28
|
-
Fiscaly.
|
29
|
-
|
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
|
34
|
+
Create from calendar year:
|
33
35
|
|
34
36
|
```ruby
|
35
|
-
Fiscaly.
|
36
|
-
|
37
|
-
|
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
|
-
|
50
|
+
If you want to create from fiscal year, following methods are available:
|
41
51
|
|
42
52
|
```ruby
|
43
|
-
|
44
|
-
|
53
|
+
fiscal = Fiscaly.fymd(2017, 1, 1)
|
54
|
+
fiscal.date
|
55
|
+
#=> 2018-01-01
|
45
56
|
|
46
|
-
|
47
|
-
|
57
|
+
fiscal = Fiscaly.fdate(Date.new(2017, 1, 1))
|
58
|
+
fiscal.date
|
59
|
+
#=> 2018-01-01
|
48
60
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
61
|
+
fiscal = Fiscaly.fparse("2017-01-01")
|
62
|
+
fiscal.date
|
63
|
+
#=> 2018-01-01
|
53
64
|
```
|
54
65
|
|
55
|
-
|
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
|
-
#=>
|
104
|
+
#=> 2016-06-30
|
92
105
|
fiscal.range_of_fquarter(0)
|
93
|
-
#=> 2016-04-01..
|
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:
|
data/lib/fiscaly.rb
CHANGED
@@ -3,16 +3,19 @@ require 'active_support/core_ext/time'
|
|
3
3
|
require 'fiscaly/version'
|
4
4
|
|
5
5
|
class Fiscaly
|
6
|
-
KEY = :
|
6
|
+
KEY = :fiscaly_options
|
7
7
|
|
8
|
-
cattr_accessor :
|
9
|
-
@@
|
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 :
|
15
|
+
attr_reader :options
|
13
16
|
|
14
|
-
def initialize(date,
|
15
|
-
@
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
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
|
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
|
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(
|
111
|
-
|
121
|
+
def fdate(fdate, options = {})
|
122
|
+
fymd(fdate.year, fdate.month, fdate.day, options)
|
112
123
|
end
|
113
124
|
|
114
|
-
def fparse(
|
115
|
-
|
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
|
-
|
131
|
+
self.new(normalize(fyear, month, day, options), options)
|
120
132
|
end
|
121
133
|
|
122
|
-
def
|
134
|
+
def with(options = {})
|
123
135
|
Thread.current[KEY] ||= []
|
124
|
-
Thread.current[KEY].push(
|
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
|
132
|
-
(Thread.current[KEY].is_a?(Array) && Thread.current[KEY][-1]) ||
|
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(
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
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
|
data/lib/fiscaly/version.rb
CHANGED
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.
|
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:
|
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
|
-
|
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
|