with_tax 0.1.20200414
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 +7 -0
- data/.github/workflows/ruby.yml +64 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +9 -0
- data/README.md +238 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/with_tax.rb +10 -0
- data/lib/with_tax/method_definer.rb +19 -0
- data/lib/with_tax/rate.rb +16 -0
- data/lib/with_tax/rate.yml +9 -0
- data/lib/with_tax/version.rb +3 -0
- data/with_tax.gemspec +25 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '068e82e41ea94b91fae8ff7ae8ca6233a827af0b908f0801ec25121f5beada08'
|
4
|
+
data.tar.gz: 035565bfc702d68afdedf025ded5d0add4f09d661434e2c73b31ffcef5a9b86c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 420edb711b830e56ffa0e45c120601d016fc1721b7a88d775252af5f2f8dc3efaff8f0a5fa27cff56c40d8edfe8a3a981a8afcc06ce64b57aea1b1a18dea0f39
|
7
|
+
data.tar.gz: df68a47b67ee68fbf2dff2d527245cb9611b592e32d530e64f5dbbebec47847470db0484b2cba6cf7089f1be55418aeffe872f4987177e71ab5bd08fcd9485d0
|
@@ -0,0 +1,64 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
build2_4:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
steps:
|
13
|
+
- uses: actions/checkout@v2
|
14
|
+
- name: Set up Ruby 2.4
|
15
|
+
uses: actions/setup-ruby@v1
|
16
|
+
with:
|
17
|
+
ruby-version: 2.4.x
|
18
|
+
- name: Build and test with Rake
|
19
|
+
run: |
|
20
|
+
gem install bundler
|
21
|
+
bundle install --jobs 4 --retry 3
|
22
|
+
bundle exec rake
|
23
|
+
|
24
|
+
build2_5:
|
25
|
+
runs-on: ubuntu-latest
|
26
|
+
steps:
|
27
|
+
- uses: actions/checkout@v2
|
28
|
+
- name: Set up Ruby 2.5
|
29
|
+
uses: actions/setup-ruby@v1
|
30
|
+
with:
|
31
|
+
ruby-version: 2.5.x
|
32
|
+
- name: Build and test with Rake
|
33
|
+
run: |
|
34
|
+
gem install bundler
|
35
|
+
bundle install --jobs 4 --retry 3
|
36
|
+
bundle exec rake
|
37
|
+
|
38
|
+
build2_6:
|
39
|
+
runs-on: ubuntu-latest
|
40
|
+
steps:
|
41
|
+
- uses: actions/checkout@v2
|
42
|
+
- name: Set up Ruby 2.6
|
43
|
+
uses: actions/setup-ruby@v1
|
44
|
+
with:
|
45
|
+
ruby-version: 2.6.x
|
46
|
+
- name: Build and test with Rake
|
47
|
+
run: |
|
48
|
+
gem install bundler
|
49
|
+
bundle install --jobs 4 --retry 3
|
50
|
+
bundle exec rake
|
51
|
+
|
52
|
+
build2_7:
|
53
|
+
runs-on: ubuntu-latest
|
54
|
+
steps:
|
55
|
+
- uses: actions/checkout@v2
|
56
|
+
- name: Set up Ruby 2.7
|
57
|
+
uses: actions/setup-ruby@v1
|
58
|
+
with:
|
59
|
+
ruby-version: 2.7.x
|
60
|
+
- name: Build and test with Rake
|
61
|
+
run: |
|
62
|
+
gem install bundler
|
63
|
+
bundle install --jobs 4 --retry 3
|
64
|
+
bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,238 @@
|
|
1
|
+
# WithTax
|
2
|
+
|
3
|
+
WithTaxはクラスにextendし、`.attr_with_tax`で属性を指定すると、`#(指定した属性)_with_tax`というメソッドで税込金額を求められるようになります。
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Gemfileに下のように記述してください。
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'with_tax'
|
11
|
+
```
|
12
|
+
|
13
|
+
それから下を実行してください:
|
14
|
+
|
15
|
+
```console
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
または自分でインストールするには:
|
20
|
+
|
21
|
+
```console
|
22
|
+
$ gem install with_tax
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### 基本的な使い方
|
28
|
+
|
29
|
+
下のように`WithTax`を`extend`し、`attr_with_tax`で対象の属性を指定してください。
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class SampleItem
|
33
|
+
extend WithTax
|
34
|
+
|
35
|
+
attr_accessor :name, :price
|
36
|
+
attr_with_tax :price
|
37
|
+
|
38
|
+
def initialize(name, price)
|
39
|
+
@name = name
|
40
|
+
@price = price
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
そうすると下のように`属性名_with_tax`というメソッドが利用できるようになり、税込み価格が取得できます。小数点以下は切り上げになっています。
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
sample_item = SampleItem.new('Some item', 123)
|
49
|
+
sample_item.price # => 123
|
50
|
+
sample_item.price_with_tax #=> 136
|
51
|
+
```
|
52
|
+
|
53
|
+
### 適用日
|
54
|
+
|
55
|
+
税率改定があるときに、計算の基準になる日を指定することもできます。
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
sample_item = SampleItem.new('Some item', 123)
|
59
|
+
sample_item.price # => 123
|
60
|
+
sample_item.price_with_tax(Date.parse('2019/09/30')) #=> 132
|
61
|
+
```
|
62
|
+
|
63
|
+
### 詳細設定
|
64
|
+
|
65
|
+
#### 小数の取り扱い
|
66
|
+
|
67
|
+
デフォルトでは切り上げになっていますが、`rounding_method`を設定することで切り替えることができます。
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
class SampleItem
|
71
|
+
extend WithTax
|
72
|
+
|
73
|
+
attr_accessor :name, :price
|
74
|
+
attr_with_tax :price, rounding_method: :floor
|
75
|
+
|
76
|
+
def initialize(name, price)
|
77
|
+
@name = name
|
78
|
+
@price = price
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
##### 切り捨て
|
84
|
+
|
85
|
+
`:floor`を設定すると小数点以下を切り捨てます。
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
class SampleItem
|
89
|
+
extend WithTax
|
90
|
+
|
91
|
+
attr_accessor :name, :price
|
92
|
+
attr_with_tax :price, rounding_method: :floor
|
93
|
+
|
94
|
+
def initialize(name, price)
|
95
|
+
@name = name
|
96
|
+
@price = price
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
sample_item1 = SampleItem.new('Sample Item', 123)
|
101
|
+
sample_item1.price #=> 123
|
102
|
+
sample_item1.price_with_tax #=> 135
|
103
|
+
|
104
|
+
sample_item2 = SampleItem.new('Sample Item', 345)
|
105
|
+
sample_item2.price #=> 345
|
106
|
+
sample_item2.price_with_tax #=> 379
|
107
|
+
```
|
108
|
+
|
109
|
+
##### 四捨五入
|
110
|
+
|
111
|
+
`:round`を設定すると小数点以下を四捨五入します。
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
class SampleItem
|
115
|
+
extend WithTax
|
116
|
+
|
117
|
+
attr_accessor :name, :price
|
118
|
+
attr_with_tax :price, rounding_method: :round
|
119
|
+
|
120
|
+
def initialize(name, price)
|
121
|
+
@name = name
|
122
|
+
@price = price
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
sample_item1 = SampleItem.new('Sample Item', 345)
|
127
|
+
sample_item1.price #=> 123
|
128
|
+
sample_item1.price_with_tax #=> 135
|
129
|
+
|
130
|
+
sample_item2 = SampleItem.new('Sample Item', 345)
|
131
|
+
sample_item2.price #=> 345
|
132
|
+
sample_item2.price_with_tax #=> 380
|
133
|
+
```
|
134
|
+
|
135
|
+
##### 処理なし
|
136
|
+
|
137
|
+
`nil`を設定すると小数点以下を処理しません。
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
class SampleItem
|
141
|
+
extend WithTax
|
142
|
+
|
143
|
+
attr_accessor :name, :price
|
144
|
+
attr_with_tax :price, rounding_method: nil
|
145
|
+
|
146
|
+
def initialize(name, price)
|
147
|
+
@name = name
|
148
|
+
@price = price
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
sample_item.price #=> 123
|
153
|
+
sample_item.price_with_tax #=> 135.3
|
154
|
+
```
|
155
|
+
|
156
|
+
#### 軽減税率
|
157
|
+
|
158
|
+
デフォルトでは`10%`ですが、`rate_type`に`:reduced`を設定することで軽減税率の8%に切り替えることができます。
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
class SampleItem
|
162
|
+
extend WithTax
|
163
|
+
|
164
|
+
attr_accessor :name, :price
|
165
|
+
attr_with_tax :price, rate_type: :reduced
|
166
|
+
|
167
|
+
def initialize(name, price)
|
168
|
+
@name = name
|
169
|
+
@price = price
|
170
|
+
end
|
171
|
+
end
|
172
|
+
```
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
sample_item.price #=> 123
|
176
|
+
sample_item.price_with_tax #=> 133
|
177
|
+
```
|
178
|
+
|
179
|
+
#### 複数のオプション設定
|
180
|
+
|
181
|
+
`attr_with_max`は複数のオプションも設定できます。
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
class SampleItem
|
185
|
+
extend WithTax
|
186
|
+
|
187
|
+
attr_accessor :name, :price
|
188
|
+
attr_with_tax :price, rounding_method: :round, rate_type: :reduced
|
189
|
+
|
190
|
+
def initialize(name, price)
|
191
|
+
@name = name
|
192
|
+
@price = price
|
193
|
+
end
|
194
|
+
end
|
195
|
+
```
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
sample_item.price #=> 123
|
199
|
+
sample_item.price_with_tax #=> 133
|
200
|
+
```
|
201
|
+
|
202
|
+
#### 複数の属性への設定
|
203
|
+
|
204
|
+
`attr_with_max`は複数の属性にも設定できます。
|
205
|
+
|
206
|
+
```ruby
|
207
|
+
class SampleItem
|
208
|
+
extend WithTax
|
209
|
+
|
210
|
+
attr_accessor :name, :price, :price_takeout
|
211
|
+
attr_with_tax :price, rounding_method: :round
|
212
|
+
attr_with_tax :price_takeout, rounding_method: :round, rate_type: :reduced
|
213
|
+
|
214
|
+
def initialize(name, price, price_takeout)
|
215
|
+
@name = name
|
216
|
+
@price = price
|
217
|
+
@price_takeout = price_takeout
|
218
|
+
end
|
219
|
+
end
|
220
|
+
```
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
sample_item.price #=> 123
|
224
|
+
sample_item.price_with_tax #=> 135
|
225
|
+
sample_item.price_takeout #=> 123
|
226
|
+
sample_item.price_takeout_with_tax #=> 133
|
227
|
+
```
|
228
|
+
|
229
|
+
## Development
|
230
|
+
|
231
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
232
|
+
|
233
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
234
|
+
|
235
|
+
## Contributing
|
236
|
+
|
237
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ken1flan/with_tax.
|
238
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "with_tax"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/lib/with_tax.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
require 'bigdecimal/util'
|
3
|
+
require 'with_tax/rate'
|
4
|
+
|
5
|
+
module WithTax
|
6
|
+
class MethodDefiner
|
7
|
+
def self.add_method(klass, attr_name, rounding_method: :ceil, rate_type: :default)
|
8
|
+
klass.class_eval do
|
9
|
+
method_name = "#{attr_name}_with_tax"
|
10
|
+
define_method(method_name) do |effective_date = nil|
|
11
|
+
mag = 1 + WithTax::Rate.rate(effective_date, rate_type)
|
12
|
+
ret = send(attr_name).to_s.to_d * mag
|
13
|
+
|
14
|
+
rounding_method ? ret.send(rounding_method) : ret
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module WithTax
|
5
|
+
class Rate
|
6
|
+
TAX_TABLE = File.open("#{__dir__}/rate.yml") { |f| YAML.safe_load(f) }
|
7
|
+
|
8
|
+
def self.rate(target_date = nil, target_type = nil)
|
9
|
+
t = target_date
|
10
|
+
t ||= Date.today
|
11
|
+
_k, rate = TAX_TABLE.find { |k, _v| Date.parse(k) <= t }
|
12
|
+
rate ||= {}
|
13
|
+
rate[target_type&.to_s] || rate['default'] || 0.0
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/with_tax.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'lib/with_tax/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "with_tax"
|
5
|
+
spec.version = WithTax::VERSION
|
6
|
+
spec.authors = ["ken1flan"]
|
7
|
+
spec.email = ["ken1flan@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Allows you to calculate sales tax without defining a method in a class.}
|
10
|
+
spec.homepage = "https://github.com/ken1flan/with_tax"
|
11
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
12
|
+
|
13
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
14
|
+
spec.metadata["source_code_uri"] = "https://github.com/ken1flan/with_tax"
|
15
|
+
spec.metadata["changelog_uri"] = "https://github.com/ken1flan/with_tax/CHANGELOG.md"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: with_tax
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.20200414
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ken1flan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- ken1flan@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".github/workflows/ruby.yml"
|
21
|
+
- ".gitignore"
|
22
|
+
- ".rspec"
|
23
|
+
- ".travis.yml"
|
24
|
+
- Gemfile
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- lib/with_tax.rb
|
30
|
+
- lib/with_tax/method_definer.rb
|
31
|
+
- lib/with_tax/rate.rb
|
32
|
+
- lib/with_tax/rate.yml
|
33
|
+
- lib/with_tax/version.rb
|
34
|
+
- with_tax.gemspec
|
35
|
+
homepage: https://github.com/ken1flan/with_tax
|
36
|
+
licenses: []
|
37
|
+
metadata:
|
38
|
+
homepage_uri: https://github.com/ken1flan/with_tax
|
39
|
+
source_code_uri: https://github.com/ken1flan/with_tax
|
40
|
+
changelog_uri: https://github.com/ken1flan/with_tax/CHANGELOG.md
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 2.3.0
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.0.3
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Allows you to calculate sales tax without defining a method in a class.
|
60
|
+
test_files: []
|