fiscaly 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0e35eef30151ba7e2bdf12e56c758c48a827de4bf255a23a4121cd31170ee7d5
4
+ data.tar.gz: 05a92cbf15bc3e36f166eb7f6939dc9dfa360de24ab4c178d16294e7fc81e677
5
+ SHA512:
6
+ metadata.gz: 6f09bfe91be211441b0286e843b9a01cab10cf569af8956a64f15ea2dccd9d949ebf0cc8f13011c917a9af4a7b9440701d6c00c5d5aca661d1f96e9c9d36b347
7
+ data.tar.gz: ab18257f45b1060ba5886bc50760bcf860c0405dd3dfad193edd7587ca16fb2c0cad7d1a5ba60fc45081e609f6d3115b342f679585af15f1f5d880600e543bdd
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .bundle/
2
+ .project
3
+ Gemfile.lock
4
+ coverage/
5
+ pkg/
6
+ tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3
4
+ - 2.4
5
+ - 2.5
6
+ gemfile:
7
+ - gemfiles/rails50.gemfile
8
+ - gemfiles/rails51.gemfile
9
+ - gemfiles/rails52.gemfile
10
+ script:
11
+ - bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Yoshikazu Kaneta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # Fiscaly
2
+
3
+ Financial date class for ruby.
4
+
5
+ ## Dependencies
6
+
7
+ * ruby 2.3+
8
+ * activesupport 5.0+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'fiscaly'
16
+ ```
17
+
18
+ Then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ Create from date:
25
+
26
+ ```ruby
27
+ Fiscaly.ymd(2017, 1, 1)
28
+ Fiscaly.date(Date.new(2017, 1, 1))
29
+ Fiscaly.parse("2017-01-01")
30
+ ```
31
+
32
+ Create from date having financial year:
33
+
34
+ ```ruby
35
+ Fiscaly.fymd(2017, 1, 1)
36
+ Fiscaly.fdate(Date.new(2017, 1, 1))
37
+ Fiscaly.fparse("2017-01-01")
38
+ ```
39
+
40
+ Change the start month of financial year:
41
+
42
+ ```ruby
43
+ # set globally (non thread-safe)
44
+ Fiscaly.start_month = 4
45
+
46
+ # set by argument
47
+ Fiscaly.date(date, start_month: 4)
48
+
49
+ # set by block
50
+ Fiscaly.with_start_month(4) do
51
+ ...
52
+ end
53
+ ```
54
+
55
+ Get range of financial calendar:
56
+
57
+ ```ruby
58
+ fiscal = Fiscaly.ymd(2017, 1, 1)
59
+
60
+ fiscal.beginning_of_fyear
61
+ #=> 2016-04-01
62
+ fiscal.end_of_fyear
63
+ #=> 2017-03-31
64
+ fiscal.range_of_fyear
65
+ #=> 2016-04-01..2017-03-31
66
+
67
+ fiscal.beginning_of_fhalf
68
+ #=> 2016-10-01
69
+ fiscal.end_of_fhalf
70
+ #=> 2017-03-31
71
+ fiscal.range_of_fhalf
72
+ #=> 2016-10-01..2017-03-31
73
+
74
+ fiscal.beginning_of_fhalf(0)
75
+ #=> 2016-04-01
76
+ fiscal.end_of_fhalf(0)
77
+ #=> 2016-09-30
78
+ fiscal.range_of_fhalf(0)
79
+ #=> 2016-04-01..2016-09-30
80
+
81
+ fiscal.beginning_of_fquarter
82
+ #=> 2017-01-01
83
+ fiscal.end_of_fquarter
84
+ #=> 2017-03-31
85
+ fiscal.range_of_fquarter
86
+ #=> 2017-01-01..2017-03-31
87
+
88
+ fiscal.beginning_of_fquarter(0)
89
+ #=> 2016-04-01
90
+ fiscal.end_of_fquarter(0)
91
+ #=> 2017-03-31
92
+ fiscal.range_of_fquarter(0)
93
+ #=> 2016-04-01..2017-03-31
94
+ ```
95
+
96
+ Extend ruby's standard `Date` class:
97
+
98
+ ```ruby
99
+ require 'fiscaly/extension'
100
+
101
+ date = Date.today.fiscaly
102
+ date.fyear
103
+ ```
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome at https://github.com/kanety/fiscaly.
108
+
109
+ ## License
110
+
111
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fiscaly"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/fiscaly.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fiscaly/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fiscaly"
8
+ spec.version = Fiscaly::VERSION
9
+ spec.authors = ["Yoshikazu Kaneta"]
10
+ spec.email = ["kaneta@sitebridge.co.jp"]
11
+ spec.summary = %q{Financial date class for ruby}
12
+ spec.description = %q{Financial date class for ruby}
13
+ spec.homepage = "https://github.com/kanety/fiscaly"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activesupport", ">= 5.0"
21
+
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "simplecov"
25
+ end
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "activesupport", "~> 5.0.0"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "activesupport", "~> 5.1.0"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "activesupport", "~> 5.2.0"
4
+
5
+ gemspec path: "../"
data/lib/fiscaly.rb ADDED
@@ -0,0 +1,146 @@
1
+ require 'active_support/core_ext/integer'
2
+ require 'active_support/core_ext/time'
3
+ require 'fiscaly/version'
4
+
5
+ class Fiscaly
6
+ KEY = :fiscaly_start_month
7
+
8
+ cattr_accessor :start_month
9
+ @@start_month = 4
10
+
11
+ attr_reader :date
12
+ attr_reader :start_month
13
+
14
+ def initialize(date, start_month: nil)
15
+ @start_month = start_month || self.class.global_start_month
16
+ @date = date
17
+ end
18
+
19
+ def method_missing(name, *args)
20
+ if @date.respond_to?(name)
21
+ @date.send(name, *args)
22
+ else
23
+ super
24
+ end
25
+ end
26
+
27
+ def fyear
28
+ if @date.month >= @start_month
29
+ @date.year
30
+ else
31
+ @date.year - 1
32
+ end
33
+ end
34
+
35
+ def fdate
36
+ Date.new(fyear, @date.month, @date.day);
37
+ end
38
+
39
+ def beginning_of_fyear
40
+ Date.new(fyear, @start_month)
41
+ end
42
+
43
+ def end_of_fyear
44
+ (beginning_of_fyear + 11.months).end_of_month
45
+ end
46
+
47
+ def range_of_fyear
48
+ beginning_of_fyear..end_of_fyear
49
+ end
50
+
51
+ def beginning_of_fhalf(index = nil)
52
+ if index
53
+ beginning_of_fyear + (index * 6).months
54
+ else
55
+ date = beginning_of_fyear
56
+ date += 6.months until @date <= date
57
+ date -= 6.months
58
+ end
59
+ end
60
+
61
+ def end_of_fhalf(index = nil)
62
+ (beginning_of_fhalf(index) + 5.months).end_of_month
63
+ end
64
+
65
+ def range_of_fhalf(index = nil)
66
+ beginning_of_fhalf(index)..end_of_fhalf(index)
67
+ end
68
+
69
+ def beginning_of_fquarter(index = nil)
70
+ if index
71
+ beginning_of_fyear + (index * 3).months
72
+ else
73
+ date = beginning_of_fyear
74
+ date += 3.months until @date <= date
75
+ date -= 3.months
76
+ end
77
+ end
78
+
79
+ def end_of_fquarter(index = nil)
80
+ (beginning_of_fquarter(index) + 2.months).end_of_month
81
+ end
82
+
83
+ def range_of_fquarter(index = nil)
84
+ beginning_of_fquarter(index)..end_of_fquarter(index)
85
+ end
86
+
87
+ def range_of_month
88
+ @date.beginning_of_month..@date.end_of_month
89
+ end
90
+
91
+ private
92
+
93
+ class << self
94
+ def today(options = {})
95
+ date(Date.today, options)
96
+ end
97
+
98
+ def date(date, options = {})
99
+ self.new(date, options)
100
+ end
101
+
102
+ def parse(str, options = {})
103
+ date(Date.parse(str), options)
104
+ end
105
+
106
+ def ymd(year, month = 1, day = 1, options = {})
107
+ date(Date.new(year, month, day), options)
108
+ end
109
+
110
+ def fdate(date, options = {})
111
+ self.new(normalize(date, options[:start_month]), options)
112
+ end
113
+
114
+ def fparse(str, options = {})
115
+ fdate(Date.parse(str), options)
116
+ end
117
+
118
+ def fymd(fyear, month = 1, day = 1, options = {})
119
+ fdate(Date.new(fyear, month, day), options)
120
+ end
121
+
122
+ def with_start_month(start_month)
123
+ Thread.current[KEY] ||= []
124
+ Thread.current[KEY].push(start_month)
125
+ yield
126
+ ensure
127
+ Thread.current[KEY].pop
128
+ Thread.current[KEY] = nil if Thread.current[KEY].empty?
129
+ end
130
+
131
+ def global_start_month
132
+ (Thread.current[KEY].is_a?(Array) && Thread.current[KEY][-1]) || self.start_month
133
+ end
134
+
135
+ private
136
+
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
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,9 @@
1
+ class Fiscaly
2
+ module Extension
3
+ def fiscaly(options = {})
4
+ Fiscaly.new(self, options)
5
+ end
6
+ end
7
+ end
8
+
9
+ Date.send(:include, Fiscaly::Extension)
@@ -0,0 +1,3 @@
1
+ class Fiscaly
2
+ VERSION = '1.0.0'
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fiscaly
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshikazu Kaneta
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Financial date class for ruby
70
+ email:
71
+ - kaneta@sitebridge.co.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - fiscaly.gemspec
86
+ - gemfiles/rails50.gemfile
87
+ - gemfiles/rails51.gemfile
88
+ - gemfiles/rails52.gemfile
89
+ - lib/fiscaly.rb
90
+ - lib/fiscaly/extension.rb
91
+ - lib/fiscaly/version.rb
92
+ homepage: https://github.com/kanety/fiscaly
93
+ licenses: []
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.7.6
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Financial date class for ruby
115
+ test_files: []