percentage 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 +7 -0
- data/LICENSE.txt +4 -0
- data/README.md +33 -19
- data/Rakefile.rb +1 -0
- data/lib/percentage.rb +14 -5
- data/percentage.gemspec +6 -2
- data/spec/percentage_spec.rb +26 -2
- metadata +42 -15
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3c32ff68bc5ee26ca6f82898ffb94d6aba49153d8187999d1206c2920a6d132b
|
4
|
+
data.tar.gz: 6c46179997da767a0cce9dba903d9b33b4ca5a00a7fe62e7ae8c2a5131c409bb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 77c8e9753c605d8ff006d7c50db1fd78478eef8983a1020951fb8c46ec752438933f30829baee3d0d303877b8533b1be8c376001fc98a0ca6cce6826fb08d072
|
7
|
+
data.tar.gz: 51a800e6f7ebd074925b7cc91dc603fb1c064f5965026276645c0781ec92b95388cd5ba5708caa7dce3fc063a9b9802c8ad077d220502f869801a02a229551ca
|
data/LICENSE.txt
ADDED
data/README.md
CHANGED
@@ -1,15 +1,20 @@
|
|
1
|
-
|
2
|
-
==========
|
1
|
+
# percentage
|
3
2
|
|
3
|
+
[](https://badge.fury.io/rb/percentage) [](https://travis-ci.org/timcraft/percentage)
|
4
4
|
|
5
|
-
A little library for working with percentages.
|
6
5
|
|
6
|
+
A little Ruby library for working with percentages.
|
7
7
|
|
8
|
-
Feature Tour
|
9
|
-
------------
|
10
8
|
|
11
|
-
|
12
|
-
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
$ gem install percentage
|
12
|
+
|
13
|
+
|
14
|
+
## Constructing Percentages
|
15
|
+
|
16
|
+
The `Percentage` method converts numeric objects to percentage objects
|
17
|
+
with values that you would expect:
|
13
18
|
|
14
19
|
```ruby
|
15
20
|
Percentage(50) # => 50%
|
@@ -20,7 +25,7 @@ Percentage(Rational(25, 2)) # => 12.5%
|
|
20
25
|
```
|
21
26
|
|
22
27
|
Percentage objects can also be constructed directly, but in this case
|
23
|
-
BigDecimal/Rational values are treated as fractions
|
28
|
+
BigDecimal/Rational values are treated as fractions:
|
24
29
|
|
25
30
|
```ruby
|
26
31
|
Percentage.new(50) # => 50%
|
@@ -30,6 +35,19 @@ Percentage.new(BigDecimal('0.175')) # => 17.5%
|
|
30
35
|
Percentage.new(Rational(1, 8)) # => 12.5%
|
31
36
|
```
|
32
37
|
|
38
|
+
Some shortcut methods are defined on Integer and BigDecimal for convenience:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
50.percent # => 50%
|
42
|
+
|
43
|
+
5.as_percentage_of(10) # => 50.0%
|
44
|
+
|
45
|
+
BigDecimal('2.9').to_percentage # => 2.9%
|
46
|
+
```
|
47
|
+
|
48
|
+
|
49
|
+
## Numeric features
|
50
|
+
|
33
51
|
As with other numerics, percentage objects are conceptually immutable.
|
34
52
|
Common numeric functionality like `to_i`, `to_f`, `to_s`, `to_r`, `zero?`,
|
35
53
|
and equality/comparison methods are defined.
|
@@ -46,9 +64,9 @@ They can also be "scaled up" using the `scale` method:
|
|
46
64
|
Percentage(10).scale(5) # => 50%
|
47
65
|
```
|
48
66
|
|
49
|
-
Multiplication is
|
50
|
-
BigDecimal objects can't be coerced into rational objects, so the
|
51
|
-
|
67
|
+
Multiplication is defined using the fractional value of the percentage.
|
68
|
+
BigDecimal objects can't be coerced into rational objects, so the
|
69
|
+
multiplication order will matter in some cases, for example:
|
52
70
|
|
53
71
|
```ruby
|
54
72
|
Percentage(50) * 10 # => (5/1)
|
@@ -61,23 +79,19 @@ Percentage(50) * BigDecimal('150.00') # raises TypeError
|
|
61
79
|
```
|
62
80
|
|
63
81
|
|
64
|
-
Bonus
|
65
|
-
------------
|
82
|
+
## Bonus extras
|
66
83
|
|
67
|
-
|
84
|
+
There's a #percent_of method defined on Integer and BigDecimal for percentage calculations:
|
68
85
|
|
69
86
|
```ruby
|
70
|
-
50.percent # => 50%
|
71
|
-
|
72
87
|
50.percent_of(BigDecimal(150)) # => BigDecimal('75.00')
|
73
88
|
|
74
89
|
10.percent_of(100) # => (10/1)
|
75
90
|
|
76
|
-
5.
|
91
|
+
BigDecimal('0.5').percent_of(88) # => BigDecimal('0.44')
|
77
92
|
```
|
78
93
|
|
79
|
-
|
80
|
-
change between two values:
|
94
|
+
There's also a `Percentage.change` method for calculating the percentage change between two values:
|
81
95
|
|
82
96
|
```ruby
|
83
97
|
Percentage.change(2, 3) # => 50.0%
|
data/Rakefile.rb
CHANGED
data/lib/percentage.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'rational'
|
2
1
|
require 'bigdecimal'
|
3
2
|
|
4
3
|
class Percentage
|
@@ -98,7 +97,7 @@ class Percentage
|
|
98
97
|
elsif BigDecimal === @value
|
99
98
|
(@value * 100).to_s('F')
|
100
99
|
else
|
101
|
-
|
100
|
+
(@value * 100).to_f.to_s
|
102
101
|
end
|
103
102
|
end
|
104
103
|
end
|
@@ -112,18 +111,28 @@ def Percentage.change(a, b)
|
|
112
111
|
end
|
113
112
|
|
114
113
|
class BigDecimal
|
114
|
+
def to_percentage
|
115
|
+
Percentage(self)
|
116
|
+
end
|
117
|
+
|
118
|
+
def percent_of(n)
|
119
|
+
n * Percentage(self)
|
120
|
+
end
|
121
|
+
|
115
122
|
def as_percentage_of(n)
|
116
123
|
Percentage.new(self / n)
|
117
124
|
end
|
118
125
|
end
|
119
126
|
|
120
127
|
class Integer
|
121
|
-
def
|
122
|
-
Percentage
|
128
|
+
def to_percentage
|
129
|
+
Percentage(self)
|
123
130
|
end
|
124
131
|
|
132
|
+
alias_method :percent, :to_percentage
|
133
|
+
|
125
134
|
def percent_of(n)
|
126
|
-
n * Percentage
|
135
|
+
n * Percentage(self)
|
127
136
|
end
|
128
137
|
|
129
138
|
def as_percentage_of(n)
|
data/percentage.gemspec
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'percentage'
|
3
|
-
s.version = '1.
|
3
|
+
s.version = '1.1.0'
|
4
|
+
s.license = 'LGPL-3.0'
|
4
5
|
s.platform = Gem::Platform::RUBY
|
5
6
|
s.authors = ['Tim Craft']
|
6
7
|
s.email = ['mail@timcraft.com']
|
7
8
|
s.homepage = 'http://github.com/timcraft/percentage'
|
8
9
|
s.description = 'A little library for working with percentages'
|
9
10
|
s.summary = 'See description'
|
10
|
-
s.files = Dir.glob('{lib,spec}/**/*') + %w(README.md Rakefile.rb percentage.gemspec)
|
11
|
+
s.files = Dir.glob('{lib,spec}/**/*') + %w(LICENSE.txt README.md Rakefile.rb percentage.gemspec)
|
12
|
+
s.required_ruby_version = '>= 1.9.3'
|
13
|
+
s.add_development_dependency('rake', '~> 12')
|
14
|
+
s.add_development_dependency('minitest', '~> 5')
|
11
15
|
s.require_path = 'lib'
|
12
16
|
end
|
data/spec/percentage_spec.rb
CHANGED
@@ -187,7 +187,7 @@ describe 'Percentage object initialized with a decimal value' do
|
|
187
187
|
|
188
188
|
describe 'zero query method' do
|
189
189
|
it 'returns true if the percentage has a zero value' do
|
190
|
-
Percentage.new(BigDecimal(0)).zero?.must_equal(true)
|
190
|
+
Percentage.new(BigDecimal('0')).zero?.must_equal(true)
|
191
191
|
end
|
192
192
|
|
193
193
|
it 'returns false otherwise' do
|
@@ -319,6 +319,22 @@ describe 'Percentage change method' do
|
|
319
319
|
end
|
320
320
|
end
|
321
321
|
|
322
|
+
describe 'BigDecimal to_percentage method' do
|
323
|
+
it 'returns a percentage object with the value of the decimal as a percentage' do
|
324
|
+
percentage = BigDecimal(90).to_percentage
|
325
|
+
percentage.must_be_instance_of(Percentage)
|
326
|
+
percentage.value.must_equal(BigDecimal('0.9'))
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
describe 'BigDecimal percent_of method' do
|
331
|
+
it 'returns the value of the receiver as a percentage multiplied by the argument' do
|
332
|
+
BigDecimal(90).percent_of(100).must_equal(90)
|
333
|
+
BigDecimal(90).percent_of(BigDecimal('15')).must_equal(BigDecimal('13.5'))
|
334
|
+
BigDecimal(90).percent_of(Rational(150, 2)).must_equal(Rational(135, 2))
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
322
338
|
describe 'BigDecimal as_percentage_of method' do
|
323
339
|
it 'returns a percentage object with the value of the decimal divided by the argument' do
|
324
340
|
percentage = BigDecimal('50.00').as_percentage_of(BigDecimal('100.00'))
|
@@ -327,6 +343,14 @@ describe 'BigDecimal as_percentage_of method' do
|
|
327
343
|
end
|
328
344
|
end
|
329
345
|
|
346
|
+
describe 'Integer to_percentage method' do
|
347
|
+
it 'returns a percentage object with the value of the integer' do
|
348
|
+
percentage = 10.to_percentage
|
349
|
+
percentage.must_be_instance_of(Percentage)
|
350
|
+
percentage.value.must_equal(10)
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
330
354
|
describe 'Integer percent method' do
|
331
355
|
it 'returns a percentage object with the value of the integer' do
|
332
356
|
percentage = 10.percent
|
@@ -338,7 +362,7 @@ end
|
|
338
362
|
describe 'Integer percent_of method' do
|
339
363
|
it 'returns the value of the receiver as a percentage multiplied by the argument' do
|
340
364
|
10.percent_of(100).must_equal(10)
|
341
|
-
10.percent_of(BigDecimal(15)).must_equal(BigDecimal('1.5'))
|
365
|
+
10.percent_of(BigDecimal('15')).must_equal(BigDecimal('1.5'))
|
342
366
|
10.percent_of(Rational(150, 2)).must_equal(Rational(15, 2))
|
343
367
|
end
|
344
368
|
end
|
metadata
CHANGED
@@ -1,16 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: percentage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Tim Craft
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
11
|
+
date: 2018-08-24 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'
|
14
41
|
description: A little library for working with percentages
|
15
42
|
email:
|
16
43
|
- mail@timcraft.com
|
@@ -18,34 +45,34 @@ executables: []
|
|
18
45
|
extensions: []
|
19
46
|
extra_rdoc_files: []
|
20
47
|
files:
|
21
|
-
-
|
22
|
-
- spec/percentage_spec.rb
|
48
|
+
- LICENSE.txt
|
23
49
|
- README.md
|
24
50
|
- Rakefile.rb
|
51
|
+
- lib/percentage.rb
|
25
52
|
- percentage.gemspec
|
53
|
+
- spec/percentage_spec.rb
|
26
54
|
homepage: http://github.com/timcraft/percentage
|
27
|
-
licenses:
|
55
|
+
licenses:
|
56
|
+
- LGPL-3.0
|
57
|
+
metadata: {}
|
28
58
|
post_install_message:
|
29
59
|
rdoc_options: []
|
30
60
|
require_paths:
|
31
61
|
- lib
|
32
62
|
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
63
|
requirements:
|
35
|
-
- -
|
64
|
+
- - ">="
|
36
65
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
66
|
+
version: 1.9.3
|
38
67
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
68
|
requirements:
|
41
|
-
- -
|
69
|
+
- - ">="
|
42
70
|
- !ruby/object:Gem::Version
|
43
71
|
version: '0'
|
44
72
|
requirements: []
|
45
73
|
rubyforge_project:
|
46
|
-
rubygems_version:
|
74
|
+
rubygems_version: 2.7.6
|
47
75
|
signing_key:
|
48
|
-
specification_version:
|
76
|
+
specification_version: 4
|
49
77
|
summary: See description
|
50
78
|
test_files: []
|
51
|
-
has_rdoc:
|