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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5f62e2dfea488d68900ce02255064e9867dbfe31
4
- data.tar.gz: 192c0d35012684af94024a3c38259506b250cf2f
3
+ metadata.gz: 30af2373167d55dbd2f96944fd5bafe439dc14ca
4
+ data.tar.gz: 66c3fe0061956d3096a1cb28b516373312faa0fb
5
5
  SHA512:
6
- metadata.gz: b3f3b813b2305202c9976d14756a5788cdfc8bdfee4ce5fe336cbfdeba791409c52f25409f49a1ac65ae772bf782a2a78625d1769a5fecf3d5a0d04b0d47ef91
7
- data.tar.gz: 71570d45df1f1c58d5701419546fed51bb5e8cf9c4f6deda17a08c8c500e0f0c89a0274fbe2d3d7e4fdb9d4dfdcda881d42d640a2bb0808383b3528bde7b70ab
6
+ metadata.gz: a8454c1a18c17be86d22f630396ac9098063d91503c5dea17fa496adfd11a28a9cc0b0d6071c4d979f2ecb0340ae3ae7c7268ac65aa5b08ab11b5199aa1aad7b
7
+ data.tar.gz: 21957a0eba7b4820487d65185c4c28bf485d8a55c0a4d0c7f13216e10679f3bf873afdf849c3e91d87024835900e1b5b8986ea5822257021b9d72275631bafd5
@@ -37,11 +37,13 @@ module Percentable
37
37
  end
38
38
 
39
39
  def * other
40
- case other
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
- self.class.new(value * other.to_f)
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
- case other
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
@@ -1,3 +1,3 @@
1
1
  module Percentable
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.2"
3
3
  end
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec", "~> 3.0"
24
24
  spec.add_development_dependency "coveralls"
25
+ spec.add_development_dependency "money"
25
26
  end
@@ -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 anything else' do
219
+ context 'multiplying something that responds to coerce' do
227
220
  let(:other_thing) { double }
228
- let(:percent) { subject.class.new(20) }
221
+ let(:value) { 20 }
229
222
 
230
- context 'responds to to_f' do
231
- before { allow(other_thing).to receive(:to_f).and_return 1.0 }
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
- it "should return the float value times the percent" do
234
- expect(percent * other_thing).to eq subject.class.new(20)
235
- end
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
- context 'does not respond to to_f' do
239
- before { allow(other_thing).to receive(:to_f).and_raise(NoMethodError) }
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
- it "should raise a no method error" do
242
- expect { percent * other_thing }.to raise_error(NoMethodError)
243
- end
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.0.2
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: 2014-12-16 00:00:00.000000000 Z
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: