percentable 1.0.2 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/percentable/percent.rb +6 -9
- data/lib/percentable/version.rb +1 -1
- data/percentable.gemspec +1 -0
- data/spec/lib/percentable/percent_spec.rb +28 -20
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30af2373167d55dbd2f96944fd5bafe439dc14ca
|
4
|
+
data.tar.gz: 66c3fe0061956d3096a1cb28b516373312faa0fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8454c1a18c17be86d22f630396ac9098063d91503c5dea17fa496adfd11a28a9cc0b0d6071c4d979f2ecb0340ae3ae7c7268ac65aa5b08ab11b5199aa1aad7b
|
7
|
+
data.tar.gz: 21957a0eba7b4820487d65185c4c28bf485d8a55c0a4d0c7f13216e10679f3bf873afdf849c3e91d87024835900e1b5b8986ea5822257021b9d72275631bafd5
|
data/lib/percentable/percent.rb
CHANGED
@@ -37,11 +37,13 @@ module Percentable
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def * other
|
40
|
-
|
41
|
-
when Percent
|
40
|
+
if other.is_a? Percent
|
42
41
|
self.class.new(to_f * other.value)
|
42
|
+
elsif other.respond_to? :coerce
|
43
|
+
a, b = other.coerce(self)
|
44
|
+
a * b
|
43
45
|
else
|
44
|
-
|
46
|
+
raise TypeError, "#{other.class} can't be coerced into Percent"
|
45
47
|
end
|
46
48
|
end
|
47
49
|
|
@@ -70,12 +72,7 @@ module Percentable
|
|
70
72
|
end
|
71
73
|
|
72
74
|
def coerce other
|
73
|
-
|
74
|
-
when Numeric
|
75
|
-
[CoercedPercent.new(self), other]
|
76
|
-
else
|
77
|
-
fail TypeError, "#{self.class} can't be coerced into #{other.class}"
|
78
|
-
end
|
75
|
+
[CoercedPercent.new(self), other]
|
79
76
|
end
|
80
77
|
end
|
81
78
|
end
|
data/lib/percentable/version.rb
CHANGED
data/percentable.gemspec
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'bigdecimal'
|
3
3
|
require 'percentable'
|
4
|
+
require 'money'
|
4
5
|
|
5
6
|
describe Percentable::Percent do
|
6
7
|
subject { Percentable::Percent.new(value) }
|
@@ -92,14 +93,6 @@ describe Percentable::Percent do
|
|
92
93
|
end
|
93
94
|
|
94
95
|
describe '#coerce' do
|
95
|
-
context 'when other is non numeric' do
|
96
|
-
let(:other) { 'string' }
|
97
|
-
|
98
|
-
it 'should raise a TypeError' do
|
99
|
-
expect { subject.coerce(other) }.to raise_error TypeError
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
96
|
context 'math' do
|
104
97
|
let(:value) { 50 }
|
105
98
|
|
@@ -223,24 +216,39 @@ describe Percentable::Percent do
|
|
223
216
|
end
|
224
217
|
end
|
225
218
|
|
226
|
-
context 'multiplying
|
219
|
+
context 'multiplying something that responds to coerce' do
|
227
220
|
let(:other_thing) { double }
|
228
|
-
let(:
|
221
|
+
let(:value) { 20 }
|
229
222
|
|
230
|
-
|
231
|
-
|
223
|
+
before { allow(other_thing).to receive(:respond_to?).with(:to_f).and_return(false) }
|
224
|
+
before { allow(other_thing).to receive(:respond_to?).with(:coerce).and_return(true) }
|
232
225
|
|
233
|
-
|
234
|
-
|
235
|
-
|
226
|
+
it 'should call coerce on the other object' do
|
227
|
+
expect(other_thing).to receive(:coerce).with(subject).and_return([0, 1])
|
228
|
+
subject * other_thing
|
236
229
|
end
|
237
230
|
|
238
|
-
|
239
|
-
|
231
|
+
it 'should multiply the things that are returned from coerce' do
|
232
|
+
expect(other_thing).to receive(:coerce).with(subject).and_return([0, 1])
|
233
|
+
expect(subject * other_thing).to eq 0 * 1
|
234
|
+
end
|
235
|
+
end
|
240
236
|
|
241
|
-
|
242
|
-
|
243
|
-
|
237
|
+
context 'multiplying against Money' do
|
238
|
+
let(:value) { 50 }
|
239
|
+
let(:money) { Money.new(100) }
|
240
|
+
|
241
|
+
it "should return 50% of the money" do
|
242
|
+
expect(subject * money).to eq Money.new(50)
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
context "multiplying against an Integer" do
|
247
|
+
let(:value) { 50 }
|
248
|
+
let(:integer) { 10 }
|
249
|
+
|
250
|
+
it "should return 50% of the number" do
|
251
|
+
expect(subject * integer).to eq 5
|
244
252
|
end
|
245
253
|
end
|
246
254
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: percentable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric Roberts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: money
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: Small gem to make working with percents easier. Includes methods to make
|
70
84
|
selected rails attributes return percents.
|
71
85
|
email:
|