crazy_money 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/crazy_money.gemspec +1 -1
- data/lib/crazy_money.rb +16 -2
- data/spec/crazy_money_spec.rb +19 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 355b6399beb0a9ecf767ab69a53b5ebf82e3174a
|
4
|
+
data.tar.gz: 12dbfb30f74ef1d609cc79a4835a1b97d669e418
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14212e16b31e42057ea64ed9eeff5a5d08df665eceb8944a057229c4a005e591c15256a384c6a6e5b9b67e2fee4d16baebb85cd6e9082b4f3200c462e9aef78d
|
7
|
+
data.tar.gz: 07d4f15aa32375ff18ea64c84984682022b42e14458da30d847e64056e747bbdc998d3d0d9cf7a1bc6f4909b4f3f0e7e3f1b46cc2384f6fdd23b4636e156e03a
|
data/.rubocop.yml
CHANGED
data/crazy_money.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "crazy_money"
|
7
|
-
spec.version = "1.
|
7
|
+
spec.version = "1.4.0"
|
8
8
|
spec.authors = ["Cédric Félizard"]
|
9
9
|
spec.email = ["cedric@felizard.fr"]
|
10
10
|
spec.description = 'Simple money wrapper'
|
data/lib/crazy_money.rb
CHANGED
@@ -27,6 +27,20 @@ class CrazyMoney
|
|
27
27
|
sprintf("%.#{decimal_places}f", @amount)
|
28
28
|
end
|
29
29
|
|
30
|
+
def to_i
|
31
|
+
@amount.to_i
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_k
|
35
|
+
if @amount.abs < 1E3
|
36
|
+
amount, suffix = @amount, ""
|
37
|
+
else
|
38
|
+
amount, suffix = (@amount / 1E3), "k"
|
39
|
+
end
|
40
|
+
|
41
|
+
"#{amount.round.to_i}#{suffix}".freeze
|
42
|
+
end
|
43
|
+
|
30
44
|
def inspect
|
31
45
|
"#<CrazyMoney amount=#{self}>"
|
32
46
|
end
|
@@ -56,8 +70,8 @@ class CrazyMoney
|
|
56
70
|
self.class.new(self * -1)
|
57
71
|
end
|
58
72
|
|
59
|
-
def round
|
60
|
-
self.class.new(@amount.round)
|
73
|
+
def round(*args)
|
74
|
+
self.class.new(@amount.round(*args))
|
61
75
|
end
|
62
76
|
|
63
77
|
def cents(ratio = 100)
|
data/spec/crazy_money_spec.rb
CHANGED
@@ -17,6 +17,23 @@ RSpec.describe CrazyMoney do
|
|
17
17
|
specify { expect(CrazyMoney.new(one_third).to_s).to eq("0.33") }
|
18
18
|
end
|
19
19
|
|
20
|
+
describe "#to_i" do
|
21
|
+
specify { expect(CrazyMoney.new(0).to_i).to eq(0) }
|
22
|
+
specify { expect(CrazyMoney.new("13.37").to_i).to eq(13) }
|
23
|
+
specify { expect(CrazyMoney.new("13.99").to_i).to eq(13) }
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#to_k" do
|
27
|
+
specify { expect(CrazyMoney.new(0).to_k).to eq("0") }
|
28
|
+
specify { expect(CrazyMoney.new("13.37").to_k).to eq("13") }
|
29
|
+
specify { expect(CrazyMoney.new("13.99").to_k).to eq("14") }
|
30
|
+
specify { expect(CrazyMoney.new("1000").to_k).to eq("1k") }
|
31
|
+
specify { expect(CrazyMoney.new("1337").to_k).to eq("1k") }
|
32
|
+
specify { expect(CrazyMoney.new("1937").to_k).to eq("2k") }
|
33
|
+
specify { expect(CrazyMoney.new("-1937").to_k).to eq("-2k") }
|
34
|
+
specify { expect(CrazyMoney.new("1E5").to_k).to eq("100k") }
|
35
|
+
end
|
36
|
+
|
20
37
|
describe "#inspect" do
|
21
38
|
specify { expect(CrazyMoney.new(0).inspect).to eq("#<CrazyMoney amount=0.00>") }
|
22
39
|
specify { expect(CrazyMoney.new("-13.37").inspect).to eq("#<CrazyMoney amount=-13.37>") }
|
@@ -93,6 +110,8 @@ RSpec.describe CrazyMoney do
|
|
93
110
|
specify { expect(CrazyMoney.new(1.23).round).to eq(1) }
|
94
111
|
specify { expect(CrazyMoney.new(1.5).round).to eq(2) }
|
95
112
|
specify { expect(CrazyMoney.new(1.999999).round).to eq(2) }
|
113
|
+
specify { expect(CrazyMoney.new(1.2345).round(2)).to eq(1.23) }
|
114
|
+
specify { expect(CrazyMoney.new(1.2345).round(5)).to eq(1.2345) }
|
96
115
|
specify { expect(CrazyMoney.new(-20).round).to eq(-20) }
|
97
116
|
specify { expect(CrazyMoney.new(-20.1).round).to eq(-20) }
|
98
117
|
specify { expect(CrazyMoney.new(-20.5).round).to eq(-21) }
|