money-mongoid 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +21 -0
- data/Gemfile.lock +59 -0
- data/LICENSE.txt +20 -0
- data/README.md +39 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/money-mongoid.rb +1 -0
- data/lib/money/mongoid.rb +16 -0
- data/lib/money/mongoid/2x/money.rb +22 -0
- data/lib/money/mongoid/3x/money.rb +38 -0
- data/lib/money/mongoid/core_ext.rb +2 -0
- data/lib/money/mongoid/core_ext/array.rb +5 -0
- data/lib/money/mongoid/core_ext/string.rb +6 -0
- data/lib/money/mongoid/macros.rb +29 -0
- data/lib/money/mongoid/monetizable.rb +17 -0
- data/lib/money/mongoid/monetize.rb +35 -0
- data/lib/money/orm/generic.rb +24 -0
- data/spec/money/mongoid/2x/money_spec.rb +14 -0
- data/spec/money/mongoid/2x/product.rb +6 -0
- data/spec/money/mongoid/3x/money_spec.rb +49 -0
- data/spec/money/mongoid/3x/product.rb +10 -0
- data/spec/money/mongoid/models/account.rb +12 -0
- data/spec/money/mongoid/models/price.rb +15 -0
- data/spec/money/mongoid/monetize_spec.rb +87 -0
- data/spec/money/mongoid/spec_helper.rb +31 -0
- data/spec/money/mongoid/support/mongoid.yml +19 -0
- data/spec/money/mongoid/version_setup.rb +37 -0
- data/spec/spec_helper.rb +4 -0
- metadata +208 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
gem 'money'
|
4
|
+
|
5
|
+
gem 'mongoid', "~> 3.0.0.rc"
|
6
|
+
gem 'moped'
|
7
|
+
|
8
|
+
# gem 'mongoid', "~> 2.4"
|
9
|
+
# gem 'bson'
|
10
|
+
|
11
|
+
group :development do
|
12
|
+
gem "rspec", "~> 2.10"
|
13
|
+
gem "rdoc", ">= 3.12"
|
14
|
+
gem "bundler", "~> 1.1.0"
|
15
|
+
gem "jeweler", ">= 1.8.3"
|
16
|
+
gem "simplecov",">= 0.5"
|
17
|
+
end
|
18
|
+
|
19
|
+
group :test do
|
20
|
+
# gem 'bson_ext'
|
21
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.2.6)
|
5
|
+
activesupport (= 3.2.6)
|
6
|
+
builder (~> 3.0.0)
|
7
|
+
activesupport (3.2.6)
|
8
|
+
i18n (~> 0.6)
|
9
|
+
multi_json (~> 1.0)
|
10
|
+
builder (3.0.0)
|
11
|
+
diff-lcs (1.1.3)
|
12
|
+
git (1.2.5)
|
13
|
+
i18n (0.6.0)
|
14
|
+
jeweler (1.8.3)
|
15
|
+
bundler (~> 1.0)
|
16
|
+
git (>= 1.2.5)
|
17
|
+
rake
|
18
|
+
rdoc
|
19
|
+
json (1.7.3)
|
20
|
+
money (5.0.0)
|
21
|
+
i18n (~> 0.4)
|
22
|
+
json
|
23
|
+
mongoid (3.0.0.rc)
|
24
|
+
activemodel (~> 3.1)
|
25
|
+
moped (~> 1.0.0.rc)
|
26
|
+
origin (~> 1.0.0.rc)
|
27
|
+
tzinfo (~> 0.3.22)
|
28
|
+
moped (1.0.0.rc)
|
29
|
+
multi_json (1.3.6)
|
30
|
+
origin (1.0.1)
|
31
|
+
rake (0.9.2.2)
|
32
|
+
rdoc (3.12)
|
33
|
+
json (~> 1.4)
|
34
|
+
rspec (2.10.0)
|
35
|
+
rspec-core (~> 2.10.0)
|
36
|
+
rspec-expectations (~> 2.10.0)
|
37
|
+
rspec-mocks (~> 2.10.0)
|
38
|
+
rspec-core (2.10.1)
|
39
|
+
rspec-expectations (2.10.0)
|
40
|
+
diff-lcs (~> 1.1.3)
|
41
|
+
rspec-mocks (2.10.1)
|
42
|
+
simplecov (0.6.4)
|
43
|
+
multi_json (~> 1.0)
|
44
|
+
simplecov-html (~> 0.5.3)
|
45
|
+
simplecov-html (0.5.3)
|
46
|
+
tzinfo (0.3.33)
|
47
|
+
|
48
|
+
PLATFORMS
|
49
|
+
ruby
|
50
|
+
|
51
|
+
DEPENDENCIES
|
52
|
+
bundler (~> 1.1.0)
|
53
|
+
jeweler (>= 1.8.3)
|
54
|
+
money
|
55
|
+
mongoid (~> 3.0.0.rc)
|
56
|
+
moped
|
57
|
+
rdoc (>= 3.12)
|
58
|
+
rspec (~> 2.10)
|
59
|
+
simplecov (>= 0.5)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Kristian Mandrup
|
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.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Money for mongoid
|
2
|
+
|
3
|
+
Use the [money](https://github.com/RubyMoney/money) gem with Mongoid 2.x and 3.x.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
in Gemfile
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'money-mongoid'
|
11
|
+
```
|
12
|
+
|
13
|
+
Bundle via Terminal:
|
14
|
+
|
15
|
+
`$ bundle`
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'money-mongoid'
|
21
|
+
``
|
22
|
+
|
23
|
+
See specs for usage examples, fx `money/mongoid/3x/money_spec.rb`
|
24
|
+
|
25
|
+
## Contributing to money-mongoid
|
26
|
+
|
27
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
28
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
29
|
+
* Fork the project.
|
30
|
+
* Start a feature/bugfix branch.
|
31
|
+
* Commit and push until you are happy with your contribution.
|
32
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
33
|
+
* 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.
|
34
|
+
|
35
|
+
== Copyright
|
36
|
+
|
37
|
+
Copyright (c) 2012 Kristian Mandrup. See LICENSE.txt for
|
38
|
+
further details.
|
39
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "money-mongoid"
|
18
|
+
gem.homepage = "http://github.com/kristianmandrup/money-mongoid"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Mongoid support and integration for the money gem}
|
21
|
+
gem.description = %Q{Makes it easy to use money with mongoid}
|
22
|
+
gem.email = "kmandrup@gmail.com"
|
23
|
+
gem.authors = ["Kristian Mandrup"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rdoc/task'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "money-mongoid #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'money/mongoid'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'money/mongoid/core_ext'
|
2
|
+
require 'money/orm/generic'
|
3
|
+
|
4
|
+
if Mongoid::VERSION > '3'
|
5
|
+
require "money/mongoid/3x/money"
|
6
|
+
else
|
7
|
+
require "money/mongoid/2x/money"
|
8
|
+
end
|
9
|
+
|
10
|
+
module Mongoid
|
11
|
+
module Money
|
12
|
+
class << self
|
13
|
+
attr_accessor :default_polymorphic_money
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# extend Mongoid::Fields in Mongoid document (model) that wants to use Money
|
2
|
+
# this makes Money accessible as a type
|
3
|
+
|
4
|
+
module Mongoid
|
5
|
+
module Fields
|
6
|
+
class Money
|
7
|
+
include Mongoid::Fields::Serializable
|
8
|
+
|
9
|
+
def self.instantiate(name, options = {})
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
def deserialize(value)
|
14
|
+
::Money.new value[:cents], value[:currency_iso]
|
15
|
+
end
|
16
|
+
|
17
|
+
def serialize(value)
|
18
|
+
{:cents => value.cents, :currency_iso => value.currency.iso_code}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Mongoize
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
# See http://mongoid.org/en/mongoid/docs/upgrading.html
|
4
|
+
|
5
|
+
def mongoize
|
6
|
+
{'cents' => cents, 'currency_iso' => currency.iso_code}
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def demongoize(value)
|
11
|
+
::Money.new get_cents(value), get_currency(value)
|
12
|
+
end
|
13
|
+
|
14
|
+
def evolve(object)
|
15
|
+
object.__evolve_to_money__.mongoize
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def get_cents value
|
21
|
+
value[:cents] || value['cents']
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_currency value
|
25
|
+
value[:currency_iso] || value['currency_iso']
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
class Money
|
33
|
+
include Mongoize
|
34
|
+
|
35
|
+
def __evolve_to_money__
|
36
|
+
self
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Money
|
3
|
+
module Macros
|
4
|
+
def self.included(base)
|
5
|
+
base.send :include, ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def monetize
|
10
|
+
self.send :include, "Mongoid::Monetize".constantize
|
11
|
+
end
|
12
|
+
|
13
|
+
def monetizable options = {}
|
14
|
+
self.send :include, "Mongoid::Monetizable".constantize
|
15
|
+
embedded_in options[:as], :polymorphic => true if options[:as]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module Mongoid
|
23
|
+
module Document
|
24
|
+
module ClassMethods
|
25
|
+
include Money::Mongoid::Macros
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Thanks to: https://gist.github.com/840500
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module Monetizable
|
5
|
+
include Money::Orm::Generic
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.class_eval do
|
9
|
+
field :cents, :type => Integer, :default => 0
|
10
|
+
field :currency_iso, :type => String, :default => ::Money.default_currency.iso_code
|
11
|
+
|
12
|
+
validates_numericality_of :cents
|
13
|
+
end
|
14
|
+
base.extend Money::Orm::Generic::ClassMethods
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Monetize
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
base.extend Money::Orm::Generic::ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def monetize_one name, options = {:as => :priced}
|
10
|
+
as_poly = options.delete(:as) || default_polymorphic_money
|
11
|
+
|
12
|
+
unless as_poly
|
13
|
+
raise ArgumentError, "You must set an :as option indicating the polymorphic Monetizable model. See money-rails gem."
|
14
|
+
end
|
15
|
+
|
16
|
+
self.embeds_one name.to_sym, options.merge(:as => as_poly)
|
17
|
+
end
|
18
|
+
alias_method :monetize, :monetize_one
|
19
|
+
|
20
|
+
def monetize_many name, options = {:as => :priced}
|
21
|
+
as_poly = options.delete(:as) || default_polymorphic_money
|
22
|
+
unless as_poly
|
23
|
+
raise ArgumentError, "You must set an :as option indicating the polymorphic Monetizable model. See money-rails gem."
|
24
|
+
end
|
25
|
+
self.embeds_many name, options.merge(:as => as_poly)
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def default_polymorphic_money
|
31
|
+
Mongoid::Money.default_polymorphic_money
|
32
|
+
end
|
33
|
+
end # module
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Money
|
2
|
+
module Orm
|
3
|
+
module Generic
|
4
|
+
# Virtual price / currency attributes
|
5
|
+
module ClassMethods
|
6
|
+
def monetize_for *names
|
7
|
+
options = names.last.kind_of?(Hash) ? names.delete(names.last) : {:as => :priced}
|
8
|
+
names.each {|name| monetize name, options }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def price
|
13
|
+
::Money.new(self.price_pence, currency)
|
14
|
+
end
|
15
|
+
def price=(price)
|
16
|
+
self.price_pence = price.cents
|
17
|
+
end
|
18
|
+
|
19
|
+
def currency
|
20
|
+
::Money::Currency.new(currency_iso)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'money/mongoid/spec_helper'
|
2
|
+
require 'money/mongoid/money'
|
3
|
+
require 'money/mongoid/2x/product'
|
4
|
+
|
5
|
+
describe 'Mongoid custom Money type' do
|
6
|
+
subject { product }
|
7
|
+
|
8
|
+
let(:product) do { Product.create :price => Money.new(3000, 'USD') }
|
9
|
+
|
10
|
+
its(:price) { should be_a Money }
|
11
|
+
|
12
|
+
specify { subject.price.cents.should == 3000 }
|
13
|
+
specify { subject.price.currency.iso_code.should == 'USD' }
|
14
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'money/mongoid/spec_helper'
|
2
|
+
require 'money/mongoid'
|
3
|
+
require 'money/mongoid/3x/product'
|
4
|
+
|
5
|
+
describe 'Mongoid custom Money type' do
|
6
|
+
subject { product }
|
7
|
+
|
8
|
+
let(:product) { Product.create(:price => Money.new(3000, 'USD')) }
|
9
|
+
let(:products) do
|
10
|
+
end
|
11
|
+
|
12
|
+
its(:price) { should be_a Money }
|
13
|
+
|
14
|
+
specify { subject.price.cents.should == 3000 }
|
15
|
+
specify { subject.price.currency.iso_code.should == 'USD' }
|
16
|
+
|
17
|
+
it "should be searchable by price as money" do
|
18
|
+
Product.create(:price => Money.new(3000, 'USD'))
|
19
|
+
Product.where(price: Money.new(3000, 'USD')).count.should == 1
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be searchable by price as string" do
|
23
|
+
product
|
24
|
+
Product.where(price: "USD 30.00").count.should == 1
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should be searchable by price using gte and a money value of same currency" do
|
28
|
+
product
|
29
|
+
Product.where(:price.gte => Money.new(2000, 'USD')).count.should == 1
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should be searchable by price using gte and a money value of different currency" do
|
33
|
+
Money.add_rate("USD","EUR", 0.5)
|
34
|
+
Money.add_rate("EUR","USD", 2)
|
35
|
+
|
36
|
+
6.times do |n|
|
37
|
+
Product.create :price => Money.new(n * 500, 'USD')
|
38
|
+
end
|
39
|
+
|
40
|
+
# So far only works by explicit conversion before search. Any solution?
|
41
|
+
Product.all.exchange_to! 'EUR'
|
42
|
+
|
43
|
+
# puts Product.all.map{|p| p.price.inspect }
|
44
|
+
|
45
|
+
# with our rates 20 EUR -> 40 USD
|
46
|
+
search_res = Product.where :price.gte => Money.new(2000, 'EUR')
|
47
|
+
search_res.count.should == 0
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Price
|
2
|
+
include Mongoid::Document
|
3
|
+
|
4
|
+
monetizable_orm :mongoid, :as => :priced
|
5
|
+
|
6
|
+
# embedded_in :priced, :polymorphic => true
|
7
|
+
|
8
|
+
def self.for amount, currency = nil
|
9
|
+
currency = ::Money::Currency.new(currency || ::Money.default_currency)
|
10
|
+
money = Money.new(amount, currency)
|
11
|
+
|
12
|
+
# see 'kristianmandrup' fork of money (money/macros)
|
13
|
+
Money::Macros.price_class.new :price => money
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'money/mongoid/spec_helper'
|
2
|
+
|
3
|
+
describe Money::Mongoid::Monetizable do
|
4
|
+
before do
|
5
|
+
Money.default_currency = 'USD'
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "monetize" do
|
9
|
+
subject { account }
|
10
|
+
|
11
|
+
let(:account) do
|
12
|
+
Account.create :rental_price => Price.for(3000, :usd),
|
13
|
+
:deposit => Price.for(150, :usd),
|
14
|
+
:rent => Price.for(100),
|
15
|
+
:utility => priced_at(200),
|
16
|
+
:services => priced_at(150)
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
it_behaves_like 'a generic account'
|
21
|
+
|
22
|
+
context 'can add multiple costs' do
|
23
|
+
before do
|
24
|
+
account.costs << Price.for(100, :usd)
|
25
|
+
account.costs << Price.for(200)
|
26
|
+
end
|
27
|
+
|
28
|
+
its(:costs) { should have(2).items }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'money macros' do
|
32
|
+
subject { account_2 }
|
33
|
+
|
34
|
+
let(:account_2) do
|
35
|
+
Account.create :rental_price => priced_at(3000, :usd),
|
36
|
+
:deposit => priced_at(150, :usd),
|
37
|
+
:rent => priced_at(100)
|
38
|
+
end
|
39
|
+
|
40
|
+
it_behaves_like 'a generic account'
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'customize macro map' do
|
44
|
+
context 'set cost_class to Price' do
|
45
|
+
subject { cost_account }
|
46
|
+
|
47
|
+
before do
|
48
|
+
MoneyRails::Moneys.cost_class = Price
|
49
|
+
end
|
50
|
+
|
51
|
+
let(:cost_account) do
|
52
|
+
Account.create :rental_price => costing(3000, :usd)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "rental is money" do
|
56
|
+
subject.rental_price.price.should == Money.new(3000)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "price compare < Numeric" do
|
60
|
+
subject.rental_price.price.should < 3100
|
61
|
+
end
|
62
|
+
|
63
|
+
it "price compare == Numeric" do
|
64
|
+
subject.rental_price.price.should be_eql(3000)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "price compare > Numeric" do
|
68
|
+
subject.rental_price.price.should_not > 3001
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'set all money classes to Price' do
|
73
|
+
subject { valued_at(4000) }
|
74
|
+
|
75
|
+
before do
|
76
|
+
MoneyRails::Moneys.classes = Price
|
77
|
+
end
|
78
|
+
|
79
|
+
specify { MoneyRails::Moneys.value_class.should == Price }
|
80
|
+
|
81
|
+
specify { subject.should be_a Price }
|
82
|
+
specify { subject.price.should be_a Money }
|
83
|
+
specify { subject.price.should == Money.new(4000) }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'mongoid'
|
3
|
+
require 'money'
|
4
|
+
|
5
|
+
require 'money/mongoid/version_setup'
|
6
|
+
|
7
|
+
Mongoid.configure do |config|
|
8
|
+
Mongoid::VersionSetup.configure config
|
9
|
+
end
|
10
|
+
|
11
|
+
if RUBY_VERSION >= '1.9.2'
|
12
|
+
YAML::ENGINE.yamler = 'syck'
|
13
|
+
end
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
# config.mock_with(:mocha)
|
17
|
+
|
18
|
+
config.before(:each) do
|
19
|
+
Mongoid.purge!
|
20
|
+
# Mongoid.database.collections.each do |collection|
|
21
|
+
# unless collection.name =~ /^system\./
|
22
|
+
# collection.remove
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# require 'money/mongoid/models/price'
|
29
|
+
# require 'money/mongoid/models/account'
|
30
|
+
|
31
|
+
# require 'money/shared/generic_account_ex'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
test:
|
2
|
+
database: money_rails_test
|
3
|
+
host: localhost
|
4
|
+
port: 27017
|
5
|
+
# slaves:
|
6
|
+
# - host: localhost
|
7
|
+
# port: 27018
|
8
|
+
# - host: localhost
|
9
|
+
# port: 27019
|
10
|
+
# allow_dynamic_fields: false
|
11
|
+
# include_root_in_json: true
|
12
|
+
# parameterize_keys: false
|
13
|
+
# persist_in_safe_mode: false
|
14
|
+
# raise_not_found_error: false
|
15
|
+
# reconnect_time: 5
|
16
|
+
# autocreate_indexes: false
|
17
|
+
# persist_types: false
|
18
|
+
# option_no_exist: false
|
19
|
+
# skip_version_check: false
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module VersionSetup
|
3
|
+
def self.configure config
|
4
|
+
version = Mongoid::VERSION
|
5
|
+
case
|
6
|
+
when version < '2'
|
7
|
+
raise ArgumentError, "Mongoid versions < 2 not supported"
|
8
|
+
when version < '3'
|
9
|
+
version_2 config
|
10
|
+
when version > '3'
|
11
|
+
version_3 config
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.version_3 config
|
16
|
+
require 'moped'
|
17
|
+
config.connect_to('money_mongoid_test')
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.version_2 config
|
21
|
+
require 'bson'
|
22
|
+
|
23
|
+
opts = YAML.load(File.read(File.dirname(__FILE__) + '/support/mongoid.yml'))["test"]
|
24
|
+
opts.delete("slaves") # no slaves support for version < 3
|
25
|
+
name = opts.delete("database")
|
26
|
+
host = opts.delete("host")
|
27
|
+
port = opts.delete("port")
|
28
|
+
config.master = begin
|
29
|
+
Mongo::Connection.new(host, port, opts).db(name)
|
30
|
+
rescue Mongo::ConnectionFailure
|
31
|
+
Mongo::Connection.new(host, '27017', opts).db(name)
|
32
|
+
end
|
33
|
+
config.logger = nil
|
34
|
+
config.allow_dynamic_fields = true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,208 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: money-mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kristian Mandrup
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: money
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: mongoid
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.0.0.rc
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.0.0.rc
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: moped
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.10'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.10'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rdoc
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '3.12'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '3.12'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: bundler
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.1.0
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 1.1.0
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: jeweler
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.8.3
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 1.8.3
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: simplecov
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0.5'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0.5'
|
142
|
+
description: Makes it easy to use money with mongoid
|
143
|
+
email: kmandrup@gmail.com
|
144
|
+
executables: []
|
145
|
+
extensions: []
|
146
|
+
extra_rdoc_files:
|
147
|
+
- LICENSE.txt
|
148
|
+
- README.md
|
149
|
+
files:
|
150
|
+
- .document
|
151
|
+
- .rspec
|
152
|
+
- Gemfile
|
153
|
+
- Gemfile.lock
|
154
|
+
- LICENSE.txt
|
155
|
+
- README.md
|
156
|
+
- Rakefile
|
157
|
+
- VERSION
|
158
|
+
- lib/money-mongoid.rb
|
159
|
+
- lib/money/mongoid.rb
|
160
|
+
- lib/money/mongoid/2x/money.rb
|
161
|
+
- lib/money/mongoid/3x/money.rb
|
162
|
+
- lib/money/mongoid/core_ext.rb
|
163
|
+
- lib/money/mongoid/core_ext/array.rb
|
164
|
+
- lib/money/mongoid/core_ext/string.rb
|
165
|
+
- lib/money/mongoid/macros.rb
|
166
|
+
- lib/money/mongoid/monetizable.rb
|
167
|
+
- lib/money/mongoid/monetize.rb
|
168
|
+
- lib/money/orm/generic.rb
|
169
|
+
- spec/money/mongoid/2x/money_spec.rb
|
170
|
+
- spec/money/mongoid/2x/product.rb
|
171
|
+
- spec/money/mongoid/3x/money_spec.rb
|
172
|
+
- spec/money/mongoid/3x/product.rb
|
173
|
+
- spec/money/mongoid/models/account.rb
|
174
|
+
- spec/money/mongoid/models/price.rb
|
175
|
+
- spec/money/mongoid/monetize_spec.rb
|
176
|
+
- spec/money/mongoid/spec_helper.rb
|
177
|
+
- spec/money/mongoid/support/mongoid.yml
|
178
|
+
- spec/money/mongoid/version_setup.rb
|
179
|
+
- spec/spec_helper.rb
|
180
|
+
homepage: http://github.com/kristianmandrup/money-mongoid
|
181
|
+
licenses:
|
182
|
+
- MIT
|
183
|
+
post_install_message:
|
184
|
+
rdoc_options: []
|
185
|
+
require_paths:
|
186
|
+
- lib
|
187
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
188
|
+
none: false
|
189
|
+
requirements:
|
190
|
+
- - ! '>='
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: '0'
|
193
|
+
segments:
|
194
|
+
- 0
|
195
|
+
hash: -620732319928410117
|
196
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
197
|
+
none: false
|
198
|
+
requirements:
|
199
|
+
- - ! '>='
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '0'
|
202
|
+
requirements: []
|
203
|
+
rubyforge_project:
|
204
|
+
rubygems_version: 1.8.24
|
205
|
+
signing_key:
|
206
|
+
specification_version: 3
|
207
|
+
summary: Mongoid support and integration for the money gem
|
208
|
+
test_files: []
|