money-mongoid 0.1.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +8 -1
- data/Gemfile.lock +15 -11
- data/README.md +2 -0
- data/VERSION +1 -1
- data/lib/money/mongoid/3x/money.rb +63 -3
- data/lib/money/mongoid/3x/origin/selectable.rb +66 -0
- data/lib/money/mongoid/core_ext/range.rb +43 -0
- data/lib/money/mongoid/core_ext.rb +1 -0
- data/lib/money/mongoid/macros.rb +1 -1
- data/lib/money/mongoid/monetizable.rb +2 -2
- data/lib/money/mongoid/monetize.rb +3 -3
- data/lib/money/mongoid.rb +60 -1
- data/lib/money/orm/generic.rb +5 -2
- data/lib/money-mongoid.rb +1 -0
- data/money-mongoid.gemspec +18 -5
- data/spec/money/mongoid/3x/money_spec.rb +44 -6
- data/spec/money/mongoid/3x/product.rb +1 -1
- data/spec/money/mongoid/models/account.rb +2 -2
- data/spec/money/mongoid/models/price.rb +3 -5
- data/spec/money/mongoid/models.rb +2 -0
- data/spec/money/mongoid/monetize_spec.rb +5 -3
- data/spec/money/mongoid/shared_ex.rb +13 -0
- data/spec/money/mongoid/spec_helper.rb +2 -0
- data/spec/spec_helper.rb +1 -0
- metadata +57 -5
data/Gemfile
CHANGED
@@ -1,12 +1,19 @@
|
|
1
1
|
source :rubygems
|
2
2
|
|
3
3
|
gem 'money'
|
4
|
-
gem 'mongoid', "
|
4
|
+
gem 'mongoid', "~> 3.0.0"
|
5
|
+
gem 'origin'
|
6
|
+
gem 'moped'
|
7
|
+
gem 'origin-selectable_ext'
|
8
|
+
|
9
|
+
# gem 'mongoid', "~> 2.4"
|
10
|
+
# gem 'bson'
|
5
11
|
|
6
12
|
group :development do
|
7
13
|
gem "rspec", ">= 2.10"
|
8
14
|
gem "rdoc", ">= 3.12"
|
9
15
|
gem "bundler", ">= 1.1.0"
|
16
|
+
|
10
17
|
gem "jeweler", ">= 1.8.4"
|
11
18
|
gem "simplecov",">= 0.5"
|
12
19
|
end
|
data/Gemfile.lock
CHANGED
@@ -7,13 +7,13 @@ GEM
|
|
7
7
|
activesupport (3.2.8)
|
8
8
|
i18n (~> 0.6)
|
9
9
|
multi_json (~> 1.0)
|
10
|
-
bson (1.
|
11
|
-
bson_ext (1.
|
12
|
-
bson (~> 1.
|
13
|
-
builder (3.0.
|
10
|
+
bson (1.7.0)
|
11
|
+
bson_ext (1.7.0)
|
12
|
+
bson (~> 1.7.0)
|
13
|
+
builder (3.0.3)
|
14
14
|
diff-lcs (1.1.3)
|
15
15
|
git (1.2.5)
|
16
|
-
i18n (0.6.
|
16
|
+
i18n (0.6.1)
|
17
17
|
jeweler (1.8.4)
|
18
18
|
bundler (~> 1.0)
|
19
19
|
git (>= 1.2.5)
|
@@ -23,14 +23,16 @@ GEM
|
|
23
23
|
money (5.0.0)
|
24
24
|
i18n (~> 0.4)
|
25
25
|
json
|
26
|
-
mongoid (3.0.
|
26
|
+
mongoid (3.0.6)
|
27
27
|
activemodel (~> 3.1)
|
28
28
|
moped (~> 1.1)
|
29
29
|
origin (~> 1.0)
|
30
30
|
tzinfo (~> 0.3.22)
|
31
|
-
moped (1.2.
|
31
|
+
moped (1.2.5)
|
32
32
|
multi_json (1.3.6)
|
33
|
-
origin (1.0.
|
33
|
+
origin (1.0.9)
|
34
|
+
origin-selectable_ext (0.1.0)
|
35
|
+
origin
|
34
36
|
rake (0.9.2.2)
|
35
37
|
rdoc (3.12)
|
36
38
|
json (~> 1.4)
|
@@ -39,9 +41,9 @@ GEM
|
|
39
41
|
rspec-expectations (~> 2.11.0)
|
40
42
|
rspec-mocks (~> 2.11.0)
|
41
43
|
rspec-core (2.11.1)
|
42
|
-
rspec-expectations (2.11.
|
44
|
+
rspec-expectations (2.11.3)
|
43
45
|
diff-lcs (~> 1.1.3)
|
44
|
-
rspec-mocks (2.11.
|
46
|
+
rspec-mocks (2.11.3)
|
45
47
|
simplecov (0.6.4)
|
46
48
|
multi_json (~> 1.0)
|
47
49
|
simplecov-html (~> 0.5.3)
|
@@ -56,8 +58,10 @@ DEPENDENCIES
|
|
56
58
|
bundler (>= 1.1.0)
|
57
59
|
jeweler (>= 1.8.4)
|
58
60
|
money
|
59
|
-
mongoid (
|
61
|
+
mongoid (~> 3.0.0)
|
60
62
|
moped
|
63
|
+
origin
|
64
|
+
origin-selectable_ext
|
61
65
|
rdoc (>= 3.12)
|
62
66
|
rspec (>= 2.10)
|
63
67
|
simplecov (>= 0.5)
|
data/README.md
CHANGED
@@ -20,6 +20,8 @@ Bundle via Terminal:
|
|
20
20
|
require 'money-mongoid'
|
21
21
|
``
|
22
22
|
|
23
|
+
Now also supports MoneyRange with between queries and can even do dynamic currency conversions as part of the query!!! ;)
|
24
|
+
|
23
25
|
See specs for usage examples, fx `money/mongoid/3x/money_spec.rb`
|
24
26
|
|
25
27
|
## Contributing to money-mongoid
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1
|
1
|
+
0.2.1
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'origin-selectable_ext'
|
2
|
+
|
1
3
|
module Mongoize
|
2
4
|
extend ActiveSupport::Concern
|
3
5
|
# See http://mongoid.org/en/mongoid/docs/upgrading.html
|
@@ -15,24 +17,82 @@ module Mongoize
|
|
15
17
|
object.__evolve_to_money__.mongoize
|
16
18
|
end
|
17
19
|
|
20
|
+
def custom_serialization?(operator)
|
21
|
+
return false unless operator
|
22
|
+
case operator
|
23
|
+
when "$gte", "$gt", "$lt", "$lte"
|
24
|
+
true
|
25
|
+
else
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def custom_specify(name, operator, value, options = {})
|
31
|
+
money = value.__evolve_to_money__
|
32
|
+
case operator
|
33
|
+
when "$gte", "$gt", "$lt", "$lte"
|
34
|
+
specify_with_multiple_currencies(name, operator, money, options)
|
35
|
+
else
|
36
|
+
raise RuntimeError, "Unsupported operator"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def custom_between(field, value, options = {})
|
41
|
+
{ "$gte" => ::Money.new(value.min, value.iso_code), "$lte" => ::Money.new(value.max, value.iso_code) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def custom_between? field, value
|
45
|
+
true
|
46
|
+
end
|
47
|
+
|
18
48
|
private
|
19
49
|
|
50
|
+
def specify_with_multiple_currencies(name, operator, value, options)
|
51
|
+
currencies = [value.currency.iso_code]
|
52
|
+
currencies.concat options[:compare_using] if options && options[:compare_using]
|
53
|
+
multiple_money_values = value.exchange_to_currencies(currencies.to_set)
|
54
|
+
subconditions = multiple_money_values.collect {|money| specify_with_single_currency(name, operator, money)}
|
55
|
+
if subconditions.count > 0
|
56
|
+
{"$or" => subconditions}
|
57
|
+
else
|
58
|
+
subconditions[0]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def specify_with_single_currency(name, operator, value)
|
63
|
+
{"#{name}.cents" => {operator => value.cents}, "#{name}.currency_iso" => value.currency.iso_code}
|
64
|
+
end
|
65
|
+
|
20
66
|
def get_cents value
|
21
67
|
value[:cents] || value['cents']
|
22
68
|
end
|
23
69
|
|
24
70
|
def get_currency value
|
25
71
|
value[:currency_iso] || value['currency_iso']
|
26
|
-
end
|
72
|
+
end
|
73
|
+
|
27
74
|
end
|
28
75
|
end
|
29
76
|
|
30
77
|
|
31
|
-
|
32
78
|
class Money
|
33
79
|
include Mongoize
|
34
80
|
|
35
81
|
def __evolve_to_money__
|
36
82
|
self
|
37
83
|
end
|
38
|
-
|
84
|
+
|
85
|
+
def exchange_to_currencies(currencies)
|
86
|
+
currencies.map {|currency| exchange_to(currency)}
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
Mongoid::Fields.option :compare_using do |model, field, value|
|
92
|
+
value.each do |iso_code|
|
93
|
+
unless Money::Currency.find(iso_code)
|
94
|
+
raise ArgumentError, "Invalid ISO currency code: #{value}"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Origin
|
3
|
+
|
4
|
+
# An origin selectable is selectable, in that it has the ability to select
|
5
|
+
# document from the database. The selectable module brings all functionality
|
6
|
+
# to the selectable that has to do with building MongoDB selectors.
|
7
|
+
module Selectable
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
# Create the standard expression query.
|
12
|
+
#
|
13
|
+
# @api private
|
14
|
+
#
|
15
|
+
# @example Create the selection.
|
16
|
+
# selectable.expr_query(age: 50)
|
17
|
+
#
|
18
|
+
# @param [ Hash ] criterion The field/value pairs.
|
19
|
+
#
|
20
|
+
# @return [ Selectable ] The cloned selectable.
|
21
|
+
#
|
22
|
+
# @since 1.0.0
|
23
|
+
def expr_query(criterion)
|
24
|
+
selection(criterion) do |selector, field, value|
|
25
|
+
if (field.is_a? Key) && custom_serialization?(field.name, field.operator)
|
26
|
+
specified = custom_specify(field.name, field.operator, value)
|
27
|
+
else
|
28
|
+
specified = field.specify(value.__expand_complex__, negating?)
|
29
|
+
end
|
30
|
+
selector.merge!(specified)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def between(criterion = nil)
|
35
|
+
selection(criterion) do |selector, field, value|
|
36
|
+
expr = custom_between?(field, value) ? custom_between(field, value) : { "$gte" => value.min, "$lte" => value.max }
|
37
|
+
selector.store(
|
38
|
+
field, expr
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def custom_between? name, value
|
44
|
+
serializer = @serializers[name.to_s]
|
45
|
+
serializer && serializer.type.respond_to?(:custom_between?) && serializer.type.custom_between?(name, value)
|
46
|
+
end
|
47
|
+
|
48
|
+
def custom_between(name, value)
|
49
|
+
serializer = @serializers[name.to_s]
|
50
|
+
raise RuntimeError, "No Serializer found for field #{name}" unless serializer
|
51
|
+
serializer.type.custom_between(name, value, serializer.options)
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def custom_serialization?(name, operator)
|
56
|
+
serializer = @serializers[name.to_s]
|
57
|
+
serializer && serializer.type.respond_to?(:custom_serialization?) && serializer.type.custom_serialization?(operator)
|
58
|
+
end
|
59
|
+
|
60
|
+
def custom_specify(name, operator, value)
|
61
|
+
serializer = @serializers[name.to_s]
|
62
|
+
raise RuntimeError, "No Serializer found for field #{name}" unless serializer
|
63
|
+
serializer.type.custom_specify(name, operator, value, serializer.options)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Range
|
2
|
+
def dollars
|
3
|
+
::MoneyRange.new self, 'usd'
|
4
|
+
end
|
5
|
+
|
6
|
+
def euros
|
7
|
+
::MoneyRange.new self, 'eur'
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_currency iso_code
|
11
|
+
::MoneyRange.new self, iso_code
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Money
|
16
|
+
def self.range range, iso_code
|
17
|
+
::MoneyRange.new range, iso_code
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# http://blog.jayfields.com/2008/02/ruby-replace-methodmissing-with-dynamic.html
|
22
|
+
class DelegateDecorator
|
23
|
+
def initialize(subject)
|
24
|
+
subject.public_methods(false).each do |meth|
|
25
|
+
(class << self; self; end).class_eval do
|
26
|
+
define_method meth do |*args|
|
27
|
+
subject.send meth, *args
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class MoneyRange < DelegateDecorator
|
35
|
+
attr_reader :iso_code, :range
|
36
|
+
|
37
|
+
def initialize range, iso_code
|
38
|
+
super(range)
|
39
|
+
@range = range
|
40
|
+
@iso_code = iso_code
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
data/lib/money/mongoid/macros.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module Mongoid
|
4
4
|
module Monetizable
|
5
|
-
include Money::Orm::Generic
|
5
|
+
include ::Money::Orm::Generic
|
6
6
|
|
7
7
|
def self.included(base)
|
8
8
|
base.class_eval do
|
@@ -11,7 +11,7 @@ module Mongoid
|
|
11
11
|
|
12
12
|
validates_numericality_of :cents
|
13
13
|
end
|
14
|
-
base.extend Money::Orm::Generic::ClassMethods
|
14
|
+
base.extend ::Money::Orm::Generic::ClassMethods
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -2,7 +2,7 @@ module Mongoid
|
|
2
2
|
module Monetize
|
3
3
|
def self.included(base)
|
4
4
|
base.extend ClassMethods
|
5
|
-
base.extend Money::Orm::Generic::ClassMethods
|
5
|
+
base.extend ::Money::Orm::Generic::ClassMethods
|
6
6
|
end
|
7
7
|
|
8
8
|
module ClassMethods
|
@@ -10,12 +10,12 @@ module Mongoid
|
|
10
10
|
as_poly = options.delete(:as) || default_polymorphic_money
|
11
11
|
|
12
12
|
unless as_poly
|
13
|
-
raise ArgumentError, "You must set an :as option indicating the polymorphic Monetizable model. See money-rails gem."
|
13
|
+
raise ArgumentError, "You must set an :as option indicating the polymorphic Monetizable model - was: #{options}. See money-rails gem."
|
14
14
|
end
|
15
15
|
|
16
16
|
self.embeds_one name.to_sym, options.merge(:as => as_poly)
|
17
17
|
end
|
18
|
-
alias_method :monetize, :monetize_one
|
18
|
+
# alias_method :monetize, :monetize_one
|
19
19
|
|
20
20
|
def monetize_many name, options = {:as => :priced}
|
21
21
|
as_poly = options.delete(:as) || default_polymorphic_money
|
data/lib/money/mongoid.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
require 'money'
|
2
2
|
require 'money/mongoid/core_ext'
|
3
|
+
|
4
|
+
require 'money/orm/generic'
|
5
|
+
require 'money/mongoid/macros'
|
6
|
+
require 'money/mongoid/monetizable'
|
7
|
+
require 'money/mongoid/monetize'
|
8
|
+
|
3
9
|
require 'money/orm/generic'
|
4
10
|
|
5
|
-
if Mongoid::VERSION > '3'
|
11
|
+
if ::Mongoid::VERSION > '3'
|
6
12
|
require "money/mongoid/3x/money"
|
7
13
|
else
|
8
14
|
require "money/mongoid/2x/money"
|
@@ -14,4 +20,57 @@ module Mongoid
|
|
14
20
|
attr_accessor :default_polymorphic_money
|
15
21
|
end
|
16
22
|
end
|
23
|
+
|
24
|
+
module Moneys
|
25
|
+
def self.macros
|
26
|
+
[:value, :price, :cost]
|
27
|
+
end
|
28
|
+
|
29
|
+
macros.each do |klass|
|
30
|
+
name = "#{klass}_class"
|
31
|
+
|
32
|
+
# define getter
|
33
|
+
define_singleton_method name do
|
34
|
+
var_name = :"@#{name}"
|
35
|
+
unless instance_variable_get(var_name)
|
36
|
+
instance_variable_set(var_name, klass.to_s.camelize.constantize)
|
37
|
+
end
|
38
|
+
instance_variable_get(var_name)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
macros.each do |name|
|
43
|
+
writ_klass = "#{name}_class"
|
44
|
+
# define attr writer
|
45
|
+
self.send(:attr_writer, writ_klass)
|
46
|
+
end
|
47
|
+
|
48
|
+
class << self
|
49
|
+
def classes= klass
|
50
|
+
macros.each {|m| send("#{m.to_s.underscore}_class=", klass) }
|
51
|
+
end
|
52
|
+
|
53
|
+
def macro_map
|
54
|
+
{
|
55
|
+
:costing => :cost,
|
56
|
+
:priced_at => :price,
|
57
|
+
:valued_at => :value
|
58
|
+
}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class Object
|
65
|
+
Mongoid::Moneys.macro_map.each do |method_name, klass|
|
66
|
+
define_method method_name do |amount, currency=nil|
|
67
|
+
currency = ::Money::Currency.new(currency || ::Money.default_currency)
|
68
|
+
money = Money.new(amount, currency)
|
69
|
+
|
70
|
+
class_name = "#{klass}_class"
|
71
|
+
money_klass = Mongoid::Moneys.send(class_name)
|
72
|
+
money_klass.new :price => money
|
73
|
+
end
|
74
|
+
end
|
17
75
|
end
|
76
|
+
|
data/lib/money/orm/generic.rb
CHANGED
@@ -4,8 +4,11 @@ class Money
|
|
4
4
|
# Virtual price / currency attributes
|
5
5
|
module ClassMethods
|
6
6
|
def monetize_for *names
|
7
|
-
options = names.
|
8
|
-
|
7
|
+
options = names.extract_options!
|
8
|
+
options = {:as => :priced} if options.blank?
|
9
|
+
names.each do |name|
|
10
|
+
monetize_one name, options.dup
|
11
|
+
end
|
9
12
|
end
|
10
13
|
end
|
11
14
|
|
data/lib/money-mongoid.rb
CHANGED
data/money-mongoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "money-mongoid"
|
8
|
-
s.version = "0.1
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kristian Mandrup"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-09-24"
|
13
13
|
s.description = "Makes it easy to use money with mongoid"
|
14
14
|
s.email = "kmandrup@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -29,8 +29,10 @@ Gem::Specification.new do |s|
|
|
29
29
|
"lib/money/mongoid.rb",
|
30
30
|
"lib/money/mongoid/2x/money.rb",
|
31
31
|
"lib/money/mongoid/3x/money.rb",
|
32
|
+
"lib/money/mongoid/3x/origin/selectable.rb",
|
32
33
|
"lib/money/mongoid/core_ext.rb",
|
33
34
|
"lib/money/mongoid/core_ext/array.rb",
|
35
|
+
"lib/money/mongoid/core_ext/range.rb",
|
34
36
|
"lib/money/mongoid/core_ext/string.rb",
|
35
37
|
"lib/money/mongoid/macros.rb",
|
36
38
|
"lib/money/mongoid/monetizable.rb",
|
@@ -41,9 +43,11 @@ Gem::Specification.new do |s|
|
|
41
43
|
"spec/money/mongoid/2x/product.rb",
|
42
44
|
"spec/money/mongoid/3x/money_spec.rb",
|
43
45
|
"spec/money/mongoid/3x/product.rb",
|
46
|
+
"spec/money/mongoid/models.rb",
|
44
47
|
"spec/money/mongoid/models/account.rb",
|
45
48
|
"spec/money/mongoid/models/price.rb",
|
46
49
|
"spec/money/mongoid/monetize_spec.rb",
|
50
|
+
"spec/money/mongoid/shared_ex.rb",
|
47
51
|
"spec/money/mongoid/spec_helper.rb",
|
48
52
|
"spec/money/mongoid/support/mongoid.yml",
|
49
53
|
"spec/money/mongoid/version_setup.rb",
|
@@ -60,7 +64,10 @@ Gem::Specification.new do |s|
|
|
60
64
|
|
61
65
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
62
66
|
s.add_runtime_dependency(%q<money>, [">= 0"])
|
63
|
-
s.add_runtime_dependency(%q<mongoid>, ["
|
67
|
+
s.add_runtime_dependency(%q<mongoid>, ["~> 3.0.0"])
|
68
|
+
s.add_runtime_dependency(%q<origin>, [">= 0"])
|
69
|
+
s.add_runtime_dependency(%q<moped>, [">= 0"])
|
70
|
+
s.add_runtime_dependency(%q<origin-selectable_ext>, [">= 0"])
|
64
71
|
s.add_development_dependency(%q<rspec>, [">= 2.10"])
|
65
72
|
s.add_development_dependency(%q<rdoc>, [">= 3.12"])
|
66
73
|
s.add_development_dependency(%q<bundler>, [">= 1.1.0"])
|
@@ -68,7 +75,10 @@ Gem::Specification.new do |s|
|
|
68
75
|
s.add_development_dependency(%q<simplecov>, [">= 0.5"])
|
69
76
|
else
|
70
77
|
s.add_dependency(%q<money>, [">= 0"])
|
71
|
-
s.add_dependency(%q<mongoid>, ["
|
78
|
+
s.add_dependency(%q<mongoid>, ["~> 3.0.0"])
|
79
|
+
s.add_dependency(%q<origin>, [">= 0"])
|
80
|
+
s.add_dependency(%q<moped>, [">= 0"])
|
81
|
+
s.add_dependency(%q<origin-selectable_ext>, [">= 0"])
|
72
82
|
s.add_dependency(%q<rspec>, [">= 2.10"])
|
73
83
|
s.add_dependency(%q<rdoc>, [">= 3.12"])
|
74
84
|
s.add_dependency(%q<bundler>, [">= 1.1.0"])
|
@@ -77,7 +87,10 @@ Gem::Specification.new do |s|
|
|
77
87
|
end
|
78
88
|
else
|
79
89
|
s.add_dependency(%q<money>, [">= 0"])
|
80
|
-
s.add_dependency(%q<mongoid>, ["
|
90
|
+
s.add_dependency(%q<mongoid>, ["~> 3.0.0"])
|
91
|
+
s.add_dependency(%q<origin>, [">= 0"])
|
92
|
+
s.add_dependency(%q<moped>, [">= 0"])
|
93
|
+
s.add_dependency(%q<origin-selectable_ext>, [">= 0"])
|
81
94
|
s.add_dependency(%q<rspec>, [">= 2.10"])
|
82
95
|
s.add_dependency(%q<rdoc>, [">= 3.12"])
|
83
96
|
s.add_dependency(%q<bundler>, [">= 1.1.0"])
|
@@ -9,6 +9,22 @@ describe 'Mongoid custom Money type' do
|
|
9
9
|
let(:products) do
|
10
10
|
end
|
11
11
|
|
12
|
+
before :all do
|
13
|
+
# Mongoid.logger = Logger.new($stdout)
|
14
|
+
# Moped.logger = Logger.new($stdout)
|
15
|
+
|
16
|
+
Money.add_rate("USD","EUR", 0.5)
|
17
|
+
Money.add_rate("EUR","USD", 2)
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_money iso_code, count = 6, options = {step: 500}
|
21
|
+
step = options[:step]
|
22
|
+
count.times do |n|
|
23
|
+
Product.create :price => Money.new(n * step, iso_code)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
|
12
28
|
its(:price) { should be_a Money }
|
13
29
|
|
14
30
|
specify { subject.price.cents.should == 3000 }
|
@@ -30,12 +46,7 @@ describe 'Mongoid custom Money type' do
|
|
30
46
|
end
|
31
47
|
|
32
48
|
it "should be searchable by price using gte and a money value of different currency" do
|
33
|
-
|
34
|
-
Money.add_rate("EUR","USD", 2)
|
35
|
-
|
36
|
-
6.times do |n|
|
37
|
-
Product.create :price => Money.new(n * 500, 'USD')
|
38
|
-
end
|
49
|
+
create_money 'USD'
|
39
50
|
|
40
51
|
# So far only works by explicit conversion before search. Any solution?
|
41
52
|
Product.all.exchange_to! 'EUR'
|
@@ -46,4 +57,31 @@ describe 'Mongoid custom Money type' do
|
|
46
57
|
search_res = Product.where :price.gte => Money.new(2000, 'EUR')
|
47
58
|
search_res.count.should == 0
|
48
59
|
end
|
60
|
+
|
61
|
+
it 'should search using money range' do
|
62
|
+
create_money 'USD'
|
63
|
+
|
64
|
+
res1 = Product.between price: (500..2000).dollars
|
65
|
+
res2 = Product.between price: (500..2000).to_currency('USD')
|
66
|
+
|
67
|
+
res1.count.should == 4
|
68
|
+
res2.count.should == res1.count
|
69
|
+
|
70
|
+
res3 = Product.between price: Money.range((500..2000), 'usd')
|
71
|
+
res3.count.should == 4
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
it "should respect the currency information when using comparison operators" do
|
76
|
+
Product.delete_all
|
77
|
+
|
78
|
+
create_money 'USD'
|
79
|
+
create_money 'EUR'
|
80
|
+
|
81
|
+
search_res = Product.where :price.gte => Money.new(2000, 'EUR')
|
82
|
+
search_res.count.should == 2
|
83
|
+
search_res = Product.where :price.lt => Money.new(2000, "USD")
|
84
|
+
search_res.count.should == 6
|
85
|
+
|
86
|
+
end
|
49
87
|
end
|
@@ -1,15 +1,13 @@
|
|
1
1
|
class Price
|
2
2
|
include Mongoid::Document
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
# embedded_in :priced, :polymorphic => true
|
4
|
+
monetizable :as => :priced
|
7
5
|
|
8
6
|
def self.for amount, currency = nil
|
9
7
|
currency = ::Money::Currency.new(currency || ::Money.default_currency)
|
10
|
-
money = Money.new(amount, currency)
|
8
|
+
money = ::Money.new(amount, currency)
|
11
9
|
|
12
10
|
# see 'kristianmandrup' fork of money (money/macros)
|
13
|
-
Money::Macros.price_class.new :price => money
|
11
|
+
Mongoid::Money::Macros.price_class.new :price => money
|
14
12
|
end
|
15
13
|
end
|
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'money/mongoid/spec_helper'
|
2
|
+
require 'money/mongoid/models'
|
3
|
+
require 'money/mongoid/shared_ex'
|
2
4
|
|
3
5
|
describe Money::Mongoid::Monetizable do
|
4
6
|
before do
|
@@ -45,7 +47,7 @@ describe Money::Mongoid::Monetizable do
|
|
45
47
|
subject { cost_account }
|
46
48
|
|
47
49
|
before do
|
48
|
-
|
50
|
+
Mongoid::Moneys.cost_class = Price
|
49
51
|
end
|
50
52
|
|
51
53
|
let(:cost_account) do
|
@@ -73,10 +75,10 @@ describe Money::Mongoid::Monetizable do
|
|
73
75
|
subject { valued_at(4000) }
|
74
76
|
|
75
77
|
before do
|
76
|
-
|
78
|
+
Mongoid::Moneys.classes = Price
|
77
79
|
end
|
78
80
|
|
79
|
-
specify {
|
81
|
+
specify { Mongoid::Moneys.value_class.should == Price }
|
80
82
|
|
81
83
|
specify { subject.should be_a Price }
|
82
84
|
specify { subject.price.should be_a Money }
|
@@ -0,0 +1,13 @@
|
|
1
|
+
shared_examples 'a generic account' do
|
2
|
+
it "rental is money" do
|
3
|
+
subject.rental_price.price.should == Money.new(3000)
|
4
|
+
end
|
5
|
+
|
6
|
+
it "deposit is also money" do
|
7
|
+
subject.deposit.price.should == Money.new(150)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "and rent is also money" do
|
11
|
+
subject.rent.price.should == Money.new(100)
|
12
|
+
end
|
13
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: money-mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
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-
|
12
|
+
date: 2012-09-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: money
|
@@ -29,12 +29,28 @@ dependencies:
|
|
29
29
|
version: '0'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
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
|
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
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: origin
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
33
49
|
none: false
|
34
50
|
requirements:
|
35
51
|
- - ! '>='
|
36
52
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
53
|
+
version: '0'
|
38
54
|
type: :runtime
|
39
55
|
prerelease: false
|
40
56
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -42,7 +58,39 @@ dependencies:
|
|
42
58
|
requirements:
|
43
59
|
- - ! '>='
|
44
60
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: moped
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: origin-selectable_ext
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :runtime
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
46
94
|
- !ruby/object:Gem::Dependency
|
47
95
|
name: rspec
|
48
96
|
requirement: !ruby/object:Gem::Requirement
|
@@ -143,8 +191,10 @@ files:
|
|
143
191
|
- lib/money/mongoid.rb
|
144
192
|
- lib/money/mongoid/2x/money.rb
|
145
193
|
- lib/money/mongoid/3x/money.rb
|
194
|
+
- lib/money/mongoid/3x/origin/selectable.rb
|
146
195
|
- lib/money/mongoid/core_ext.rb
|
147
196
|
- lib/money/mongoid/core_ext/array.rb
|
197
|
+
- lib/money/mongoid/core_ext/range.rb
|
148
198
|
- lib/money/mongoid/core_ext/string.rb
|
149
199
|
- lib/money/mongoid/macros.rb
|
150
200
|
- lib/money/mongoid/monetizable.rb
|
@@ -155,9 +205,11 @@ files:
|
|
155
205
|
- spec/money/mongoid/2x/product.rb
|
156
206
|
- spec/money/mongoid/3x/money_spec.rb
|
157
207
|
- spec/money/mongoid/3x/product.rb
|
208
|
+
- spec/money/mongoid/models.rb
|
158
209
|
- spec/money/mongoid/models/account.rb
|
159
210
|
- spec/money/mongoid/models/price.rb
|
160
211
|
- spec/money/mongoid/monetize_spec.rb
|
212
|
+
- spec/money/mongoid/shared_ex.rb
|
161
213
|
- spec/money/mongoid/spec_helper.rb
|
162
214
|
- spec/money/mongoid/support/mongoid.yml
|
163
215
|
- spec/money/mongoid/version_setup.rb
|
@@ -177,7 +229,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
177
229
|
version: '0'
|
178
230
|
segments:
|
179
231
|
- 0
|
180
|
-
hash:
|
232
|
+
hash: 3922419886446166376
|
181
233
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
182
234
|
none: false
|
183
235
|
requirements:
|