cash 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/cash.gemspec +1 -1
  2. data/lib/cash.rb +21 -2
  3. data/spec/lib/cash_spec.rb +42 -7
  4. metadata +4 -4
data/cash.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "cash"
7
- gem.version = '0.1.1'
7
+ gem.version = '0.2.0'
8
8
  gem.authors = ["Norbert Wojtowicz"]
9
9
  gem.email = ["wojtowicz.norbert@gmail.com"]
10
10
  gem.description = "Money model backed by BigDecimal"
data/lib/cash.rb CHANGED
@@ -33,12 +33,23 @@ class Cash
33
33
  fail ArgumentError, "invalid value for StrictDecimal(): #{arg.inspect}"
34
34
  end
35
35
 
36
- def to_s
36
+ def pretty_print
37
37
  Format.display(self)
38
38
  end
39
39
 
40
+ def to_s
41
+ "#{amount_string} #{currency.code}"
42
+ end
43
+
40
44
  def inspect
41
- "<Cash #{amount_string} #{currency.code}>"
45
+ "<Cash #{to_s}>"
46
+ end
47
+
48
+ def to_h
49
+ {
50
+ amount: amount_string,
51
+ currency: currency.code
52
+ }
42
53
  end
43
54
 
44
55
  def amount_string
@@ -67,6 +78,14 @@ class Cash
67
78
  raise ArgumentError, "cannot multiply #{self.inspect} by #{factor}"
68
79
  end
69
80
 
81
+ def /(factor)
82
+ factor = StrictDecimal(factor)
83
+ Cash.new(amount / factor, currency)
84
+ rescue ArgumentError
85
+ raise ArgumentError, "cannot divide #{self.inspect} by #{factor}"
86
+ end
87
+
88
+
70
89
  protected
71
90
 
72
91
  def equality_state
@@ -70,23 +70,38 @@ describe Cash do
70
70
  end
71
71
 
72
72
  describe '#to_s' do
73
+ it 'displays amount and currency' do
74
+ Cash.new('100.3', :eur).to_s.should == '100.30 EUR'
75
+ Cash.new('9999', :usd).to_s.should == '9999.00 USD'
76
+ Cash.new('.123', :pln).to_s.should == '0.12 PLN'
77
+ end
78
+
79
+ it 'displays correct offset' do
80
+ Cash.new('123.4567', :jpy).to_s.should == '123 JPY'
81
+ Cash.new('123.4567', :twd).to_s.should == '123.5 TWD'
82
+ Cash.new('123.4567', :pln).to_s.should == '123.46 PLN'
83
+ Cash.new('123.4567', :bhd).to_s.should == '123.457 BHD'
84
+ end
85
+ end
86
+
87
+ describe '#pretty_print' do
73
88
  it 'displays Euro' do
74
- Cash.new('100.3', :eur).to_s.should == '€100.30'
89
+ Cash.new('100.3', :eur).pretty_print.should == '€100.30'
75
90
  end
76
91
 
77
92
  it 'displays Dollar' do
78
- Cash.new('9999', :usd).to_s.should == '$9999.00'
93
+ Cash.new('9999', :usd).pretty_print.should == '$9999.00'
79
94
  end
80
95
 
81
96
  it 'displays other currencies' do
82
- Cash.new('.123', :pln).to_s.should == '0.12 PLN'
97
+ Cash.new('.123', :pln).pretty_print.should == '0.12 PLN'
83
98
  end
84
99
 
85
100
  it 'displays correct offset' do
86
- Cash.new('123.4567', :jpy).to_s.should == '123 JPY'
87
- Cash.new('123.4567', :twd).to_s.should == '123.5 TWD'
88
- Cash.new('123.4567', :pln).to_s.should == '123.46 PLN'
89
- Cash.new('123.4567', :bhd).to_s.should == '123.457 BHD'
101
+ Cash.new('123.4567', :jpy).pretty_print.should == '123 JPY'
102
+ Cash.new('123.4567', :twd).pretty_print.should == '123.5 TWD'
103
+ Cash.new('123.4567', :pln).pretty_print.should == '123.46 PLN'
104
+ Cash.new('123.4567', :bhd).pretty_print.should == '123.457 BHD'
90
105
  end
91
106
  end
92
107
 
@@ -96,6 +111,15 @@ describe Cash do
96
111
  end
97
112
  end
98
113
 
114
+ describe '#to_h' do
115
+ it 'returns serialized hash' do
116
+ Cash.new('100.3', :eur).to_h.should == {
117
+ amount: '100.30',
118
+ currency: 'EUR'
119
+ }
120
+ end
121
+ end
122
+
99
123
  context 'comparable' do
100
124
  describe '#==' do
101
125
  it { Cash.new(1, :usd).should == Cash.new(1, :usd) }
@@ -168,6 +192,17 @@ describe Cash do
168
192
  expect{ one_usd * one_usd }.to raise_error(ArgumentError)
169
193
  end
170
194
  end
195
+
196
+ describe 'division' do
197
+ it 'divides amount by bigdecimal' do
198
+ split = one_usd / BigDecimal.new('4')
199
+ split.should == Cash.new('0.25', 'USD')
200
+ end
201
+
202
+ it 'fails if multiplying cash by cash' do
203
+ expect{ two_usd / one_usd }.to raise_error(ArgumentError)
204
+ end
205
+ end
171
206
  end
172
207
 
173
208
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-19 00:00:00.000000000 Z
12
+ date: 2013-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -79,7 +79,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
79
  version: '0'
80
80
  segments:
81
81
  - 0
82
- hash: 269915481050317062
82
+ hash: -1365261179025828843
83
83
  required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
88
  version: '0'
89
89
  segments:
90
90
  - 0
91
- hash: 269915481050317062
91
+ hash: -1365261179025828843
92
92
  requirements: []
93
93
  rubyforge_project:
94
94
  rubygems_version: 1.8.23