percentable 0.1.0 → 1.0.0

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: 0fbd045effde2a7949bd1f83593a92c8d320abf1
4
- data.tar.gz: 0f629c3a9f5afb113e17a62265d47320cb78ae4d
3
+ metadata.gz: d9096b94ae18a9333ed3c0c62c5e073c25cd0fdc
4
+ data.tar.gz: 147fe75ef459749f6ac2761ea89e27db2ad99412
5
5
  SHA512:
6
- metadata.gz: 21674843db344a6ae56bab761521e302194d7350c891ccc0ee15d567f36ab1902e4590fb6c86878d12130550aaade1f602f9c0883ca35fe66160c7a610bd56eb
7
- data.tar.gz: a9709db91180a7edb2498cbaee2a23d4999fdb0087d905ce9f236f2e1b6d6708f9765892ec8953f5b712d1013ea1add97a37be9afe9a9b48c4064542eb8de287
6
+ metadata.gz: eff8be4785754451bb00d48688e43674e114e09eb79ab568534db8134055a0ecae5e036c094f73cf9a686ebdd7112ea7754329ef3c3e246639adec174462135b
7
+ data.tar.gz: 28295c08b4d64035a69b91ed21e1afab836b274d9fd2a7f3ae904d31291c82353e52ee6fa5c02868e6d034d1e77fe7128c3372edbb69d32a5834a945355207fd
data/README.md CHANGED
@@ -68,7 +68,7 @@ percent = Percent.new(50)
68
68
 
69
69
  Repeat steps above for Floats, BigDecimals, etc. It should all work.
70
70
 
71
- ### Can I turn other Numerics into Percents?
71
+ ### Can I turn Numerics into Percents?
72
72
 
73
73
  Yes, yes you can.
74
74
 
@@ -76,6 +76,14 @@ Yes, yes you can.
76
76
  10.to_percent #=> Percent.new(10)
77
77
  ```
78
78
 
79
+ ### What if I want to turn my Numeric into what it would actually be as a percent?
80
+
81
+ Sometimes 10.to_percent is not what you want. That returns you `Percent.new(10)` which is equal only to 0.1. If you have 0.1 and want to turn that into a percent representation, use the code below:
82
+
83
+ ``` ruby
84
+ Percent.from_numeric(1) #=> Percent.new(100)
85
+ ```
86
+
79
87
  ### How do I use this with Rails?
80
88
 
81
89
  Well let's say you have a User model, and on that user model you have a health attribute. You want to set that to a number that represents the percent. Just use the extend Percentable::Percentize method, and then use the percentize method to make sure you are returned percent objects always. Example below:
@@ -112,7 +120,9 @@ percentize_object = PercentizedClass.new
112
120
  percentize_object.returns_a_percent #=> Percent.new(10)
113
121
  ```
114
122
 
115
- You can do percentize multiple methods at a time:
123
+ ### What else can percentize do?
124
+
125
+ You can percentize multiple methods at a time:
116
126
 
117
127
  ``` ruby
118
128
  class PercentizedClass < OriginalClass
@@ -122,7 +132,7 @@ class PercentizedClass < OriginalClass
122
132
  end
123
133
  ```
124
134
 
125
- You can define defaults for when the percentized method returns nil:
135
+ You can also define defaults for when the percentized method returns nil:
126
136
 
127
137
  ``` ruby
128
138
  class PercentizedClass < OriginalClass
@@ -1,4 +1,5 @@
1
1
  require_relative 'percentable/version'
2
2
  require_relative 'percentable/numeric'
3
+ require_relative 'percentable/applied_percent'
3
4
  require_relative 'percentable/percent'
4
5
  require_relative 'percentable/percentize'
@@ -0,0 +1,17 @@
1
+ class AppliedPercent < Struct.new(:percent)
2
+ def + other
3
+ other + self * other
4
+ end
5
+
6
+ def - other
7
+ other - self * other
8
+ end
9
+
10
+ def * other
11
+ other * percent.to_f
12
+ end
13
+
14
+ def / other
15
+ other / percent.to_f
16
+ end
17
+ end
@@ -1,7 +1,11 @@
1
1
  module Percentable
2
2
  class Percent < ::Numeric
3
3
  def initialize(value)
4
- @value = value.to_f
4
+ if value.is_a? Percent
5
+ @value = value.value
6
+ else
7
+ @value = value.to_f
8
+ end
5
9
  end
6
10
 
7
11
  def value
@@ -20,24 +24,6 @@ module Percentable
20
24
  value.to_i
21
25
  end
22
26
 
23
- def coerce other
24
- method = caller[0].match("`(.+)'")[1].to_sym
25
-
26
- case other
27
- when Numeric
28
- case method
29
- when :+
30
- [to_f * other, other]
31
- when :-
32
- [other, to_f * other]
33
- else
34
- [other, to_f]
35
- end
36
- else
37
- fail TypeError, "#{self.class} can't be coerced into #{other.class}"
38
- end
39
- end
40
-
41
27
  def == other
42
28
  (other.class == self.class && other.value == self.value) || other == self.to_f
43
29
  end
@@ -70,6 +56,10 @@ module Percentable
70
56
  end
71
57
  end
72
58
 
59
+ def to_percent
60
+ self
61
+ end
62
+
73
63
  def self.from_numeric(numeric)
74
64
  case numeric
75
65
  when Numeric
@@ -78,6 +68,15 @@ module Percentable
78
68
  fail TypeError, 'must inherit from Numeric'
79
69
  end
80
70
  end
71
+
72
+ def coerce other
73
+ case other
74
+ when Numeric
75
+ [AppliedPercent.new(self), other]
76
+ else
77
+ fail TypeError, "#{self.class} can't be coerced into #{other.class}"
78
+ end
79
+ end
81
80
  end
82
81
  end
83
82
 
@@ -1,3 +1,3 @@
1
1
  module Percentable
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -14,6 +14,15 @@ describe Percentable::Percent do
14
14
  expect(subject.value).to eq 50
15
15
  end
16
16
  end
17
+
18
+ context 'with percent value' do
19
+ let(:value) { percent }
20
+ let(:percent) { Percentable::Percent.new(50) }
21
+
22
+ it 'should return an equivalent percent' do
23
+ expect(subject).to eq percent
24
+ end
25
+ end
17
26
  end
18
27
 
19
28
  describe '#to_s' do
@@ -277,6 +286,14 @@ describe Percentable::Percent do
277
286
  end
278
287
  end
279
288
 
289
+ describe '#to_percent' do
290
+ let(:value) { 50 }
291
+
292
+ it 'should return self' do
293
+ expect(subject.to_percent).to eq subject
294
+ end
295
+ end
296
+
280
297
  describe '.from_numeric' do
281
298
  let(:n) { 0.5 }
282
299
 
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: 0.1.0
4
+ version: 1.0.0
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-07-30 00:00:00.000000000 Z
11
+ date: 2014-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -82,6 +82,7 @@ files:
82
82
  - README.md
83
83
  - Rakefile
84
84
  - lib/percentable.rb
85
+ - lib/percentable/applied_percent.rb
85
86
  - lib/percentable/numeric.rb
86
87
  - lib/percentable/percent.rb
87
88
  - lib/percentable/percentize.rb