percentage 1.2.0 → 2.0.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/CHANGES.md +43 -0
- data/LICENSE.txt +1 -1
- data/README.md +10 -3
- data/lib/percentage/yaml.rb +29 -0
- data/lib/percentage.rb +23 -3
- data/percentage.gemspec +10 -6
- metadata +16 -40
- data/Rakefile.rb +0 -8
- data/spec/percentage_spec.rb +0 -382
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d73c317eceabcfd5b4b4f385f7def4664d5e7d827e925d101a306ba404170b69
|
4
|
+
data.tar.gz: 3f225768beb51e4d8a743f2550be17119773ef392550369c2756d603bf448d68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50b8c3ca5f618908e1ea75c2bb5076bf87a27d902781f96267f76954b9ec4481965a3dddc1a2f259b677a151bcf6bd39f48093e42307430335eb459b7270d3e7
|
7
|
+
data.tar.gz: 16e31215f053b38882f6b96e49e8b5c13a106342d7fefc0f4a5a2e87e8ce60aae95f64d5d65b279c3ec2e04e86b6230ca136904140b93c3321079903732b02ac
|
data/CHANGES.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# 2.0.0
|
2
|
+
|
3
|
+
* Added Percentage#to_d
|
4
|
+
|
5
|
+
* Percentage instances are now frozen on initialization
|
6
|
+
|
7
|
+
* Changed `Percentage.change` to return `nil` when first argument is zero
|
8
|
+
|
9
|
+
# 1.4.1
|
10
|
+
|
11
|
+
* Added CHANGES.md to gem files
|
12
|
+
|
13
|
+
* Fixed outdated changelog_uri
|
14
|
+
|
15
|
+
# 1.4.0
|
16
|
+
|
17
|
+
* Added optional YAML integration
|
18
|
+
|
19
|
+
This makes it possible to dump/load percentage objects to/from YAML as scalar values. For example:
|
20
|
+
|
21
|
+
require 'percentage/yaml'
|
22
|
+
|
23
|
+
puts YAML.dump([Percentage(BigDecimal('17.5'))])
|
24
|
+
|
25
|
+
This functionality is not enabled by default, due to the use of Module#prepend.
|
26
|
+
|
27
|
+
# 1.3.0
|
28
|
+
|
29
|
+
* Added ndigits argument to Percentage#truncate
|
30
|
+
|
31
|
+
# 1.2.0
|
32
|
+
|
33
|
+
* Removed memoization so that Percentage objects can be frozen (thanks @iamvery)
|
34
|
+
|
35
|
+
# 1.1.0
|
36
|
+
|
37
|
+
* Added BigDecimal#to_percentage and Integer#to_percentage
|
38
|
+
|
39
|
+
* Added BigDecimal#percent_of (thanks @mikeymicrophone)
|
40
|
+
|
41
|
+
# 1.0.0
|
42
|
+
|
43
|
+
* First version!
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,19 @@
|
|
1
1
|
# percentage
|
2
2
|
|
3
|
-
|
3
|
+

|
4
|
+

