mongoid_money 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jeff Bozek
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = mongoid_money
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to mongoid_money
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Jeff Bozek. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,24 @@
1
+ module Mongoid
2
+ module Extensions
3
+ module Money
4
+ module Conversions
5
+ def set(value)
6
+ return nil if value.blank?
7
+ begin
8
+ value.cents
9
+ rescue
10
+ value
11
+ end
12
+ end
13
+ def get(value)
14
+ return nil if value.blank?
15
+ begin
16
+ self.new_from_cents value
17
+ rescue
18
+ nil
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,180 @@
1
+ class Money < Numeric
2
+ extend Mongoid::Extensions::Money::Conversions
3
+ include Comparable
4
+
5
+ attr_reader :cents
6
+
7
+ def self.new_from_dollars(value)
8
+ case value
9
+ when Fixnum
10
+ Money.new(value * 100)
11
+ when BigDecimal
12
+ Money.new((value * 100).fix)
13
+ when Float
14
+ Money.new((BigDecimal.new(value.to_s) * 100).fix)
15
+ when Numeric
16
+ Money.new((BigDecimal.new(value.to_s) * 100).fix)
17
+ when String
18
+ Money.new((BigDecimal.new(value.to_s) * 100).fix)
19
+ else
20
+ raise ArgumentError, "#{value} must be a numeric object or a string representation of a number."
21
+ end
22
+ end
23
+
24
+ def self.new_from_cents(cents)
25
+ Money.new cents.round.to_i
26
+ end
27
+
28
+ def initialize(cents)
29
+ @cents = cents.round.to_i
30
+ end
31
+
32
+ def dollars
33
+ (BigDecimal.new(cents.to_s) / 100 ).to_f
34
+ end
35
+
36
+ def ==(other_money)
37
+ if other_money.respond_to?(:to_money)
38
+ other_money = other_money.to_money
39
+ cents == other_money.cents
40
+ else
41
+ false
42
+ end
43
+ end
44
+
45
+ def eql?(other_money)
46
+ self == other_money
47
+ end
48
+
49
+ def hash
50
+ cents.hash
51
+ end
52
+
53
+ def <=>(other_money)
54
+ if other_money.respond_to?(:to_money)
55
+ other_money = other_money.to_money
56
+ cents <=> other_money.cents
57
+ else
58
+ raise ArgumentError, "Comparison of #{self.class} with #{other_money.inspect} failed"
59
+ end
60
+ end
61
+
62
+ def +(other_money)
63
+ Money.new(cents + other_money.cents)
64
+ end
65
+
66
+ def -(other_money)
67
+ Money.new(cents - other_money.cents)
68
+ end
69
+
70
+ def *(value)
71
+ if value.is_a?(Money)
72
+ raise ArgumentError, "Can't multiply a Money by a Money"
73
+ else
74
+ Money.new(cents * value)
75
+ end
76
+ end
77
+
78
+ def /(value)
79
+ if value.is_a?(Money)
80
+ (cents / BigDecimal.new(value.cents.to_s)).to_f
81
+ else
82
+ Money.new(cents / value)
83
+ end
84
+ end
85
+
86
+ def div(value)
87
+ self / value
88
+ end
89
+
90
+ def divmod(val)
91
+ if val.is_a?(Money)
92
+ a = self.cents
93
+ b = val.cents
94
+ q, m = a.divmod(b)
95
+ return [q, Money.new(m)]
96
+ else
97
+ return [self.div(val), Money.new(self.cents.modulo(val))]
98
+ end
99
+ end
100
+
101
+ def modulo(val)
102
+ self.divmod(val)[1]
103
+ end
104
+
105
+ def %(val)
106
+ self.modulo(val)
107
+ end
108
+
109
+ def remainder(val)
110
+ a, b = self, val
111
+
112
+ a_sign, b_sign = :pos, :pos
113
+ a_sign = :neg if a.cents < 0
114
+ b_sign = :neg if (b.is_a?(Money) and b.cents < 0) or (b < 0)
115
+
116
+ return a.modulo(b) if a_sign == b_sign
117
+ a.modulo(b) - (b.is_a?(Money) ? b : Money.new(b))
118
+ end
119
+
120
+ def abs
121
+ Money.new(self.cents.abs)
122
+ end
123
+
124
+ def zero?
125
+ cents == 0
126
+ end
127
+
128
+ def nonzero?
129
+ cents != 0 ? self : nil
130
+ end
131
+
132
+ def to_money
133
+ self
134
+ end
135
+
136
+ def inspect
137
+ to_s
138
+ end
139
+
140
+ def odd?
141
+ @cents%2 > 0
142
+ end
143
+
144
+ def even?
145
+ @cents%2 == 0
146
+ end
147
+
148
+ def to_s
149
+ dollars.to_s
150
+ end
151
+
152
+ def to_f
153
+ dollars
154
+ end
155
+
156
+ def coerce(other)
157
+ return other, to_f
158
+ end
159
+
160
+ end
161
+
162
+ class Numeric
163
+
164
+ def dollar
165
+ dollars
166
+ end
167
+
168
+ def dollars
169
+ Money.new_from_dollars self
170
+ end
171
+
172
+ def cent
173
+ cents
174
+ end
175
+
176
+ def cents
177
+ Money.new_from_cents self
178
+ end
179
+
180
+ end
@@ -0,0 +1,2 @@
1
+ require 'mongoid_money/conversions'
2
+ require 'mongoid_money/money'
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Extensions::Money::Conversions do
4
+
5
+ describe 'when persisting a document with a Money datatype' do
6
+
7
+ it 'should be persisted normally when set as dollars' do
8
+ dummy = DummyMoney.new
9
+ dummy.price = 10.dollars
10
+ dummy.save.should eq true
11
+ end
12
+
13
+ it 'should be persisted normally when set as cents' do
14
+ dummy = DummyMoney.new
15
+ dummy.price = 99.cents
16
+ dummy.save.should eq true
17
+ end
18
+
19
+ end
20
+
21
+ describe 'when accessing a document from the datastore with a Money datatype' do
22
+
23
+ before(:each) do
24
+ DummyMoney.create(:description => "Test", :price => 9.dollars)
25
+ end
26
+
27
+ it 'should have a Money value that matches the money value that was initially persisted' do
28
+ dummy = DummyMoney.first
29
+ dummy.price.should eq 9.dollars
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ describe Money do
4
+
5
+ describe "#==" do
6
+ it 'should identify equal dollar values correctly' do
7
+ (10.dollars == Money.new_from_dollars(10)).should eq true
8
+ (10.dollars == 10.dollars).should eq true
9
+ end
10
+
11
+ it 'should identify equal monetary values correctly' do
12
+ (10.dollars == Money.new_from_cents(10 * 100)).should eq true
13
+ (10.dollars == 1000.cents).should eq true
14
+ end
15
+ end
16
+
17
+ describe "#eql?" do
18
+ it 'should identify equal dollar values correctly' do
19
+ (10.dollars.eql?(Money.new_from_dollars(10))).should eq true
20
+ (10.dollars.eql?(10.dollars)).should eq true
21
+ end
22
+
23
+ it 'should identify equal monetary values correctly' do
24
+ (10.dollars.eql?(Money.new_from_cents(10 * 100))).should eq true
25
+ (10.dollars.eql?(1000.cents)).should eq true
26
+ end
27
+ end
28
+
29
+ describe "#<=>" do
30
+ it 'should find that 10 dollars is greater than 5 dollars' do
31
+ (10.dollars <=> 5.dollars).should eq 1
32
+ end
33
+
34
+ it 'should find that 5 dollars is less than 10 dollars' do
35
+ (5.dollars <=> 10.dollars).should eq -1
36
+ end
37
+
38
+ it 'should raise an error when Money is compared to non-money' do
39
+ expect{ 5.dollars <=> 500}.to raise_error(ArgumentError, "Comparison of Money with 500 failed")
40
+ end
41
+ end
42
+
43
+ describe "#+" do
44
+ it 'should add 10 dollars and 5 dollars and get 15 dollars' do
45
+ (10.dollars + 5.dollars).should eq 15.dollars
46
+ end
47
+
48
+ it 'should add 10 dollars and 200 cents and get 12 dollars' do
49
+ (10.dollars + 200.cents).should eq 12.dollars
50
+ end
51
+
52
+ it 'should add 50 cents and 200 cents and get 250 cents' do
53
+ (50.cents + 200.cents).should eq 250.cents
54
+ end
55
+ end
56
+
57
+ describe "#-" do
58
+ it 'should subtract 5 dollars from 15 dollars and get 10 dollars' do
59
+ (15.dollars - 5.dollars).should eq 10.dollars
60
+ end
61
+
62
+ it 'should subtract 200 cents from 12 dollars and get 10 dollars' do
63
+ (12.dollars - 200.cents).should eq 10.dollars
64
+ end
65
+
66
+ it 'should subtract 50 cents from 200 cents and get 150 cents' do
67
+ (200.cents - 50.cents).should eq 150.cents
68
+ end
69
+ end
70
+
71
+ describe "#*" do
72
+ it 'should multiply 10 dollars by 3 and get 30 dollars' do
73
+ (10.dollars * 3).should eq 30.dollars
74
+ end
75
+
76
+ it 'should raise an error when Money is multiplied by money' do
77
+ expect{ 5.dollars * 5.dollars}.to raise_error(ArgumentError, "Can't multiply a Money by a Money")
78
+ end
79
+ end
80
+
81
+ describe "#/" do
82
+ it 'should divide 10 dollars by 2 and get 5 dollars' do
83
+ (10.dollars / 2).should eq 5.dollars
84
+ end
85
+
86
+ it 'should divide 10 dollars by 5 dollars and get 2' do
87
+ (10.dollars / 5.dollars).should eq 2
88
+ end
89
+ end
90
+
91
+ describe "#div" do
92
+ it 'should divide 10 dollars by 2 and get 5 dollars' do
93
+ (10.dollars.div(2)).should eq 5.dollars
94
+ end
95
+
96
+ it 'should divide 10 dollars by 5 dollars and get 2' do
97
+ (10.dollars.div(5.dollars)).should eq 2
98
+ end
99
+ end
100
+
101
+ describe "#abs" do
102
+ it 'should return the abs value of -10 dollars as 10 dollars' do
103
+ -10.dollars.abs.should eq 10.dollars
104
+ end
105
+
106
+ it 'should return the abs value of 10 dollars as 10 dollars' do
107
+ 10.dollars.abs.should eq 10.dollars
108
+ end
109
+ end
110
+
111
+ end
@@ -0,0 +1,32 @@
1
+ require 'mongoid'
2
+ require 'database_cleaner'
3
+ require 'mongoid_money'
4
+ require 'rubygems'
5
+
6
+ Dir["./spec/support/**/*.rb"].each {|f| require f}
7
+
8
+ RSpec.configure do |config|
9
+
10
+ Mongoid.configure do |mconfig|
11
+ name = "monogid_money_test"
12
+ host = "localhost"
13
+ port = 27017
14
+ mconfig.database = Mongo::Connection.new.db(name)
15
+ end
16
+
17
+ DatabaseCleaner.strategy = :truncation
18
+
19
+ config.before(:each) do
20
+ DatabaseCleaner.clean
21
+ end
22
+
23
+ # == Mock Framework
24
+ #
25
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
26
+ #
27
+ # config.mock_with :mocha
28
+ # config.mock_with :flexmock
29
+ # config.mock_with :rr
30
+ config.mock_with :rspec
31
+
32
+ end
@@ -0,0 +1,7 @@
1
+ class DummyMoney
2
+ include Mongoid::Document
3
+
4
+ field :description
5
+ field :price, :type => Money
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_money
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Jeff Bozek
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-22 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: bundler
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.0
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: jeweler
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 1.5.2
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: shoulda
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: mongo
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: "1.1"
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: mongo_ext
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ type: :development
80
+ prerelease: false
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: mongoid
84
+ requirement: &id007 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - "="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.0.0.rc.7
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *id007
93
+ - !ruby/object:Gem::Dependency
94
+ name: bson_ext
95
+ requirement: &id008 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ~>
99
+ - !ruby/object:Gem::Version
100
+ version: "1.2"
101
+ type: :development
102
+ prerelease: false
103
+ version_requirements: *id008
104
+ - !ruby/object:Gem::Dependency
105
+ name: database_cleaner
106
+ requirement: &id009 !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: "0"
112
+ type: :development
113
+ prerelease: false
114
+ version_requirements: *id009
115
+ - !ruby/object:Gem::Dependency
116
+ name: mongo
117
+ requirement: &id010 !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ~>
121
+ - !ruby/object:Gem::Version
122
+ version: "1.1"
123
+ type: :runtime
124
+ prerelease: false
125
+ version_requirements: *id010
126
+ - !ruby/object:Gem::Dependency
127
+ name: mongoid
128
+ requirement: &id011 !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - "="
132
+ - !ruby/object:Gem::Version
133
+ version: 2.0.0.rc.7
134
+ type: :runtime
135
+ prerelease: false
136
+ version_requirements: *id011
137
+ description: Money datatype for Mongoid. Inspired by Ruby Money. Makes handling money simple. Stores the value in the db as cents. Currently handles USD.
138
+ email: jeff.bozek@gmail.com
139
+ executables: []
140
+
141
+ extensions: []
142
+
143
+ extra_rdoc_files:
144
+ - LICENSE.txt
145
+ - README.rdoc
146
+ files:
147
+ - lib/mongoid_money.rb
148
+ - lib/mongoid_money/conversions.rb
149
+ - lib/mongoid_money/money.rb
150
+ - LICENSE.txt
151
+ - README.rdoc
152
+ - spec/conversion_spec.rb
153
+ - spec/money_spec.rb
154
+ - spec/spec_helper.rb
155
+ - spec/support/dummy_money.rb
156
+ has_rdoc: true
157
+ homepage: http://github.com/jeffbozek/mongoid_money
158
+ licenses:
159
+ - MIT
160
+ post_install_message:
161
+ rdoc_options: []
162
+
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ hash: -1985478583530141391
171
+ segments:
172
+ - 0
173
+ version: "0"
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ none: false
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: "0"
180
+ requirements: []
181
+
182
+ rubyforge_project:
183
+ rubygems_version: 1.5.3
184
+ signing_key:
185
+ specification_version: 3
186
+ summary: Money datatype for Mongoid. Inspired by Ruby Money.
187
+ test_files:
188
+ - spec/conversion_spec.rb
189
+ - spec/money_spec.rb
190
+ - spec/spec_helper.rb
191
+ - spec/support/dummy_money.rb