percentage 1.2.0 → 1.3.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: d7dde72257c6d27abdfca2ca058a7048287cbdbcd1421b70e2e517144242e639
4
- data.tar.gz: '0619980650ea0118d1127e3aee909076a8edba57e94cf3a9d686f4d358f00976'
3
+ metadata.gz: fa46273b5d11f1c29c5cf694621b8980f4d8f3f4b7541fe5e97c7184f49c20ec
4
+ data.tar.gz: 87921d27b21b0e3da81f848374c62738b091821935d771dc6d10b5216602b613
5
5
  SHA512:
6
- metadata.gz: 69b9ab800bc4898f765dd2e5667ae35b74ae02a59276fe5d85680267a5eaf0ae3cda8fe4188d18dcf0a530c46a19e2a2d5c5691dadf9bfc0851592951911361e
7
- data.tar.gz: 333b4e6f9d8867aebf17ecdceab9d36997b026d2bdc1efd09612dfb92797d68dbf6d5eab3f40a1921b0406ae981ef1589eeb7efd06db242abcfc1330cddc9cdd
6
+ metadata.gz: 96ee728f7f7f2b16387567f9fa4c78fb7f677a64083cc6ba04dfd04b8e18d6eca8dab868b7b6e54a982826e2972912b6a87dbbc707e46d3d3dd2327614ffee92
7
+ data.tar.gz: df6983418f01b9b06998428fa7eeb4b8b0800d5fa7f8e654112f5b13cf0920385a897f123724b1fb83b99c02fea8bd47a2796e654d5e63ebeec26c4f38c5469b
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012-2019 TIMCRAFT
1
+ Copyright (c) 2012-2020 TIMCRAFT
2
2
 
3
3
  This is an Open Source project licensed under the terms of the LGPLv3 license.
4
4
  Please see <http://www.gnu.org/licenses/lgpl-3.0.html> for license text.
@@ -75,8 +75,16 @@ class Percentage
75
75
  end
76
76
  end
77
77
 
78
- def truncate(n)
79
- self.class.new(fractional_value.truncate(n))
78
+ def truncate(ndigits = nil)
79
+ return self if @value.integer?
80
+
81
+ value = @value * 100
82
+
83
+ if ndigits.nil?
84
+ self.class.new(value.truncate)
85
+ else
86
+ self.class.new(value.truncate(ndigits) / 100)
87
+ end
80
88
  end
81
89
 
82
90
  def scale(n)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'percentage'
3
- s.version = '1.2.0'
3
+ s.version = '1.3.0'
4
4
  s.license = 'LGPL-3.0'
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.authors = ['Tim Craft']
@@ -8,9 +8,7 @@ Gem::Specification.new do |s|
8
8
  s.homepage = 'https://github.com/timcraft/percentage'
9
9
  s.description = 'A little library for working with percentages'
10
10
  s.summary = 'See description'
11
- s.files = Dir.glob('{lib,spec}/**/*') + %w(LICENSE.txt README.md Rakefile.rb percentage.gemspec)
11
+ s.files = Dir.glob('lib/**/*.rb') + %w(LICENSE.txt README.md percentage.gemspec)
12
12
  s.required_ruby_version = '>= 1.9.3'
13
- s.add_development_dependency('rake', '~> 12')
14
- s.add_development_dependency('minitest', '~> 5')
15
13
  s.require_path = 'lib'
16
14
  end
metadata CHANGED
@@ -1,43 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: percentage
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Craft
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-26 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: rake
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '12'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '12'
27
- - !ruby/object:Gem::Dependency
28
- name: minitest
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '5'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '5'
11
+ date: 2020-03-27 00:00:00.000000000 Z
12
+ dependencies: []
41
13
  description: A little library for working with percentages
42
14
  email:
43
15
  - mail@timcraft.com
@@ -47,10 +19,8 @@ extra_rdoc_files: []
47
19
  files:
48
20
  - LICENSE.txt
49
21
  - README.md
50
- - Rakefile.rb
51
22
  - lib/percentage.rb
52
23
  - percentage.gemspec
53
- - spec/percentage_spec.rb
54
24
  homepage: https://github.com/timcraft/percentage
55
25
  licenses:
56
26
  - LGPL-3.0
@@ -70,7 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
40
  - !ruby/object:Gem::Version
71
41
  version: '0'
72
42
  requirements: []
73
- rubygems_version: 3.0.3
43
+ rubygems_version: 3.1.2
74
44
  signing_key:
75
45
  specification_version: 4
76
46
  summary: See description
