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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d624827a45e617ad361779b9b350c48094aefa5d
4
- data.tar.gz: 447ce0a7248a10b6d40c908dfcda7d520423d10e
3
+ metadata.gz: 355b6399beb0a9ecf767ab69a53b5ebf82e3174a
4
+ data.tar.gz: 12dbfb30f74ef1d609cc79a4835a1b97d669e418
5
5
  SHA512:
6
- metadata.gz: e912a41ad5d4d0799eee8ae48207ecb84b626dab88795477b7e5dc2b79303c8163b33653376acc092fab020536cbb7dc57df11ea21615c2edc412f2b8320d8d3
7
- data.tar.gz: ae320ed84d7b709806bd456823c678ec8f86ee635756b27b4a33058ec63ce7729632f2c8d188a195780e6427ae5b2a8f35c9b3175aa462c70a96a51921172e2b
6
+ metadata.gz: 14212e16b31e42057ea64ed9eeff5a5d08df665eceb8944a057229c4a005e591c15256a384c6a6e5b9b67e2fee4d16baebb85cd6e9082b4f3200c462e9aef78d
7
+ data.tar.gz: 07d4f15aa32375ff18ea64c84984682022b42e14458da30d847e64056e747bbdc998d3d0d9cf7a1bc6f4909b4f3f0e7e3f1b46cc2384f6fdd23b4636e156e03a
@@ -37,3 +37,6 @@ Style/Documentation:
37
37
 
38
38
  Style/FormatString:
39
39
  Enabled: false
40
+
41
+ Performance/ParallelAssignment:
42
+ Enabled: false
@@ -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.3.0"
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'
@@ -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)
@@ -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) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crazy_money
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cédric Félizard