|
4
5
|
|
5
6
|
|
6
|
-
|
7
|
+
Ruby gem for working with percentages.
|
7
8
|
|
8
9
|
|
9
|
-
##
|
10
|
+
## Install
|
11
|
+
|
12
|
+
Using Bundler:
|
13
|
+
|
14
|
+
$ bundle add percentage
|
15
|
+
|
16
|
+
Using RubyGems:
|
10
17
|
|
11
18
|
$ gem install percentage
|
12
19
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
class Percentage
|
4
|
+
def encode_with(coder)
|
5
|
+
coder.represent_scalar(nil, to_s)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ScalarScannerPatch
|
9
|
+
INTEGER = /\A(\d+)%\z/
|
10
|
+
|
11
|
+
DECIMAL = /\A(\d+\.\d+)%\z/
|
12
|
+
|
13
|
+
def tokenize(string)
|
14
|
+
if !string.empty? && string.match(INTEGER)
|
15
|
+
return Percentage($1.to_i)
|
16
|
+
end
|
17
|
+
|
18
|
+
if !string.empty? && string.match(DECIMAL)
|
19
|
+
return Percentage(BigDecimal($1))
|
20
|
+
end
|
21
|
+
|
22
|
+
super string
|
23
|
+
end
|
24
|
+
|
25
|
+
YAML::ScalarScanner.prepend(self)
|
26
|
+
end
|
27
|
+
|
28
|
+
private_constant :ScalarScannerPatch
|
29
|
+
end
|
data/lib/percentage.rb
CHANGED
@@ -7,6 +7,8 @@ class Percentage
|
|
7
7
|
|
8
8
|
def initialize(value)
|
9
9
|
@value = value
|
10
|
+
|
11
|
+
freeze
|
10
12
|
end
|
11
13
|
|
12
14
|
def to_i
|
@@ -25,6 +27,16 @@ class Percentage
|
|
25
27
|
fractional_value.to_r
|
26
28
|
end
|
27
29
|
|
30
|
+
def to_d
|
31
|
+
if @value.integer?
|
32
|
+
@value.to_d / 100
|
33
|
+
elsif BigDecimal === @value
|
34
|
+
@value
|
35
|
+
else
|
36
|
+
@value.to_d(0)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
28
40
|
def zero?
|
29
41
|
@value.zero?
|
30
42
|
end
|
@@ -75,8 +87,16 @@ class Percentage
|
|
75
87
|
end
|
76
88
|
end
|
77
89
|
|
78
|
-
def truncate(
|
79
|
-
self.
|
90
|
+
def truncate(ndigits = nil)
|
91
|
+
return self if @value.integer?
|
92
|
+
|
93
|
+
value = @value * 100
|
94
|
+
|
95
|
+
if ndigits.nil?
|
96
|
+
self.class.new(value.truncate)
|
97
|
+
else
|
98
|
+
self.class.new(value.truncate(ndigits) / 100)
|
99
|
+
end
|
80
100
|
end
|
81
101
|
|
82
102
|
def scale(n)
|
@@ -107,7 +127,7 @@ def Percentage(object)
|
|
107
127
|
end
|
108
128
|
|
109
129
|
def Percentage.change(a, b)
|
110
|
-
Percentage.new((b - a).to_r / a)
|
130
|
+
Percentage.new((b - a).to_r / a) unless a.zero?
|
111
131
|
end
|
112
132
|
|
113
133
|
class BigDecimal
|
data/percentage.gemspec
CHANGED
@@ -1,16 +1,20 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'percentage'
|
3
|
-
s.version = '
|
3
|
+
s.version = '2.0.0'
|
4
4
|
s.license = 'LGPL-3.0'
|
5
5
|
s.platform = Gem::Platform::RUBY
|
6
6
|
s.authors = ['Tim Craft']
|
7
7
|
s.email = ['mail@timcraft.com']
|
8
|
-
s.homepage = 'https://github.com/
|
9
|
-
s.description = '
|
8
|
+
s.homepage = 'https://github.com/readysteady/percentage'
|
9
|
+
s.description = 'Ruby gem for working with percentages'
|
10
10
|
s.summary = 'See description'
|
11
|
-
s.files = Dir.glob('
|
11
|
+
s.files = Dir.glob('lib/**/*.rb') + %w[CHANGES.md 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'
|
14
|
+
s.metadata = {
|
15
|
+
'homepage' => 'https://github.com/readysteady/percentage',
|
16
|
+
'source_code_uri' => 'https://github.com/readysteady/percentage',
|
17
|
+
'bug_tracker_uri' => 'https://github.com/readysteady/percentage/issues',
|
18
|
+
'changelog_uri' => 'https://github.com/readysteady/percentage/blob/main/CHANGES.md'
|
19
|
+
}
|
16
20
|
end
|
metadata
CHANGED
@@ -1,61 +1,37 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: percentage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tim Craft
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
|
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'
|
41
|
-
description: A little library for working with percentages
|
11
|
+
date: 2022-01-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Ruby gem for working with percentages
|
42
14
|
email:
|
43
15
|
- mail@timcraft.com
|
44
16
|
executables: []
|
45
17
|
extensions: []
|
46
18
|
extra_rdoc_files: []
|
47
19
|
files:
|
20
|
+
- CHANGES.md
|
48
21
|
- LICENSE.txt
|
49
22
|
- README.md
|
50
|
-
- Rakefile.rb
|
51
23
|
- lib/percentage.rb
|
24
|
+
- lib/percentage/yaml.rb
|
52
25
|
- percentage.gemspec
|
53
|
-
|
54
|
-
homepage: https://github.com/timcraft/percentage
|
26
|
+
homepage: https://github.com/readysteady/percentage
|
55
27
|
licenses:
|
56
28
|
- LGPL-3.0
|
57
|
-
metadata:
|
58
|
-
|
29
|
+
metadata:
|
30
|
+
homepage: https://github.com/readysteady/percentage
|
31
|
+
source_code_uri: https://github.com/readysteady/percentage
|
32
|
+
bug_tracker_uri: https://github.com/readysteady/percentage/issues
|
33
|
+
changelog_uri: https://github.com/readysteady/percentage/blob/main/CHANGES.md
|
34
|
+
post_install_message:
|
59
35
|
rdoc_options: []
|
60
36
|
require_paths:
|
61
37
|
- lib
|
@@ -70,8 +46,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
46
|
- !ruby/object:Gem::Version
|
71
47
|
version: '0'
|
72
48
|
requirements: []
|
73
|
-
rubygems_version: 3.
|
74
|
-
signing_key:
|
49
|
+
rubygems_version: 3.3.3
|
50
|
+
signing_key:
|
75
51
|
specification_version: 4
|
76
52
|
summary: See description
|
77
53
|
test_files: []
|
data/Rakefile.rb
DELETED
data/spec/percentage_spec.rb
DELETED
@@ -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
|