@@ -1,8 +0,0 @@
1
- require 'rake/testtask'
2
-
3
- task :default => :spec
4
-
5
- Rake::TestTask.new(:spec) do |t|
6
- t.test_files = FileList['spec/*_spec.rb']
7
- t.warning = true
8
- end
@@ -1,382 +0,0 @@
1
- require 'minitest/autorun'
2
-
3
- require_relative '../lib/percentage'
4
-
5
- describe 'Percentage object' do
6
- it 'is comparable' do
7
- percentage = Percentage.new(Rational(1, 8))
8
-
9
- (Comparable === percentage).must_equal(true)
10
- end
11
-
12
- it 'is comparable to other objects of the same class' do
13
- (Percentage.new(Rational(1, 8)) > Percentage.new(Rational(1, 10))).must_equal(true)
14
- end
15
-
16
- it 'is comparable to other numeric objects' do
17
- (Percentage.new(Rational(1, 8)) > Rational(1, 10)).must_equal(true)
18
- end
19
-
20
- it 'can be used as a hash key' do
21
- hash = Hash.new(0)
22
-
23
- 3.times { hash[Percentage.new(Rational(1, 10))] += 1 }
24
-
25
- hash[Percentage.new(Rational(1, 10))].must_equal(3)
26
- end
27
-
28
- it 'can be frozen' do
29
- percentage = Percentage.new(10).freeze
30
- percentage.frozen?.must_equal(true)
31
- percentage.to_i.must_equal(10)
32
- end
33
- end
34
-
35
- describe 'Percentage object initialized with an integer value' do
36
- before do
37
- @percentage = Percentage.new(10)
38
- end
39
-
40
- describe 'value method' do
41
- it 'returns the value passed to the constructor' do
42
- @percentage.value.must_equal(10)
43
- end
44
- end
45
-
46
- describe 'to_i method' do
47
- it 'returns the integer value of the percentage' do
48
- @percentage.to_i.must_equal(10)
49
- end
50
- end
51
-
52
- describe 'to_f method' do
53
- it 'returns the float value of the percentage' do
54
- @percentage.to_f.must_be_close_to(10.0)
55
- end
56
- end
57
-
58
- describe 'to_s method' do
59
- it 'returns the integer value of the percentage suffixed with the percent symbol' do
60
- @percentage.to_s.must_equal('10%')
61
- end
62
- end
63
-
64
- describe 'to_r method' do
65
- it 'returns the rational value of the percentage' do
66
- @percentage.to_r.must_equal(Rational(1, 10))
67
- end
68
- end
69
-
70
- describe 'zero query method' do
71
- it 'returns true if the percentage has a zero value' do
72
- Percentage.new(0).zero?.must_equal(true)
73
- end
74
-
75
- it 'returns false otherwise' do
76
- @percentage.zero?.must_equal(false)
77
- end
78
- end
79
-
80
- describe 'truncate method' do
81
- it 'returns a percentage object with a truncated rational value' do
82
- percentage = @percentage.truncate(1)
83
- percentage.must_be_instance_of(Percentage)
84
- percentage.value.must_equal(Rational(1, 10))
85
- end
86
- end
87
-
88
- describe 'scale method' do
89
- it 'returns a percentage object with the value of the percentage multiplied by the integer argument' do
90
- percentage = @percentage.scale(2)
91
- percentage.must_be_instance_of(Percentage)
92
- percentage.value.must_equal(20)
93
- end
94
- end
95
- end
96
-
97
- describe 'Percentage object initialized with a rational value' do
98
- before do
99
- @percentage = Percentage.new(Rational(1, 8))
100
- end
101
-
102
- describe 'value method' do
103
- it 'returns the value passed to the constructor' do
104
- @percentage.value.must_equal(Rational(1, 8))
105
- end
106
- end
107
-
108
- describe 'to_i method' do
109
- it 'returns the integer value of the percentage' do
110
- @percentage.to_i.must_equal(12)
111
- end
112
- end
113
-
114
- describe 'to_f method' do
115
- it 'returns the float value of the percentage' do
116
- @percentage.to_f.must_be_close_to(12.5)
117
- end
118
- end
119
-
120
- describe 'to_s method' do
121
- it 'returns the decimal value of the percentage suffixed with the percent symbol' do
122
- @percentage.to_s.must_equal('12.5%')
123
- end
124
- end
125
-
126
- describe 'to_r method' do
127
- it 'returns the rational value of the percentage' do
128
- @percentage.to_r.must_equal(Rational(1, 8))
129
- end
130
- end
131
-
132
- describe 'zero query method' do
133
- it 'returns true if the percentage has a zero value' do
134
- Percentage.new(Rational(0)).zero?.must_equal(true)
135
- end
136
-
137
- it 'returns false otherwise' do
138
- @percentage.zero?.must_equal(false)
139
- end
140
- end
141
-
142
- describe 'truncate method' do
143
- it 'returns a percentage object with a truncated rational value' do
144
- percentage = @percentage.truncate(1)
145
- percentage.must_be_instance_of(Percentage)
146
- percentage.value.must_equal(Rational(1, 10))
147
- end
148
- end
149
-
150
- describe 'scale method' do
151
- it 'returns a percentage object with the value of the percentage multiplied by the integer argument' do
152
- percentage = @percentage.scale(2)
153
- percentage.must_be_instance_of(Percentage)
154
- percentage.value.must_equal(Rational(1, 4))
155
- end
156
- end
157
- end
158
-
159
- describe 'Percentage object initialized with a decimal value' do
160
- before do
161
- @percentage = Percentage.new(BigDecimal('0.175'))
162
- end
163
-
164
- describe 'value method' do
165
- it 'returns the value passed to the constructor' do
166
- @percentage.value.must_equal(BigDecimal('0.175'))
167
- end
168
- end
169
-
170
- describe 'to_i method' do
171
- it 'returns the integer value of the percentage' do
172
- @percentage.to_i.must_equal(17)
173
- end
174
- end
175
-
176
- describe 'to_f method' do
177
- it 'returns the float value of the percentage' do
178
- @percentage.to_f.must_be_close_to(17.5)
179
- end
180
- end
181
-
182
- describe 'to_s method' do
183
- it 'returns the decimal value of the percentage suffixed with the percent symbol' do
184
- @percentage.to_s.must_equal('17.5%')
185
- end
186
- end
187
-
188
- describe 'to_r method' do
189
- it 'returns the rational value of the percentage' do
190
- @percentage.to_r.must_equal(Rational(175, 1000))
191
- end
192
- end
193
-
194
- describe 'zero query method' do
195
- it 'returns true if the percentage has a zero value' do
196
- Percentage.new(BigDecimal('0')).zero?.must_equal(true)
197
- end
198
-
199
- it 'returns false otherwise' do
200
- @percentage.zero?.must_equal(false)
201
- end
202
- end
203
-
204
- describe 'truncate method' do
205
- it 'returns a percentage object with a truncated decimal value' do
206
- percentage = @percentage.truncate(1)
207
- percentage.must_be_instance_of(Percentage)
208
- percentage.value.must_equal(BigDecimal('0.1'))
209
- end
210
- end
211
-
212
- describe 'scale method' do
213
- it 'returns a percentage object with the value of the percentage multiplied by the integer argument' do
214
- percentage = @percentage.scale(2)
215
- percentage.must_be_instance_of(Percentage)
216
- percentage.value.must_equal(BigDecimal('0.35'))
217
- end
218
- end
219
- end
220
-
221
- describe 'Addition of percentage objects' do
222
- it 'returns a percentage object with the value of the two percentages added together' do
223
- percentage = Percentage.new(10) + Percentage.new(10)
224
- percentage.must_be_instance_of(Percentage)
225
- percentage.value.must_equal(20)
226
-
227
- percentage = Percentage.new(Rational(1, 8)) + Percentage.new(10)
228
- percentage.must_be_instance_of(Percentage)
229
- percentage.value.must_equal(Rational(9, 40))
230
-
231
- percentage = Percentage.new(BigDecimal('0.175')) + Percentage.new(BigDecimal('0.025'))
232
- percentage.must_be_instance_of(Percentage)
233
- percentage.value.must_equal(BigDecimal('0.2'))
234
- end
235
- end
236
-
237
- describe 'Addition of percentage object with another type of object' do
238
- it 'raises an exception' do
239
- proc { Percentage.new(10) + 5 }.must_raise(TypeError)
240
- end
241
- end
242
-
243
- describe 'Multiplication of percentage objects' do
244
- it 'returns a percentage object with the fractional value of the two percentages multiplied together' do
245
- percentage = Percentage.new(10) * Percentage.new(10)
246
- percentage.must_be_instance_of(Percentage)
247
- percentage.value.must_equal(Rational(1, 100))
248
-
249
- percentage = Percentage.new(Rational(1, 8)) * Percentage.new(10)
250
- percentage.must_be_instance_of(Percentage)
251
- percentage.value.must_equal(Rational(1, 80))
252
-
253
- percentage = Percentage.new(BigDecimal('0.175')) * Percentage.new(10)
254
- percentage.must_be_instance_of(Percentage)
255
- percentage.value.must_equal(Rational(7, 400))
256
- end
257
- end
258
-
259
- describe 'Multiplication of a decimal object with a percentage object' do
260
- it 'returns a decimal object with the value of the decimal multiplied by the fractional value of the percentage' do
261
- percentage, decimal = Percentage.new(BigDecimal('0.175')), BigDecimal('99.00')
262
-
263
- (decimal * percentage).must_equal(BigDecimal('17.325'))
264
- (percentage * decimal).must_equal(BigDecimal('17.325'))
265
- end
266
- end
267
-
268
- describe 'Percentage object equality' do
269
- describe 'double equals method' do
270
- it 'returns true for percentage objects with the same fractional value' do
271
- (Percentage.new(50) == Percentage.new(50)).must_equal(true)
272
- (Percentage.new(50) == Percentage.new(Rational(1, 2))).must_equal(true)
273
- (Percentage.new(50) == Percentage.new(BigDecimal('0.5'))).must_equal(true)
274
- end
275
-
276
- it 'returns false otherwise' do
277
- (Percentage.new(50) == Percentage.new(100)).must_equal(false)
278
- end
279
- end
280
-
281
- describe 'eql query method' do
282
- it 'returns true for percentage objects with exactly the same fractional value' do
283
- (Percentage.new(50).eql? Percentage.new(50)).must_equal(true)
284
- end
285
-
286
- it 'returns false otherwise' do
287
- (Percentage.new(50).eql? Percentage.new(Rational(1, 2))).must_equal(false)
288
- (Percentage.new(50).eql? Percentage.new(BigDecimal('0.5'))).must_equal(false)
289
- (Percentage.new(50).eql? Percentage.new(100)).must_equal(false)
290
- end
291
- end
292
- end
293
-
294
- describe 'Percentage method' do
295
- describe 'when called with an integer argument' do
296
- it 'returns a percentage object with the integer value' do
297
- percentage = Percentage(10)
298
- percentage.must_be_instance_of(Percentage)
299
- percentage.value.must_equal(10)
300
- end
301
- end
302
-
303
- describe 'when called with a decimal argument' do
304
- it 'returns a percentage object with the value of the argument divided by 100' do
305
- percentage = Percentage(BigDecimal('17.5'))
306
- percentage.must_be_instance_of(Percentage)
307
- percentage.value.must_equal(BigDecimal('0.175'))
308
- end
309
- end
310
-
311
- describe 'when called with a rational argument' do
312
- it 'returns a percentage object with the value of the argument divided by 100' do
313
- percentage = Percentage(Rational(100, 3))
314
- percentage.must_be_instance_of(Percentage)
315
- percentage.value.must_equal(Rational(1, 3))
316
- end
317
- end
318
- end
319
-
320
- describe 'Percentage change method' do
321
- it 'returns the difference between the arguments as a percentage of the first argument' do
322
- percentage = Percentage.change(2, 3)
323
- percentage.must_be_instance_of(Percentage)
324
- percentage.must_equal(Percentage.new(50))
325
- end
326
- end
327
-
328
- describe 'BigDecimal to_percentage method' do
329
- it 'returns a percentage object with the value of the decimal as a percentage' do
330
- percentage = BigDecimal(90).to_percentage
331
- percentage.must_be_instance_of(Percentage)
332
- percentage.value.must_equal(BigDecimal('0.9'))
333
- end
334
- end
335
-
336
- describe 'BigDecimal percent_of method' do
337
- it 'returns the value of the receiver as a percentage multiplied by the argument' do
338
- BigDecimal(90).percent_of(100).must_equal(90)
339
- BigDecimal(90).percent_of(BigDecimal('15')).must_equal(BigDecimal('13.5'))
340
- BigDecimal(90).percent_of(Rational(150, 2)).must_equal(Rational(135, 2))
341
- end
342
- end
343
-
344
- describe 'BigDecimal as_percentage_of method' do
345
- it 'returns a percentage object with the value of the decimal divided by the argument' do
346
- percentage = BigDecimal('50.00').as_percentage_of(BigDecimal('100.00'))
347
- percentage.must_be_instance_of(Percentage)
348
- percentage.value.must_equal(BigDecimal('0.5'))
349
- end
350
- end
351
-
352
- describe 'Integer to_percentage method' do
353
- it 'returns a percentage object with the value of the integer' do
354
- percentage = 10.to_percentage
355
- percentage.must_be_instance_of(Percentage)
356
- percentage.value.must_equal(10)
357
- end
358
- end
359
-
360
- describe 'Integer percent method' do
361
- it 'returns a percentage object with the value of the integer' do
362
- percentage = 10.percent
363
- percentage.must_be_instance_of(Percentage)
364
- percentage.value.must_equal(10)
365
- end
366
- end
367
-
368
- describe 'Integer percent_of method' do
369
- it 'returns the value of the receiver as a percentage multiplied by the argument' do
370
- 10.percent_of(100).must_equal(10)
371
- 10.percent_of(BigDecimal('15')).must_equal(BigDecimal('1.5'))
372
- 10.percent_of(Rational(150, 2)).must_equal(Rational(15, 2))
373
- end
374
- end
375
-
376
- describe 'Integer as_percentage_of method' do
377
- it 'returns a percentage object with the value of the integer divided by the argument' do
378
- percentage = 10.as_percentage_of(20)
379
- percentage.must_be_instance_of(Percentage)
380
- percentage.value.must_equal(Rational(1, 2))
381
- end
382
- end