djsun-mongo_mapper 0.5.4.1 → 0.5.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +4 -2
- data/VERSION +1 -1
- data/djsun-mongo_mapper.gemspec +21 -12
- data/lib/mongo_mapper/associations/base.rb +31 -2
- data/lib/mongo_mapper/associations/many_documents_proxy.rb +3 -1
- data/lib/mongo_mapper/associations.rb +13 -12
- data/lib/mongo_mapper/callbacks.rb +52 -67
- data/lib/mongo_mapper/dirty.rb +11 -18
- data/lib/mongo_mapper/document.rb +19 -10
- data/lib/mongo_mapper/embedded_document.rb +6 -2
- data/lib/mongo_mapper/rails_compatibility/document.rb +0 -4
- data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +0 -4
- data/lib/mongo_mapper.rb +2 -3
- data/mongo_mapper.gemspec +19 -12
- data/test/functional/associations/test_many_documents_as_proxy.rb +2 -1
- data/test/functional/associations/test_namespace.rb +33 -0
- data/test/functional/test_document.rb +12 -7
- data/test/functional/test_embedded_document.rb +9 -1
- data/test/functional/test_rails_compatibility.rb +0 -5
- data/test/functional/test_validations.rb +20 -0
- data/test/models.rb +24 -0
- data/test/{custom_matchers.rb → support/custom_matchers.rb} +0 -0
- data/test/support/test_timing.rb +16 -0
- data/test/test_helper.rb +9 -9
- data/test/unit/test_embedded_document.rb +12 -1
- data/test/unit/test_rails_compatibility.rb +1 -5
- data/test/unit/test_serializations.rb +52 -52
- data/test/unit/test_time_zones.rb +39 -39
- metadata +33 -10
- data/lib/mongo_mapper/save_with_validation.rb +0 -19
data/Rakefile
CHANGED
@@ -11,12 +11,14 @@ begin
|
|
11
11
|
gem.authors = ["John Nunemaker"]
|
12
12
|
gem.rubyforge_project = "mongomapper"
|
13
13
|
|
14
|
-
gem.add_dependency('activesupport')
|
14
|
+
gem.add_dependency('activesupport', '>= 2.3')
|
15
15
|
gem.add_dependency('mongo', '0.15.1')
|
16
16
|
gem.add_dependency('jnunemaker-validatable', '1.7.4')
|
17
17
|
|
18
|
-
gem.add_development_dependency('mocha', '0.9.4')
|
19
18
|
gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
|
19
|
+
gem.add_development_dependency('shoulda', '2.10.2')
|
20
|
+
gem.add_development_dependency('timecop', '0.3.1')
|
21
|
+
gem.add_development_dependency('mocha', '0.9.4')
|
20
22
|
end
|
21
23
|
|
22
24
|
Jeweler::RubyforgeTasks.new do |rubyforge|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.5.2
|
data/djsun-mongo_mapper.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{djsun-mongo_mapper}
|
8
|
-
s.version = "0.5.
|
8
|
+
s.version = "0.5.5.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["John Nunemaker"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-16}
|
13
13
|
s.default_executable = %q{mmconsole}
|
14
14
|
s.email = %q{nunemaker@gmail.com}
|
15
15
|
s.executables = ["mmconsole"]
|
@@ -48,7 +48,6 @@ Gem::Specification.new do |s|
|
|
48
48
|
"lib/mongo_mapper/pagination.rb",
|
49
49
|
"lib/mongo_mapper/rails_compatibility/document.rb",
|
50
50
|
"lib/mongo_mapper/rails_compatibility/embedded_document.rb",
|
51
|
-
"lib/mongo_mapper/save_with_validation.rb",
|
52
51
|
"lib/mongo_mapper/serialization.rb",
|
53
52
|
"lib/mongo_mapper/serializers/json_serializer.rb",
|
54
53
|
"lib/mongo_mapper/support.rb",
|
@@ -56,7 +55,6 @@ Gem::Specification.new do |s|
|
|
56
55
|
"mongo_mapper.gemspec",
|
57
56
|
"specs.watchr",
|
58
57
|
"test/NOTE_ON_TESTING",
|
59
|
-
"test/custom_matchers.rb",
|
60
58
|
"test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
|
61
59
|
"test/functional/associations/test_belongs_to_proxy.rb",
|
62
60
|
"test/functional/associations/test_many_documents_as_proxy.rb",
|
@@ -64,6 +62,7 @@ Gem::Specification.new do |s|
|
|
64
62
|
"test/functional/associations/test_many_embedded_proxy.rb",
|
65
63
|
"test/functional/associations/test_many_polymorphic_proxy.rb",
|
66
64
|
"test/functional/associations/test_many_proxy.rb",
|
65
|
+
"test/functional/associations/test_namespace.rb",
|
67
66
|
"test/functional/test_associations.rb",
|
68
67
|
"test/functional/test_binary.rb",
|
69
68
|
"test/functional/test_callbacks.rb",
|
@@ -75,6 +74,8 @@ Gem::Specification.new do |s|
|
|
75
74
|
"test/functional/test_rails_compatibility.rb",
|
76
75
|
"test/functional/test_validations.rb",
|
77
76
|
"test/models.rb",
|
77
|
+
"test/support/custom_matchers.rb",
|
78
|
+
"test/support/test_timing.rb",
|
78
79
|
"test/test_helper.rb",
|
79
80
|
"test/unit/serializers/test_json_serializer.rb",
|
80
81
|
"test/unit/test_association_base.rb",
|
@@ -99,14 +100,14 @@ Gem::Specification.new do |s|
|
|
99
100
|
s.rubygems_version = %q{1.3.5}
|
100
101
|
s.summary = %q{Awesome gem for modeling your domain and storing it in mongo}
|
101
102
|
s.test_files = [
|
102
|
-
"test/
|
103
|
-
"test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
|
103
|
+
"test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
|
104
104
|
"test/functional/associations/test_belongs_to_proxy.rb",
|
105
105
|
"test/functional/associations/test_many_documents_as_proxy.rb",
|
106
106
|
"test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
|
107
107
|
"test/functional/associations/test_many_embedded_proxy.rb",
|
108
108
|
"test/functional/associations/test_many_polymorphic_proxy.rb",
|
109
109
|
"test/functional/associations/test_many_proxy.rb",
|
110
|
+
"test/functional/associations/test_namespace.rb",
|
110
111
|
"test/functional/test_associations.rb",
|
111
112
|
"test/functional/test_binary.rb",
|
112
113
|
"test/functional/test_callbacks.rb",
|
@@ -118,6 +119,8 @@ Gem::Specification.new do |s|
|
|
118
119
|
"test/functional/test_rails_compatibility.rb",
|
119
120
|
"test/functional/test_validations.rb",
|
120
121
|
"test/models.rb",
|
122
|
+
"test/support/custom_matchers.rb",
|
123
|
+
"test/support/test_timing.rb",
|
121
124
|
"test/test_helper.rb",
|
122
125
|
"test/unit/serializers/test_json_serializer.rb",
|
123
126
|
"test/unit/test_association_base.rb",
|
@@ -141,23 +144,29 @@ Gem::Specification.new do |s|
|
|
141
144
|
s.specification_version = 3
|
142
145
|
|
143
146
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
144
|
-
s.add_runtime_dependency(%q<activesupport>, [">=
|
147
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 2.3"])
|
145
148
|
s.add_runtime_dependency(%q<mongo>, ["= 0.15.1"])
|
146
149
|
s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.7.4"])
|
147
|
-
s.add_development_dependency(%q<mocha>, ["= 0.9.4"])
|
148
150
|
s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
151
|
+
s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
|
152
|
+
s.add_development_dependency(%q<timecop>, ["= 0.3.1"])
|
153
|
+
s.add_development_dependency(%q<mocha>, ["= 0.9.4"])
|
149
154
|
else
|
150
|
-
s.add_dependency(%q<activesupport>, [">=
|
155
|
+
s.add_dependency(%q<activesupport>, [">= 2.3"])
|
151
156
|
s.add_dependency(%q<mongo>, ["= 0.15.1"])
|
152
157
|
s.add_dependency(%q<jnunemaker-validatable>, ["= 1.7.4"])
|
153
|
-
s.add_dependency(%q<mocha>, ["= 0.9.4"])
|
154
158
|
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
159
|
+
s.add_dependency(%q<shoulda>, ["= 2.10.2"])
|
160
|
+
s.add_dependency(%q<timecop>, ["= 0.3.1"])
|
161
|
+
s.add_dependency(%q<mocha>, ["= 0.9.4"])
|
155
162
|
end
|
156
163
|
else
|
157
|
-
s.add_dependency(%q<activesupport>, [">=
|
164
|
+
s.add_dependency(%q<activesupport>, [">= 2.3"])
|
158
165
|
s.add_dependency(%q<mongo>, ["= 0.15.1"])
|
159
166
|
s.add_dependency(%q<jnunemaker-validatable>, ["= 1.7.4"])
|
160
|
-
s.add_dependency(%q<mocha>, ["= 0.9.4"])
|
161
167
|
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
168
|
+
s.add_dependency(%q<shoulda>, ["= 2.10.2"])
|
169
|
+
s.add_dependency(%q<timecop>, ["= 0.3.1"])
|
170
|
+
s.add_dependency(%q<mocha>, ["= 0.9.4"])
|
162
171
|
end
|
163
172
|
end
|
@@ -1,3 +1,32 @@
|
|
1
|
+
module ConstantHelper
|
2
|
+
def self.lookup(class_name, scope)
|
3
|
+
if namespaced?(class_name)
|
4
|
+
class_name.constantize
|
5
|
+
else
|
6
|
+
lookup_in_parent(class_name, scope)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.namespaced?(class_name)
|
11
|
+
class_name.include?("::")
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.lookup_in_parent(class_name, scope)
|
15
|
+
parent = parent_scope(scope)
|
16
|
+
return nil unless parent.const_defined?(class_name)
|
17
|
+
parent.const_get(class_name)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.parent_scope(scope)
|
21
|
+
chain = scope.to_s.split("::")[0 ... -1]
|
22
|
+
chain.reduce(Object) { |m, o| m.const_get(o) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.leaf(constant_name)
|
26
|
+
constant_name.split("::").last
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
1
30
|
module MongoMapper
|
2
31
|
module Associations
|
3
32
|
class Base
|
@@ -18,9 +47,9 @@ module MongoMapper
|
|
18
47
|
end
|
19
48
|
end
|
20
49
|
end
|
21
|
-
|
50
|
+
|
22
51
|
def klass
|
23
|
-
@klass ||= class_name
|
52
|
+
@klass ||= ConstantHelper.lookup(class_name, options[:scope])
|
24
53
|
end
|
25
54
|
|
26
55
|
def many?
|
@@ -109,7 +109,9 @@ module MongoMapper
|
|
109
109
|
end
|
110
110
|
|
111
111
|
def foreign_key
|
112
|
-
@association.options[:foreign_key] ||
|
112
|
+
@association.options[:foreign_key] ||
|
113
|
+
(ConstantHelper.leaf(@owner.class.name).
|
114
|
+
underscore.gsub("/", "_") + "_id")
|
113
115
|
end
|
114
116
|
end
|
115
117
|
end
|
@@ -2,12 +2,12 @@ module MongoMapper
|
|
2
2
|
module Associations
|
3
3
|
module ClassMethods
|
4
4
|
def belongs_to(association_id, options = {})
|
5
|
-
create_association(:belongs_to, association_id, options)
|
5
|
+
create_association(:belongs_to, association_id, options, self)
|
6
6
|
self
|
7
7
|
end
|
8
8
|
|
9
9
|
def many(association_id, options = {})
|
10
|
-
create_association(:many, association_id, options)
|
10
|
+
create_association(:many, association_id, options, self)
|
11
11
|
self
|
12
12
|
end
|
13
13
|
|
@@ -18,7 +18,8 @@ module MongoMapper
|
|
18
18
|
end
|
19
19
|
|
20
20
|
private
|
21
|
-
def create_association(type, name, options)
|
21
|
+
def create_association(type, name, options, scope)
|
22
|
+
options[:scope] = scope
|
22
23
|
association = Associations::Base.new(type, name, options)
|
23
24
|
associations[association.name] = association
|
24
25
|
define_association_methods(association)
|
@@ -48,16 +49,16 @@ module MongoMapper
|
|
48
49
|
end
|
49
50
|
|
50
51
|
def define_dependent_callback_for_many(association)
|
51
|
-
return if association.embeddable?
|
52
|
-
|
53
52
|
after_destroy do |doc|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
53
|
+
if !association.embeddable?
|
54
|
+
case association.options[:dependent]
|
55
|
+
when :destroy
|
56
|
+
doc.get_proxy(association).destroy_all
|
57
|
+
when :delete_all
|
58
|
+
doc.get_proxy(association).delete_all
|
59
|
+
when :nullify
|
60
|
+
doc.get_proxy(association).nullify
|
61
|
+
end
|
61
62
|
end
|
62
63
|
end
|
63
64
|
end
|
@@ -1,87 +1,50 @@
|
|
1
1
|
module MongoMapper
|
2
|
-
module Callbacks
|
2
|
+
module Callbacks
|
3
3
|
def self.included(model) #:nodoc:
|
4
4
|
model.class_eval do
|
5
5
|
extend Observable
|
6
6
|
include ActiveSupport::Callbacks
|
7
|
-
|
8
|
-
|
9
|
-
before_save
|
10
|
-
|
11
|
-
|
7
|
+
|
8
|
+
callbacks = %w(
|
9
|
+
before_save
|
10
|
+
after_save
|
11
|
+
before_create
|
12
|
+
after_create
|
13
|
+
before_update
|
14
|
+
after_update
|
15
|
+
before_validation
|
16
|
+
after_validation
|
17
|
+
before_validation_on_create
|
18
|
+
after_validation_on_create
|
19
|
+
before_validation_on_update
|
20
|
+
after_validation_on_update
|
21
|
+
before_destroy
|
22
|
+
after_destroy
|
12
23
|
)
|
13
|
-
|
14
|
-
[:create_or_update, :valid?, :create, :update, :destroy].each do |method|
|
15
|
-
alias_method_chain method, :callbacks
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
24
|
|
20
|
-
|
25
|
+
define_callbacks(*callbacks)
|
21
26
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
if result = create_or_update_without_callbacks
|
26
|
-
callback(:after_save)
|
27
|
+
callbacks.each do |callback|
|
28
|
+
define_method(callback.to_sym) {}
|
29
|
+
end
|
27
30
|
end
|
28
|
-
result
|
29
31
|
end
|
30
|
-
private :create_or_update_with_callbacks
|
31
|
-
|
32
|
-
def before_create() end
|
33
|
-
|
34
|
-
def after_create() end
|
35
|
-
def create_with_callbacks #:nodoc:
|
36
|
-
return false if callback(:before_create) == false
|
37
|
-
result = create_without_callbacks
|
38
|
-
callback(:after_create)
|
39
|
-
result
|
40
|
-
end
|
41
|
-
private :create_with_callbacks
|
42
|
-
|
43
|
-
def before_update() end
|
44
|
-
|
45
|
-
def after_update() end
|
46
|
-
|
47
|
-
def update_with_callbacks(*args) #:nodoc:
|
48
|
-
return false if callback(:before_update) == false
|
49
|
-
result = update_without_callbacks(*args)
|
50
|
-
callback(:after_update)
|
51
|
-
result
|
52
|
-
end
|
53
|
-
private :update_with_callbacks
|
54
|
-
|
55
|
-
def before_validation() end
|
56
|
-
|
57
|
-
def after_validation() end
|
58
32
|
|
59
|
-
def
|
60
|
-
|
61
|
-
def after_validation_on_create() end
|
62
|
-
|
63
|
-
def before_validation_on_update() end
|
64
|
-
|
65
|
-
def after_validation_on_update() end
|
66
|
-
|
67
|
-
def valid_with_callbacks? #:nodoc:
|
33
|
+
def valid? #:nodoc:
|
68
34
|
return false if callback(:before_validation) == false
|
69
35
|
result = new? ? callback(:before_validation_on_create) : callback(:before_validation_on_update)
|
70
36
|
return false if false == result
|
71
|
-
|
72
|
-
result =
|
37
|
+
|
38
|
+
result = super
|
73
39
|
callback(:after_validation)
|
74
|
-
|
40
|
+
|
75
41
|
new? ? callback(:after_validation_on_create) : callback(:after_validation_on_update)
|
76
42
|
return result
|
77
43
|
end
|
78
44
|
|
79
|
-
def
|
80
|
-
|
81
|
-
def after_destroy() end
|
82
|
-
def destroy_with_callbacks #:nodoc:
|
45
|
+
def destroy #:nodoc:
|
83
46
|
return false if callback(:before_destroy) == false
|
84
|
-
result =
|
47
|
+
result = super
|
85
48
|
callback(:after_destroy)
|
86
49
|
result
|
87
50
|
end
|
@@ -89,18 +52,40 @@ module MongoMapper
|
|
89
52
|
private
|
90
53
|
def callback(method)
|
91
54
|
result = run_callbacks(method) { |result, object| false == result }
|
92
|
-
|
55
|
+
|
93
56
|
if result != false && respond_to?(method)
|
94
57
|
result = send(method)
|
95
58
|
end
|
96
|
-
|
59
|
+
|
97
60
|
notify(method)
|
98
61
|
return result
|
99
62
|
end
|
100
|
-
|
63
|
+
|
101
64
|
def notify(method) #:nodoc:
|
102
65
|
self.class.changed
|
103
66
|
self.class.notify_observers(method, self)
|
104
67
|
end
|
68
|
+
|
69
|
+
def create_or_update #:nodoc:
|
70
|
+
return false if callback(:before_save) == false
|
71
|
+
if result = super
|
72
|
+
callback(:after_save)
|
73
|
+
end
|
74
|
+
result
|
75
|
+
end
|
76
|
+
|
77
|
+
def create #:nodoc:
|
78
|
+
return false if callback(:before_create) == false
|
79
|
+
result = super
|
80
|
+
callback(:after_create)
|
81
|
+
result
|
82
|
+
end
|
83
|
+
|
84
|
+
def update(*args) #:nodoc:
|
85
|
+
return false if callback(:before_update) == false
|
86
|
+
result = super
|
87
|
+
callback(:after_update)
|
88
|
+
result
|
89
|
+
end
|
105
90
|
end
|
106
91
|
end
|
data/lib/mongo_mapper/dirty.rb
CHANGED
@@ -2,13 +2,6 @@ module MongoMapper
|
|
2
2
|
module Dirty
|
3
3
|
DIRTY_SUFFIXES = ['_changed?', '_change', '_will_change!', '_was']
|
4
4
|
|
5
|
-
def self.included(base)
|
6
|
-
base.alias_method_chain :initialize, :dirty
|
7
|
-
base.alias_method_chain :write_attribute, :dirty
|
8
|
-
base.alias_method_chain :save, :dirty
|
9
|
-
base.alias_method_chain :save!, :dirty
|
10
|
-
end
|
11
|
-
|
12
5
|
def method_missing(method, *args, &block)
|
13
6
|
if method.to_s =~ /(_changed\?|_change|_will_change!|_was)$/
|
14
7
|
method_suffix = $1
|
@@ -53,26 +46,26 @@ module MongoMapper
|
|
53
46
|
changed.inject({}) { |h, attribute| h[attribute] = key_change(attribute); h }
|
54
47
|
end
|
55
48
|
|
56
|
-
def
|
57
|
-
|
49
|
+
def initialize(attrs={})
|
50
|
+
super(attrs)
|
58
51
|
changed_keys.clear unless new?
|
59
52
|
end
|
60
|
-
|
53
|
+
|
61
54
|
# Attempts to +save+ the record and clears changed keys if successful.
|
62
|
-
def
|
63
|
-
if status =
|
55
|
+
def save(*args)
|
56
|
+
if status = super
|
64
57
|
changed_keys.clear
|
65
58
|
end
|
66
59
|
status
|
67
60
|
end
|
68
61
|
|
69
62
|
# Attempts to <tt>save!</tt> the record and clears changed keys if successful.
|
70
|
-
def
|
71
|
-
status =
|
63
|
+
def save!(*args)
|
64
|
+
status = super
|
72
65
|
changed_keys.clear
|
73
66
|
status
|
74
67
|
end
|
75
|
-
|
68
|
+
|
76
69
|
# <tt>reload</tt> the record and clears changed keys.
|
77
70
|
# def reload_with_dirty(*args) #:nodoc:
|
78
71
|
# record = reload_without_dirty(*args)
|
@@ -114,7 +107,7 @@ module MongoMapper
|
|
114
107
|
end
|
115
108
|
|
116
109
|
# Wrap write_attribute to remember original key value.
|
117
|
-
def
|
110
|
+
def write_attribute(attribute, value)
|
118
111
|
attribute = attribute.to_s
|
119
112
|
|
120
113
|
# The key already has an unsaved change.
|
@@ -127,7 +120,7 @@ module MongoMapper
|
|
127
120
|
end
|
128
121
|
|
129
122
|
# Carry on.
|
130
|
-
|
123
|
+
super(attribute, value)
|
131
124
|
end
|
132
125
|
|
133
126
|
def value_changed?(key_name, old, value)
|
@@ -140,4 +133,4 @@ module MongoMapper
|
|
140
133
|
old != value
|
141
134
|
end
|
142
135
|
end
|
143
|
-
end
|
136
|
+
end
|
@@ -8,7 +8,6 @@ module MongoMapper
|
|
8
8
|
include InstanceMethods
|
9
9
|
include Observing
|
10
10
|
include Callbacks
|
11
|
-
include SaveWithValidation
|
12
11
|
include Dirty
|
13
12
|
include RailsCompatibility::Document
|
14
13
|
extend Validations::Macros
|
@@ -111,13 +110,11 @@ module MongoMapper
|
|
111
110
|
end
|
112
111
|
|
113
112
|
def create(*docs)
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
end
|
120
|
-
instances.size == 1 ? instances[0] : instances
|
113
|
+
initialize_each(*docs) { |doc| doc.save }
|
114
|
+
end
|
115
|
+
|
116
|
+
def create!(*docs)
|
117
|
+
initialize_each(*docs) { |doc| doc.save! }
|
121
118
|
end
|
122
119
|
|
123
120
|
# For updating single document
|
@@ -207,6 +204,18 @@ module MongoMapper
|
|
207
204
|
end
|
208
205
|
|
209
206
|
private
|
207
|
+
# Initializes each document and yields each initialized document
|
208
|
+
def initialize_each(*docs)
|
209
|
+
instances = []
|
210
|
+
docs = [{}] if docs.blank?
|
211
|
+
docs.flatten.each do |attrs|
|
212
|
+
doc = new(attrs)
|
213
|
+
yield(doc)
|
214
|
+
instances << doc
|
215
|
+
end
|
216
|
+
instances.size == 1 ? instances[0] : instances
|
217
|
+
end
|
218
|
+
|
210
219
|
def create_indexes_for(key)
|
211
220
|
ensure_index key.name if key.options[:index]
|
212
221
|
end
|
@@ -285,11 +294,11 @@ module MongoMapper
|
|
285
294
|
end
|
286
295
|
|
287
296
|
def save
|
288
|
-
create_or_update
|
297
|
+
valid? ? create_or_update : false
|
289
298
|
end
|
290
299
|
|
291
300
|
def save!
|
292
|
-
create_or_update
|
301
|
+
valid? ? create_or_update : raise(DocumentNotValid.new(self))
|
293
302
|
end
|
294
303
|
|
295
304
|
def destroy
|
@@ -198,6 +198,10 @@ module MongoMapper
|
|
198
198
|
def new?
|
199
199
|
!!@new_document
|
200
200
|
end
|
201
|
+
|
202
|
+
def to_param
|
203
|
+
id
|
204
|
+
end
|
201
205
|
|
202
206
|
def attributes=(attrs)
|
203
207
|
return if attrs.blank?
|
@@ -308,7 +312,7 @@ module MongoMapper
|
|
308
312
|
|
309
313
|
private
|
310
314
|
def _keys
|
311
|
-
self.
|
315
|
+
self.metaclass.keys
|
312
316
|
end
|
313
317
|
|
314
318
|
def key_names
|
@@ -324,7 +328,7 @@ module MongoMapper
|
|
324
328
|
end
|
325
329
|
|
326
330
|
def ensure_key_exists(name)
|
327
|
-
self.
|
331
|
+
self.metaclass.key(name) unless respond_to?("#{name}=")
|
328
332
|
end
|
329
333
|
|
330
334
|
def read_attribute(name)
|
data/lib/mongo_mapper.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
|
3
|
-
gem 'activesupport'
|
3
|
+
gem 'activesupport', '>= 2.3'
|
4
4
|
gem 'mongo', '0.15.1'
|
5
5
|
gem 'jnunemaker-validatable', '1.7.4'
|
6
6
|
|
@@ -102,10 +102,9 @@ require 'mongo_mapper/dynamic_finder'
|
|
102
102
|
require 'mongo_mapper/key'
|
103
103
|
require 'mongo_mapper/observing'
|
104
104
|
require 'mongo_mapper/pagination'
|
105
|
-
require 'mongo_mapper/save_with_validation'
|
106
105
|
require 'mongo_mapper/serialization'
|
107
106
|
require 'mongo_mapper/validations'
|
108
107
|
require 'mongo_mapper/rails_compatibility/document'
|
109
108
|
require 'mongo_mapper/rails_compatibility/embedded_document'
|
110
109
|
require 'mongo_mapper/embedded_document'
|
111
|
-
require 'mongo_mapper/document'
|
110
|
+
require 'mongo_mapper/document'
|
data/mongo_mapper.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongo_mapper}
|
8
|
-
s.version = "0.5.
|
8
|
+
s.version = "0.5.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["John Nunemaker"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-16}
|
13
13
|
s.default_executable = %q{mmconsole}
|
14
14
|
s.email = %q{nunemaker@gmail.com}
|
15
15
|
s.executables = ["mmconsole"]
|
@@ -47,7 +47,6 @@ Gem::Specification.new do |s|
|
|
47
47
|
"lib/mongo_mapper/pagination.rb",
|
48
48
|
"lib/mongo_mapper/rails_compatibility/document.rb",
|
49
49
|
"lib/mongo_mapper/rails_compatibility/embedded_document.rb",
|
50
|
-
"lib/mongo_mapper/save_with_validation.rb",
|
51
50
|
"lib/mongo_mapper/serialization.rb",
|
52
51
|
"lib/mongo_mapper/serializers/json_serializer.rb",
|
53
52
|
"lib/mongo_mapper/support.rb",
|
@@ -55,7 +54,6 @@ Gem::Specification.new do |s|
|
|
55
54
|
"mongo_mapper.gemspec",
|
56
55
|
"specs.watchr",
|
57
56
|
"test/NOTE_ON_TESTING",
|
58
|
-
"test/custom_matchers.rb",
|
59
57
|
"test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
|
60
58
|
"test/functional/associations/test_belongs_to_proxy.rb",
|
61
59
|
"test/functional/associations/test_many_documents_as_proxy.rb",
|
@@ -74,6 +72,8 @@ Gem::Specification.new do |s|
|
|
74
72
|
"test/functional/test_rails_compatibility.rb",
|
75
73
|
"test/functional/test_validations.rb",
|
76
74
|
"test/models.rb",
|
75
|
+
"test/support/custom_matchers.rb",
|
76
|
+
"test/support/test_timing.rb",
|
77
77
|
"test/test_helper.rb",
|
78
78
|
"test/unit/serializers/test_json_serializer.rb",
|
79
79
|
"test/unit/test_association_base.rb",
|
@@ -98,8 +98,7 @@ Gem::Specification.new do |s|
|
|
98
98
|
s.rubygems_version = %q{1.3.5}
|
99
99
|
s.summary = %q{Awesome gem for modeling your domain and storing it in mongo}
|
100
100
|
s.test_files = [
|
101
|
-
"test/
|
102
|
-
"test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
|
101
|
+
"test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
|
103
102
|
"test/functional/associations/test_belongs_to_proxy.rb",
|
104
103
|
"test/functional/associations/test_many_documents_as_proxy.rb",
|
105
104
|
"test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
|
@@ -117,6 +116,8 @@ Gem::Specification.new do |s|
|
|
117
116
|
"test/functional/test_rails_compatibility.rb",
|
118
117
|
"test/functional/test_validations.rb",
|
119
118
|
"test/models.rb",
|
119
|
+
"test/support/custom_matchers.rb",
|
120
|
+
"test/support/test_timing.rb",
|
120
121
|
"test/test_helper.rb",
|
121
122
|
"test/unit/serializers/test_json_serializer.rb",
|
122
123
|
"test/unit/test_association_base.rb",
|
@@ -140,23 +141,29 @@ Gem::Specification.new do |s|
|
|
140
141
|
s.specification_version = 3
|
141
142
|
|
142
143
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
143
|
-
s.add_runtime_dependency(%q<activesupport>, [">=
|
144
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 2.3"])
|
144
145
|
s.add_runtime_dependency(%q<mongo>, ["= 0.15.1"])
|
145
146
|
s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.7.4"])
|
146
|
-
s.add_development_dependency(%q<mocha>, ["= 0.9.4"])
|
147
147
|
s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
148
|
+
s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
|
149
|
+
s.add_development_dependency(%q<timecop>, ["= 0.3.1"])
|
150
|
+
s.add_development_dependency(%q<mocha>, ["= 0.9.4"])
|
148
151
|
else
|
149
|
-
s.add_dependency(%q<activesupport>, [">=
|
152
|
+
s.add_dependency(%q<activesupport>, [">= 2.3"])
|
150
153
|
s.add_dependency(%q<mongo>, ["= 0.15.1"])
|
151
154
|
s.add_dependency(%q<jnunemaker-validatable>, ["= 1.7.4"])
|
152
|
-
s.add_dependency(%q<mocha>, ["= 0.9.4"])
|
153
155
|
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
156
|
+
s.add_dependency(%q<shoulda>, ["= 2.10.2"])
|
157
|
+
s.add_dependency(%q<timecop>, ["= 0.3.1"])
|
158
|
+
s.add_dependency(%q<mocha>, ["= 0.9.4"])
|
154
159
|
end
|
155
160
|
else
|
156
|
-
s.add_dependency(%q<activesupport>, [">=
|
161
|
+
s.add_dependency(%q<activesupport>, [">= 2.3"])
|
157
162
|
s.add_dependency(%q<mongo>, ["= 0.15.1"])
|
158
163
|
s.add_dependency(%q<jnunemaker-validatable>, ["= 1.7.4"])
|
159
|
-
s.add_dependency(%q<mocha>, ["= 0.9.4"])
|
160
164
|
s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
|
165
|
+
s.add_dependency(%q<shoulda>, ["= 2.10.2"])
|
166
|
+
s.add_dependency(%q<timecop>, ["= 0.3.1"])
|
167
|
+
s.add_dependency(%q<mocha>, ["= 0.9.4"])
|
161
168
|
end
|
162
169
|
end
|
@@ -149,7 +149,8 @@ class ManyDocumentsAsProxyTest < Test::Unit::TestCase
|
|
149
149
|
|
150
150
|
context "with #all" do
|
151
151
|
should "work" do
|
152
|
-
@post.comments.all.should
|
152
|
+
@post.comments.all.should include(@comment1)
|
153
|
+
@post.comments.all.should include(@comment2)
|
153
154
|
end
|
154
155
|
|
155
156
|
should "work with conditions" do
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class NamespaceTest < Test::Unit::TestCase
|
5
|
+
include Hollywood
|
6
|
+
|
7
|
+
def setup
|
8
|
+
Movie.collection.clear
|
9
|
+
Actor.collection.clear
|
10
|
+
Role.collection.clear
|
11
|
+
end
|
12
|
+
|
13
|
+
context "Hollywood namespace" do
|
14
|
+
setup do
|
15
|
+
@movie = Movie.create
|
16
|
+
@actor = Actor.create
|
17
|
+
@role = Role.create(
|
18
|
+
:movie_id => @movie.id,
|
19
|
+
:actor_id => @actor.id
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
should "belongs_to associations" do
|
24
|
+
@role.actor.should == @actor
|
25
|
+
@role.movie.should == @movie
|
26
|
+
end
|
27
|
+
|
28
|
+
should "many associations" do
|
29
|
+
@movie.roles.should == [@role]
|
30
|
+
@actor.roles.should == [@role]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -669,7 +669,7 @@ class DocumentTest < Test::Unit::TestCase
|
|
669
669
|
@thing.properties << @property3
|
670
670
|
end
|
671
671
|
|
672
|
-
|
672
|
+
should_eventually "destroy the thing" do
|
673
673
|
Thing.count.should == 1
|
674
674
|
@property1.destroy
|
675
675
|
Thing.count.should == 0
|
@@ -1012,8 +1012,11 @@ class DocumentTest < Test::Unit::TestCase
|
|
1012
1012
|
old_created_at = doc.created_at
|
1013
1013
|
old_updated_at = doc.updated_at
|
1014
1014
|
doc.first_name = 'Johnny'
|
1015
|
-
|
1016
|
-
|
1015
|
+
|
1016
|
+
Timecop.freeze(Time.now + 5.seconds) do
|
1017
|
+
doc.save
|
1018
|
+
end
|
1019
|
+
|
1017
1020
|
doc.created_at.should == old_created_at
|
1018
1021
|
doc.updated_at.should_not == old_updated_at
|
1019
1022
|
end
|
@@ -1022,12 +1025,14 @@ class DocumentTest < Test::Unit::TestCase
|
|
1022
1025
|
doc = @document.create(:first_name => 'John', :age => 27)
|
1023
1026
|
old_created_at = doc.created_at
|
1024
1027
|
old_updated_at = doc.updated_at
|
1025
|
-
|
1026
|
-
|
1028
|
+
|
1029
|
+
Timecop.freeze(Time.now + 5.seconds) do
|
1030
|
+
@document.update(doc._id, { :first_name => 'Johnny' })
|
1031
|
+
end
|
1027
1032
|
|
1028
1033
|
from_db = @document.find(doc.id)
|
1029
|
-
from_db.created_at.
|
1030
|
-
from_db.updated_at.
|
1034
|
+
from_db.created_at.should == old_created_at
|
1035
|
+
from_db.updated_at.should_not == old_updated_at
|
1031
1036
|
end
|
1032
1037
|
end
|
1033
1038
|
|
@@ -74,6 +74,14 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
74
74
|
doc = RealPerson.find(person.id)
|
75
75
|
doc.pets.first.should == pet
|
76
76
|
end
|
77
|
+
|
78
|
+
should "save new keys" do
|
79
|
+
person = RealPerson.new
|
80
|
+
person[:new_attribute] = 'foobar'
|
81
|
+
person.save
|
82
|
+
from_db = RealPerson.find(person.id)
|
83
|
+
person.new_attribute.should == 'foobar'
|
84
|
+
end
|
77
85
|
end
|
78
86
|
|
79
87
|
context "update_attributes" do
|
@@ -94,4 +102,4 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
94
102
|
embedded.name.should == 'koda'
|
95
103
|
end
|
96
104
|
end
|
97
|
-
end
|
105
|
+
end
|
@@ -16,11 +16,6 @@ class TestRailsCompatibility < Test::Unit::TestCase
|
|
16
16
|
setup do
|
17
17
|
Order.collection.clear
|
18
18
|
end
|
19
|
-
|
20
|
-
should "have to_param that returns id" do
|
21
|
-
instance = Order.create('_id' => 1234)
|
22
|
-
instance.to_param.should == '1234'
|
23
|
-
end
|
24
19
|
|
25
20
|
should "alias new to new_record?" do
|
26
21
|
instance = Order.new
|
@@ -40,6 +40,26 @@ class ValidationsTest < Test::Unit::TestCase
|
|
40
40
|
lambda { doc.save! }.should raise_error(MongoMapper::DocumentNotValid)
|
41
41
|
end
|
42
42
|
end
|
43
|
+
|
44
|
+
context "Creating a document that is invalid (destructive)" do
|
45
|
+
setup do
|
46
|
+
@document = Class.new do
|
47
|
+
include MongoMapper::Document
|
48
|
+
set_collection_name 'test'
|
49
|
+
key :name, String, :required => true
|
50
|
+
end
|
51
|
+
@document.collection.clear
|
52
|
+
end
|
53
|
+
|
54
|
+
should "raise error" do
|
55
|
+
lambda { @document.create! }.should raise_error(MongoMapper::DocumentNotValid)
|
56
|
+
end
|
57
|
+
|
58
|
+
should "create a new document" do
|
59
|
+
instance = @document.create!(:name => "James")
|
60
|
+
instance.new_record?.should be_false
|
61
|
+
end
|
62
|
+
end
|
43
63
|
|
44
64
|
context "Saving an existing document that is invalid" do
|
45
65
|
setup do
|
data/test/models.rb
CHANGED
@@ -193,3 +193,27 @@ module TrModels
|
|
193
193
|
key :name, String
|
194
194
|
end
|
195
195
|
end
|
196
|
+
|
197
|
+
module Hollywood
|
198
|
+
class Movie
|
199
|
+
include MongoMapper::Document
|
200
|
+
|
201
|
+
many :roles
|
202
|
+
end
|
203
|
+
|
204
|
+
class Actor
|
205
|
+
include MongoMapper::Document
|
206
|
+
|
207
|
+
many :roles
|
208
|
+
end
|
209
|
+
|
210
|
+
class Role
|
211
|
+
include MongoMapper::Document
|
212
|
+
|
213
|
+
key :movie_id, String
|
214
|
+
key :actor_id, String
|
215
|
+
|
216
|
+
belongs_to :movie
|
217
|
+
belongs_to :actor
|
218
|
+
end
|
219
|
+
end
|
File without changes
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Test::Unit::TestCase
|
2
|
+
def run_with_test_timing(*args, &block)
|
3
|
+
begin_time = Time.now
|
4
|
+
run_without_test_timing(*args, &block)
|
5
|
+
end_time = Time.now
|
6
|
+
|
7
|
+
duration = end_time - begin_time
|
8
|
+
threshold = 0.3
|
9
|
+
|
10
|
+
if duration > threshold
|
11
|
+
puts "\nSLOW TEST: #{duration} - #{self.name}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method_chain :run, :test_timing unless method_defined?(:run_without_test_timing)
|
16
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
require '
|
2
|
-
require 'pp'
|
3
|
-
require 'rubygems'
|
4
|
-
require 'shoulda'
|
1
|
+
require File.join(File.expand_path(File.dirname(__FILE__) + '/../lib/mongo_mapper'))
|
5
2
|
|
6
|
-
gem 'mocha', '0.9.4'
|
7
3
|
gem 'jnunemaker-matchy', '0.4.0'
|
4
|
+
gem 'shoulda', '2.10.2'
|
5
|
+
gem 'timecop', '0.3.1'
|
6
|
+
gem 'mocha', '0.9.4'
|
8
7
|
|
9
8
|
require 'matchy'
|
9
|
+
require 'shoulda'
|
10
|
+
require 'timecop'
|
10
11
|
require 'mocha'
|
11
|
-
require '
|
12
|
+
require 'pp'
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
require dir + 'mongo_mapper'
|
14
|
+
require 'support/custom_matchers'
|
15
|
+
require 'support/test_timing'
|
16
16
|
|
17
17
|
class Test::Unit::TestCase
|
18
18
|
include CustomMatchers
|
@@ -289,6 +289,11 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
289
289
|
end
|
290
290
|
end
|
291
291
|
|
292
|
+
should "have to_param that is id" do
|
293
|
+
doc = @document.new
|
294
|
+
doc.to_param.should == doc.id
|
295
|
+
end
|
296
|
+
|
292
297
|
should "have access to class logger" do
|
293
298
|
doc = @document.new
|
294
299
|
doc.logger.should == @document.logger
|
@@ -475,9 +480,15 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
|
|
475
480
|
should "create key and write value for missing key" do
|
476
481
|
doc = @document.new
|
477
482
|
doc[:foo] = 'string'
|
478
|
-
|
483
|
+
doc.metaclass.keys.include?('foo').should be_true
|
479
484
|
doc[:foo].should == 'string'
|
480
485
|
end
|
486
|
+
|
487
|
+
should "not share the new key" do
|
488
|
+
doc = @document.new
|
489
|
+
doc[:foo] = 'string'
|
490
|
+
@document.keys.should_not include('foo')
|
491
|
+
end
|
481
492
|
end
|
482
493
|
end
|
483
494
|
|
@@ -15,11 +15,7 @@ class TestRailsCompatibility < Test::Unit::TestCase
|
|
15
15
|
key :second_only, String
|
16
16
|
end
|
17
17
|
|
18
|
-
context "EmbeddedDocument" do
|
19
|
-
should "raise error for to_param as embedded do not have id's" do
|
20
|
-
lambda { Item.new.to_param }.should raise_error
|
21
|
-
end
|
22
|
-
|
18
|
+
context "EmbeddedDocument" do
|
23
19
|
should "alias many to has_many" do
|
24
20
|
FirstItem.should respond_to(:has_many)
|
25
21
|
FirstItem.method(:has_many).should == FirstItem.method(:many)
|
@@ -1,52 +1,52 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class SerializationTest < Test::Unit::TestCase
|
4
|
-
def setup
|
5
|
-
@document = Class.new do
|
6
|
-
include MongoMapper::EmbeddedDocument
|
7
|
-
key :name, String
|
8
|
-
key :age, Integer
|
9
|
-
key :awesome, Boolean
|
10
|
-
key :preferences, Hash
|
11
|
-
key :created_at, Time
|
12
|
-
end
|
13
|
-
|
14
|
-
@instance = @document.new(
|
15
|
-
:name => 'John Doe',
|
16
|
-
:age => 25,
|
17
|
-
:awesome => true,
|
18
|
-
:preferences => {:language => 'Ruby'},
|
19
|
-
:created_at => Time.now.change(:usec => 0)
|
20
|
-
)
|
21
|
-
end
|
22
|
-
|
23
|
-
[:json].each do |format|
|
24
|
-
context format do
|
25
|
-
should "be reversable" do
|
26
|
-
serialized = @instance.send("to_#{format}")
|
27
|
-
unserialized = @document.new.send("from_#{format}", serialized)
|
28
|
-
|
29
|
-
assert_equal @instance, unserialized
|
30
|
-
end
|
31
|
-
|
32
|
-
should "allow attribute only filtering" do
|
33
|
-
serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
|
34
|
-
unserialized = @document.new.send("from_#{format}", serialized)
|
35
|
-
|
36
|
-
assert_equal @instance.name, unserialized.name
|
37
|
-
assert_equal @instance.age, unserialized.age
|
38
|
-
assert ! unserialized.awesome
|
39
|
-
assert_nil unserialized.created_at
|
40
|
-
end
|
41
|
-
|
42
|
-
should "allow attribute except filtering" do
|
43
|
-
serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
|
44
|
-
unserialized = @document.new.send("from_#{format}", serialized)
|
45
|
-
|
46
|
-
assert_nil unserialized.name
|
47
|
-
assert_nil unserialized.age
|
48
|
-
assert_equal @instance.awesome, unserialized.awesome
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SerializationTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@document = Class.new do
|
6
|
+
include MongoMapper::EmbeddedDocument
|
7
|
+
key :name, String
|
8
|
+
key :age, Integer
|
9
|
+
key :awesome, Boolean
|
10
|
+
key :preferences, Hash
|
11
|
+
key :created_at, Time
|
12
|
+
end
|
13
|
+
|
14
|
+
@instance = @document.new(
|
15
|
+
:name => 'John Doe',
|
16
|
+
:age => 25,
|
17
|
+
:awesome => true,
|
18
|
+
:preferences => {:language => 'Ruby'},
|
19
|
+
:created_at => Time.now.change(:usec => 0)
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
[:json].each do |format|
|
24
|
+
context format do
|
25
|
+
should "be reversable" do
|
26
|
+
serialized = @instance.send("to_#{format}")
|
27
|
+
unserialized = @document.new.send("from_#{format}", serialized)
|
28
|
+
|
29
|
+
assert_equal @instance, unserialized
|
30
|
+
end
|
31
|
+
|
32
|
+
should "allow attribute only filtering" do
|
33
|
+
serialized = @instance.send("to_#{format}", :only => [ :age, :name ])
|
34
|
+
unserialized = @document.new.send("from_#{format}", serialized)
|
35
|
+
|
36
|
+
assert_equal @instance.name, unserialized.name
|
37
|
+
assert_equal @instance.age, unserialized.age
|
38
|
+
assert ! unserialized.awesome
|
39
|
+
assert_nil unserialized.created_at
|
40
|
+
end
|
41
|
+
|
42
|
+
should "allow attribute except filtering" do
|
43
|
+
serialized = @instance.send("to_#{format}", :except => [ :age, :name ])
|
44
|
+
unserialized = @document.new.send("from_#{format}", serialized)
|
45
|
+
|
46
|
+
assert_nil unserialized.name
|
47
|
+
assert_nil unserialized.age
|
48
|
+
assert_equal @instance.awesome, unserialized.awesome
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -1,40 +1,40 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TimeZonesTest < Test::Unit::TestCase
|
4
|
-
context "An instance of an embedded document" do
|
5
|
-
setup do
|
6
|
-
@document = Class.new do
|
7
|
-
include MongoMapper::EmbeddedDocument
|
8
|
-
key :name, String
|
9
|
-
key :created_at, Time
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
should "work without Time.zone" do
|
14
|
-
Time.zone = nil
|
15
|
-
|
16
|
-
doc = @document.new(:created_at => "2009-08-15 14:00:00")
|
17
|
-
doc.created_at.should == Time.local(2009, 8, 15, 14, 0, 0).utc
|
18
|
-
end
|
19
|
-
|
20
|
-
should "work with Time.zone set to the (default) UTC" do
|
21
|
-
Time.zone = 'UTC'
|
22
|
-
|
23
|
-
doc = @document.new(:created_at => "2009-08-15 14:00:00")
|
24
|
-
doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
|
25
|
-
doc.created_at.should == Time.utc(2009, 8, 15, 14)
|
26
|
-
|
27
|
-
Time.zone = nil
|
28
|
-
end
|
29
|
-
|
30
|
-
should_eventually "work with timezones that are not UTC" do
|
31
|
-
Time.zone = 'Hawaii'
|
32
|
-
|
33
|
-
doc = @document.new(:created_at => @original_time)
|
34
|
-
doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
|
35
|
-
doc.created_at.should == Time.utc(2009, 8, 16)
|
36
|
-
|
37
|
-
Time.zone = nil
|
38
|
-
end
|
39
|
-
end
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TimeZonesTest < Test::Unit::TestCase
|
4
|
+
context "An instance of an embedded document" do
|
5
|
+
setup do
|
6
|
+
@document = Class.new do
|
7
|
+
include MongoMapper::EmbeddedDocument
|
8
|
+
key :name, String
|
9
|
+
key :created_at, Time
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
should "work without Time.zone" do
|
14
|
+
Time.zone = nil
|
15
|
+
|
16
|
+
doc = @document.new(:created_at => "2009-08-15 14:00:00")
|
17
|
+
doc.created_at.should == Time.local(2009, 8, 15, 14, 0, 0).utc
|
18
|
+
end
|
19
|
+
|
20
|
+
should "work with Time.zone set to the (default) UTC" do
|
21
|
+
Time.zone = 'UTC'
|
22
|
+
|
23
|
+
doc = @document.new(:created_at => "2009-08-15 14:00:00")
|
24
|
+
doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
|
25
|
+
doc.created_at.should == Time.utc(2009, 8, 15, 14)
|
26
|
+
|
27
|
+
Time.zone = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
should_eventually "work with timezones that are not UTC" do
|
31
|
+
Time.zone = 'Hawaii'
|
32
|
+
|
33
|
+
doc = @document.new(:created_at => @original_time)
|
34
|
+
doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
|
35
|
+
doc.created_at.should == Time.utc(2009, 8, 16)
|
36
|
+
|
37
|
+
Time.zone = nil
|
38
|
+
end
|
39
|
+
end
|
40
40
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: djsun-mongo_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Nunemaker
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-16 00:00:00 -04:00
|
13
13
|
default_executable: mmconsole
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: "
|
23
|
+
version: "2.3"
|
24
24
|
version:
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: mongo
|
@@ -43,24 +43,44 @@ dependencies:
|
|
43
43
|
version: 1.7.4
|
44
44
|
version:
|
45
45
|
- !ruby/object:Gem::Dependency
|
46
|
-
name:
|
46
|
+
name: jnunemaker-matchy
|
47
47
|
type: :development
|
48
48
|
version_requirement:
|
49
49
|
version_requirements: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
51
|
- - "="
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: 0.
|
53
|
+
version: 0.4.0
|
54
54
|
version:
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: shoulda
|
57
57
|
type: :development
|
58
58
|
version_requirement:
|
59
59
|
version_requirements: !ruby/object:Gem::Requirement
|
60
60
|
requirements:
|
61
61
|
- - "="
|
62
62
|
- !ruby/object:Gem::Version
|
63
|
-
version:
|
63
|
+
version: 2.10.2
|
64
|
+
version:
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: timecop
|
67
|
+
type: :development
|
68
|
+
version_requirement:
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 0.3.1
|
74
|
+
version:
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: mocha
|
77
|
+
type: :development
|
78
|
+
version_requirement:
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 0.9.4
|
64
84
|
version:
|
65
85
|
description:
|
66
86
|
email: nunemaker@gmail.com
|
@@ -102,7 +122,6 @@ files:
|
|
102
122
|
- lib/mongo_mapper/pagination.rb
|
103
123
|
- lib/mongo_mapper/rails_compatibility/document.rb
|
104
124
|
- lib/mongo_mapper/rails_compatibility/embedded_document.rb
|
105
|
-
- lib/mongo_mapper/save_with_validation.rb
|
106
125
|
- lib/mongo_mapper/serialization.rb
|
107
126
|
- lib/mongo_mapper/serializers/json_serializer.rb
|
108
127
|
- lib/mongo_mapper/support.rb
|
@@ -110,7 +129,6 @@ files:
|
|
110
129
|
- mongo_mapper.gemspec
|
111
130
|
- specs.watchr
|
112
131
|
- test/NOTE_ON_TESTING
|
113
|
-
- test/custom_matchers.rb
|
114
132
|
- test/functional/associations/test_belongs_to_polymorphic_proxy.rb
|
115
133
|
- test/functional/associations/test_belongs_to_proxy.rb
|
116
134
|
- test/functional/associations/test_many_documents_as_proxy.rb
|
@@ -118,6 +136,7 @@ files:
|
|
118
136
|
- test/functional/associations/test_many_embedded_proxy.rb
|
119
137
|
- test/functional/associations/test_many_polymorphic_proxy.rb
|
120
138
|
- test/functional/associations/test_many_proxy.rb
|
139
|
+
- test/functional/associations/test_namespace.rb
|
121
140
|
- test/functional/test_associations.rb
|
122
141
|
- test/functional/test_binary.rb
|
123
142
|
- test/functional/test_callbacks.rb
|
@@ -129,6 +148,8 @@ files:
|
|
129
148
|
- test/functional/test_rails_compatibility.rb
|
130
149
|
- test/functional/test_validations.rb
|
131
150
|
- test/models.rb
|
151
|
+
- test/support/custom_matchers.rb
|
152
|
+
- test/support/test_timing.rb
|
132
153
|
- test/test_helper.rb
|
133
154
|
- test/unit/serializers/test_json_serializer.rb
|
134
155
|
- test/unit/test_association_base.rb
|
@@ -174,7 +195,6 @@ signing_key:
|
|
174
195
|
specification_version: 3
|
175
196
|
summary: Awesome gem for modeling your domain and storing it in mongo
|
176
197
|
test_files:
|
177
|
-
- test/custom_matchers.rb
|
178
198
|
- test/functional/associations/test_belongs_to_polymorphic_proxy.rb
|
179
199
|
- test/functional/associations/test_belongs_to_proxy.rb
|
180
200
|
- test/functional/associations/test_many_documents_as_proxy.rb
|
@@ -182,6 +202,7 @@ test_files:
|
|
182
202
|
- test/functional/associations/test_many_embedded_proxy.rb
|
183
203
|
- test/functional/associations/test_many_polymorphic_proxy.rb
|
184
204
|
- test/functional/associations/test_many_proxy.rb
|
205
|
+
- test/functional/associations/test_namespace.rb
|
185
206
|
- test/functional/test_associations.rb
|
186
207
|
- test/functional/test_binary.rb
|
187
208
|
- test/functional/test_callbacks.rb
|
@@ -193,6 +214,8 @@ test_files:
|
|
193
214
|
- test/functional/test_rails_compatibility.rb
|
194
215
|
- test/functional/test_validations.rb
|
195
216
|
- test/models.rb
|
217
|
+
- test/support/custom_matchers.rb
|
218
|
+
- test/support/test_timing.rb
|
196
219
|
- test/test_helper.rb
|
197
220
|
- test/unit/serializers/test_json_serializer.rb
|
198
221
|
- test/unit/test_association_base.rb
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module MongoMapper
|
2
|
-
module SaveWithValidation
|
3
|
-
def self.included(base)
|
4
|
-
base.class_eval do
|
5
|
-
alias_method_chain :save, :validation
|
6
|
-
alias_method_chain :save!, :validation
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
private
|
11
|
-
def save_with_validation
|
12
|
-
valid? ? save_without_validation : false
|
13
|
-
end
|
14
|
-
|
15
|
-
def save_with_validation!
|
16
|
-
valid? ? save_without_validation! : raise(DocumentNotValid.new(self))
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|