mongo_mapper 0.7.2 → 0.7.3

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 CHANGED
@@ -13,7 +13,7 @@ Jeweler::Tasks.new do |gem|
13
13
  gem.version = MongoMapper::Version
14
14
 
15
15
  gem.add_dependency('activesupport', '>= 2.3.4')
16
- gem.add_dependency('mongo', '0.19.1')
16
+ gem.add_dependency('mongo', '0.19.3')
17
17
  gem.add_dependency('jnunemaker-validatable', '1.8.3')
18
18
 
19
19
  gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
@@ -32,21 +32,5 @@ Rake::TestTask.new(:test) do |test|
32
32
  test.verbose = true
33
33
  end
34
34
 
35
- namespace :test do
36
- Rake::TestTask.new(:units) do |test|
37
- test.libs << 'test'
38
- test.ruby_opts << '-rubygems'
39
- test.pattern = 'test/unit/**/test_*.rb'
40
- test.verbose = true
41
- end
42
-
43
- Rake::TestTask.new(:functionals) do |test|
44
- test.libs << 'test'
45
- test.ruby_opts << '-rubygems'
46
- test.pattern = 'test/functional/**/test_*.rb'
47
- test.verbose = true
48
- end
49
- end
50
-
51
35
  task :default => :test
52
36
  task :test => :check_dependencies
@@ -1,19 +1,13 @@
1
+ # Make sure you have the following libs in your load path or you could have issues:
2
+ # gem 'activesupport', '>= 2.3.4'
3
+ # gem 'mongo', '0.19.3'
4
+ # gem 'jnunemaker-validatable', '1.8.3'
5
+ # gem 'activesupport', '= 2.3.4'
1
6
  require 'set'
2
7
  require 'uri'
3
-
4
- # if Gem is defined i'll assume you are using rubygems and lock specific versions
5
- # call me crazy but a plain old require will just get the latest version you have installed
6
- # so i want to make sure that if you are using gems you do in fact have the correct versions
7
- # if there is a better way to do this, please enlighten me!
8
- if self.class.const_defined?(:Gem)
9
- gem 'activesupport', '>= 2.3.4'
10
- gem 'mongo', '0.19.1'
11
- gem 'jnunemaker-validatable', '1.8.3'
12
- end
13
-
14
- require 'active_support/all'
15
8
  require 'mongo'
16
9
  require 'validatable'
10
+ require 'active_support/all'
17
11
 
18
12
  module MongoMapper
19
13
  # generic MM error
@@ -113,21 +107,6 @@ module MongoMapper
113
107
  end
114
108
  end
115
109
 
116
- # @api private
117
- def self.use_time_zone?
118
- Time.respond_to?(:zone) && Time.zone ? true : false
119
- end
120
-
121
- # @api private
122
- def self.time_class
123
- use_time_zone? ? Time.zone : Time
124
- end
125
-
126
- # @api private
127
- def self.normalize_object_id(value)
128
- value.is_a?(String) ? Mongo::ObjectID.from_string(value) : value
129
- end
130
-
131
110
  autoload :Query, 'mongo_mapper/query'
132
111
  autoload :Document, 'mongo_mapper/document'
133
112
  autoload :EmbeddedDocument, 'mongo_mapper/embedded_document'
@@ -19,6 +19,7 @@ module MongoMapper
19
19
  plugin Plugins::Logger
20
20
  plugin Plugins::Modifiers
21
21
  plugin Plugins::Pagination
22
+ plugin Plugins::Persistence
22
23
  plugin Plugins::Protected
23
24
  plugin Plugins::Rails
24
25
  plugin Plugins::Serialization
@@ -39,14 +40,8 @@ module MongoMapper
39
40
  super
40
41
  end
41
42
 
42
- def ensure_index(name_or_array, options={})
43
- keys_to_index = if name_or_array.is_a?(Array)
44
- name_or_array.map { |pair| [pair[0], pair[1]] }
45
- else
46
- name_or_array
47
- end
48
-
49
- collection.create_index(keys_to_index, options[:unique])
43
+ def ensure_index(spec, options={})
44
+ collection.create_index(spec, options)
50
45
  end
51
46
 
52
47
  def find(*args)
@@ -150,43 +145,6 @@ module MongoMapper
150
145
  false
151
146
  end
152
147
 
153
- def connection(mongo_connection=nil)
154
- if mongo_connection.nil?
155
- @connection ||= MongoMapper.connection
156
- else
157
- @connection = mongo_connection
158
- end
159
- @connection
160
- end
161
-
162
- def set_database_name(name)
163
- @database_name = name
164
- end
165
-
166
- def database_name
167
- @database_name
168
- end
169
-
170
- def database
171
- if database_name.nil?
172
- MongoMapper.database
173
- else
174
- connection.db(database_name)
175
- end
176
- end
177
-
178
- def set_collection_name(name)
179
- @collection_name = name
180
- end
181
-
182
- def collection_name
183
- @collection_name ||= self.to_s.tableize.gsub(/\//, '.')
184
- end
185
-
186
- def collection
187
- database.collection(collection_name)
188
- end
189
-
190
148
  def single_collection_inherited?
191
149
  keys.key?(:_type) && single_collection_inherited_superclass?
192
150
  end
@@ -287,14 +245,6 @@ module MongoMapper
287
245
  end
288
246
 
289
247
  module InstanceMethods
290
- def collection
291
- self.class.collection
292
- end
293
-
294
- def database
295
- self.class.database
296
- end
297
-
298
248
  def save(options={})
299
249
  options.assert_valid_keys(:validate, :safe)
300
250
  options.reverse_merge!(:validate => true)
@@ -15,6 +15,7 @@ module MongoMapper
15
15
  plugin Plugins::Inspect
16
16
  plugin Plugins::Keys
17
17
  plugin Plugins::Logger
18
+ plugin Plugins::Persistence
18
19
  plugin Plugins::Protected
19
20
  plugin Plugins::Rails
20
21
  plugin Plugins::Serialization
@@ -21,6 +21,7 @@ module MongoMapper
21
21
  autoload :Keys, 'mongo_mapper/plugins/keys'
22
22
  autoload :Logger, 'mongo_mapper/plugins/logger'
23
23
  autoload :Modifiers, 'mongo_mapper/plugins/modifiers'
24
+ autoload :Persistence, 'mongo_mapper/plugins/persistence'
24
25
  autoload :Protected, 'mongo_mapper/plugins/protected'
25
26
  autoload :Rails, 'mongo_mapper/plugins/rails'
26
27
  autoload :Serialization, 'mongo_mapper/plugins/serialization'
@@ -7,6 +7,7 @@ module MongoMapper
7
7
 
8
8
  module ClassMethods
9
9
  def inherited(descendant)
10
+ key :_type, String unless keys.keys.include?(:_type)
10
11
  descendant.instance_variable_set(:@keys, keys.dup)
11
12
  super
12
13
  end
@@ -147,7 +148,6 @@ module MongoMapper
147
148
  module InstanceMethods
148
149
  def initialize(attrs={}, from_database=false)
149
150
  default_id_value(attrs)
150
- assign_type
151
151
 
152
152
  if from_database
153
153
  @new = false
@@ -156,6 +156,8 @@ module MongoMapper
156
156
  @new = true
157
157
  assign(attrs)
158
158
  end
159
+
160
+ assign_type
159
161
  end
160
162
 
161
163
  def persisted?
@@ -214,7 +216,7 @@ module MongoMapper
214
216
 
215
217
  def id=(value)
216
218
  if self.class.using_object_id?
217
- value = MongoMapper.normalize_object_id(value)
219
+ value = ObjectId.to_mongo(value)
218
220
  end
219
221
 
220
222
  self[:_id] = value
@@ -0,0 +1,68 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module Persistence
4
+ module ClassMethods
5
+ class Unsupported < MongoMapperError; end
6
+
7
+ def connection(mongo_connection=nil)
8
+ not_supported_by_embedded
9
+ if mongo_connection.nil?
10
+ @connection ||= MongoMapper.connection
11
+ else
12
+ @connection = mongo_connection
13
+ end
14
+ @connection
15
+ end
16
+
17
+ def set_database_name(name)
18
+ not_supported_by_embedded
19
+ @database_name = name
20
+ end
21
+
22
+ def database_name
23
+ not_supported_by_embedded
24
+ @database_name
25
+ end
26
+
27
+ def database
28
+ not_supported_by_embedded
29
+ if database_name.nil?
30
+ MongoMapper.database
31
+ else
32
+ connection.db(database_name)
33
+ end
34
+ end
35
+
36
+ def set_collection_name(name)
37
+ not_supported_by_embedded
38
+ @collection_name = name
39
+ end
40
+
41
+ def collection_name
42
+ not_supported_by_embedded
43
+ @collection_name ||= self.to_s.tableize.gsub(/\//, '.')
44
+ end
45
+
46
+ def collection
47
+ not_supported_by_embedded
48
+ database.collection(collection_name)
49
+ end
50
+
51
+ private
52
+ def not_supported_by_embedded
53
+ raise Unsupported.new('This is not supported for embeddable documents at this time.') if embeddable?
54
+ end
55
+ end
56
+
57
+ module InstanceMethods
58
+ def collection
59
+ _root_document.class.collection
60
+ end
61
+
62
+ def database
63
+ _root_document.class.database
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -182,14 +182,15 @@ class Time
182
182
  if value.nil? || value == ''
183
183
  nil
184
184
  else
185
- time = value.is_a?(Time) ? value : MongoMapper.time_class.parse(value.to_s)
185
+ time_class = Time.try(:zone).present? ? Time.zone : Time
186
+ time = value.is_a?(Time) ? value : time_class.parse(value.to_s)
186
187
  # Convert time to milliseconds since BSON stores dates with that accurracy, but Ruby uses microseconds
187
188
  Time.at((time.to_f * 1000).round / 1000.0).utc if time
188
189
  end
189
190
  end
190
191
 
191
192
  def self.from_mongo(value)
192
- if MongoMapper.use_time_zone? && value.present?
193
+ if Time.try(:zone).present? && value.present?
193
194
  value.in_time_zone(Time.zone)
194
195
  else
195
196
  value
@@ -1,3 +1,3 @@
1
1
  module MongoMapper
2
- Version = '0.7.2'
2
+ Version = '0.7.3'
3
3
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongo_mapper}
8
- s.version = "0.7.2"
8
+ s.version = "0.7.3"
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{2010-03-26}
12
+ s.date = %q{2010-04-05}
13
13
  s.default_executable = %q{mmconsole}
14
14
  s.email = %q{nunemaker@gmail.com}
15
15
  s.executables = ["mmconsole"]
@@ -53,6 +53,7 @@ Gem::Specification.new do |s|
53
53
  "lib/mongo_mapper/plugins/modifiers.rb",
54
54
  "lib/mongo_mapper/plugins/pagination.rb",
55
55
  "lib/mongo_mapper/plugins/pagination/proxy.rb",
56
+ "lib/mongo_mapper/plugins/persistence.rb",
56
57
  "lib/mongo_mapper/plugins/protected.rb",
57
58
  "lib/mongo_mapper/plugins/rails.rb",
58
59
  "lib/mongo_mapper/plugins/serialization.rb",
@@ -179,7 +180,7 @@ Gem::Specification.new do |s|
179
180
 
180
181
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
181
182
  s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
182
- s.add_runtime_dependency(%q<mongo>, ["= 0.19.1"])
183
+ s.add_runtime_dependency(%q<mongo>, ["= 0.19.3"])
183
184
  s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.3"])
184
185
  s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
185
186
  s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -187,7 +188,7 @@ Gem::Specification.new do |s|
187
188
  s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
188
189
  else
189
190
  s.add_dependency(%q<activesupport>, [">= 2.3.4"])
190
- s.add_dependency(%q<mongo>, ["= 0.19.1"])
191
+ s.add_dependency(%q<mongo>, ["= 0.19.3"])
191
192
  s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.3"])
192
193
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
193
194
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -196,7 +197,7 @@ Gem::Specification.new do |s|
196
197
  end
197
198
  else
198
199
  s.add_dependency(%q<activesupport>, [">= 2.3.4"])
199
- s.add_dependency(%q<mongo>, ["= 0.19.1"])
200
+ s.add_dependency(%q<mongo>, ["= 0.19.3"])
200
201
  s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.3"])
201
202
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
202
203
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -942,7 +942,6 @@ class DocumentTest < Test::Unit::TestCase
942
942
  setup do
943
943
  class ::DocParent
944
944
  include MongoMapper::Document
945
- key :_type, String
946
945
  key :name, String
947
946
  end
948
947
  DocParent.collection.remove
@@ -964,6 +963,10 @@ class DocumentTest < Test::Unit::TestCase
964
963
  Object.send :remove_const, 'DocGrandSon' if defined?(::DocGrandSon)
965
964
  end
966
965
 
966
+ should "automatically add _type key to store class" do
967
+ DocParent.keys.should include(:_type)
968
+ end
969
+
967
970
  should "use the same collection in the subclass" do
968
971
  DocDaughter.collection.name.should == DocParent.collection.name
969
972
  end
@@ -1091,6 +1094,11 @@ class DocumentTest < Test::Unit::TestCase
1091
1094
  }.should change { DocParent.count }.by(-2)
1092
1095
  end
1093
1096
 
1097
+ should "set type from class and ignore _type in attributes" do
1098
+ doc = DocSon.create(:_type => 'DocDaughter', :name => 'John')
1099
+ DocParent.first.should be_instance_of(DocSon)
1100
+ end
1101
+
1094
1102
  should "be able to reload parent inherited class" do
1095
1103
  brian = DocParent.create(:name => 'Brian')
1096
1104
  brian.name = 'B-Dawg'
@@ -126,7 +126,7 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
126
126
  @doc = @klass.new(:name => 'persisted doc', :pets => [@pet_klass.new(:name => 'persisted pet')])
127
127
  end
128
128
 
129
- should_eventually "be false if new" do
129
+ should "be false if new" do
130
130
  @doc.pets.first.should_not be_persisted
131
131
  end
132
132
 
@@ -189,4 +189,14 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
189
189
  pet.expects(:save!)
190
190
  pet.update_attributes!(attributes)
191
191
  end
192
+
193
+ should "have database instance method that is equal to root document" do
194
+ person = @klass.create(:pets => [@pet_klass.new(:name => 'sparky')])
195
+ person.pets.first.database.should == person.database
196
+ end
197
+
198
+ should "have collection instance method that is equal to root document" do
199
+ person = @klass.create(:pets => [@pet_klass.new(:name => 'sparky')])
200
+ person.pets.first.collection.name.should == person.collection.name
201
+ end
192
202
  end
@@ -360,7 +360,6 @@ class IdentityMapTest < Test::Unit::TestCase
360
360
  include MongoMapper::Document
361
361
  plugin MongoMapper::Plugins::IdentityMap
362
362
 
363
- key :_type, String
364
363
  key :title, String
365
364
  key :parent_id, ObjectId
366
365
 
@@ -31,8 +31,8 @@ class IndexingTest < Test::Unit::TestCase
31
31
  # just checking have_index('first_name_1_last_name_-1') I'm checking
32
32
  # the values of the indexes to make sure the index creation was successful
33
33
  @document.collection.index_information.detect do |index|
34
- keys = index[1]
35
- keys.include?(['first_name', 1]) && keys.include?(['last_name', -1])
34
+ keys = index[0]
35
+ keys.include?('first_name_1') && keys.include?('last_name_-1')
36
36
  end.should_not be_nil
37
37
  end
38
38
 
@@ -76,9 +76,7 @@ class ProtectedTest < Test::Unit::TestCase
76
76
  class ::GrandParent
77
77
  include MongoMapper::Document
78
78
 
79
- key :_type, String
80
79
  key :site_id, ObjectId
81
-
82
80
  attr_protected :site_id
83
81
  end
84
82
  GrandParent.collection.remove
@@ -67,7 +67,6 @@ class Message
67
67
 
68
68
  key :body, String
69
69
  key :position, Integer
70
- key :_type, String
71
70
  key :room_id, ObjectId
72
71
 
73
72
  belongs_to :room
@@ -94,7 +93,6 @@ end
94
93
  class Account
95
94
  include MongoMapper::Document
96
95
 
97
- key :_type, String
98
96
  key :room_id, ObjectId
99
97
  key :last_logged_in, Time
100
98
 
@@ -158,9 +156,7 @@ end
158
156
  class Media
159
157
  include MongoMapper::EmbeddedDocument
160
158
 
161
- key :_type, String
162
159
  key :file, String
163
-
164
160
  key :visible, Boolean
165
161
  end
166
162
 
@@ -192,7 +188,6 @@ module TrModels
192
188
  class Transport
193
189
  include MongoMapper::EmbeddedDocument
194
190
 
195
- key :_type, String
196
191
  key :license_plate, String
197
192
  key :purchased_on, Date
198
193
  end
@@ -179,17 +179,17 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
179
179
 
180
180
  context "keys" do
181
181
  should "be inherited" do
182
- Grandparent.keys.keys.sort.should == ['_id', 'grandparent']
183
- Parent.keys.keys.sort.should == ['_id', 'grandparent', 'parent']
184
- Child.keys.keys.sort.should == ['_id', 'child', 'grandparent', 'parent']
182
+ Grandparent.keys.keys.sort.should == ['_id', '_type', 'grandparent']
183
+ Parent.keys.keys.sort.should == ['_id', '_type', 'grandparent', 'parent']
184
+ Child.keys.keys.sort.should == ['_id', '_type', 'child', 'grandparent', 'parent']
185
185
  end
186
186
 
187
187
  should "propogate to descendants if key added after class definition" do
188
- Grandparent.key :_type, String
188
+ Grandparent.key :foo, String
189
189
 
190
- Grandparent.keys.keys.sort.should == ['_id', '_type', 'grandparent']
191
- Parent.keys.keys.sort.should == ['_id', '_type', 'grandparent', 'parent']
192
- Child.keys.keys.sort.should == ['_id', '_type', 'child', 'grandparent', 'parent']
190
+ Grandparent.keys.keys.sort.should == ['_id', '_type', 'foo', 'grandparent']
191
+ Parent.keys.keys.sort.should == ['_id', '_type', 'foo', 'grandparent', 'parent']
192
+ Child.keys.keys.sort.should == ['_id', '_type', 'child', 'foo', 'grandparent', 'parent']
193
193
  end
194
194
 
195
195
  should "not add anonymous objects to the ancestor tree" do
@@ -302,8 +302,8 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
302
302
  @klass.new._type.should == 'FooBar'
303
303
  end
304
304
 
305
- should "not change _type if already set" do
306
- @klass.new(:_type => 'Foo')._type.should == 'Foo'
305
+ should "ignore _type attribute and always use class" do
306
+ @klass.new(:_type => 'Foo')._type.should == 'FooBar'
307
307
  end
308
308
  end
309
309
 
@@ -115,41 +115,4 @@ class MongoMapperTest < Test::Unit::TestCase
115
115
  MongoMapper.setup(config, 'development', :logger => logger, :passenger => true)
116
116
  end
117
117
  end
118
-
119
-
120
- context "use_time_zone?" do
121
- should "be true if Time.zone set" do
122
- Time.zone = 'Hawaii'
123
- MongoMapper.use_time_zone?.should be_true
124
- Time.zone = nil
125
- end
126
-
127
- should "be false if Time.zone not set" do
128
- MongoMapper.use_time_zone?.should be_false
129
- end
130
- end
131
-
132
- context "time_class" do
133
- should "be Time.zone if using time zones" do
134
- Time.zone = 'Hawaii'
135
- MongoMapper.time_class.should == Time.zone
136
- Time.zone = nil
137
- end
138
-
139
- should "be Time if not using time zones" do
140
- MongoMapper.time_class.should == Time
141
- end
142
- end
143
-
144
- context "normalize_object_id" do
145
- should "turn string into object id" do
146
- id = Mongo::ObjectID.new
147
- MongoMapper.normalize_object_id(id.to_s).should == id
148
- end
149
-
150
- should "leave object id alone" do
151
- id = Mongo::ObjectID.new
152
- MongoMapper.normalize_object_id(id).should == id
153
- end
154
- end
155
118
  end
@@ -29,9 +29,9 @@ class TestRailsCompatibility < Test::Unit::TestCase
29
29
  end
30
30
 
31
31
  should "have column names" do
32
- Item.column_names.sort.should == ['_id', 'for_all']
33
- FirstItem.column_names.sort.should == ['_id', 'first_only', 'for_all']
34
- SecondItem.column_names.sort.should == ['_id', 'for_all', 'second_only']
32
+ Item.column_names.sort.should == ['_id', '_type', 'for_all']
33
+ FirstItem.column_names.sort.should == ['_id', '_type', 'first_only', 'for_all']
34
+ SecondItem.column_names.sort.should == ['_id', '_type', 'for_all', 'second_only']
35
35
  end
36
36
 
37
37
  should "alias new to new_record?" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 7
8
- - 2
9
- version: 0.7.2
8
+ - 3
9
+ version: 0.7.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Nunemaker
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-26 00:00:00 -04:00
17
+ date: 2010-04-05 00:00:00 -04:00
18
18
  default_executable: mmconsole
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -41,8 +41,8 @@ dependencies:
41
41
  segments:
42
42
  - 0
43
43
  - 19
44
- - 1
45
- version: 0.19.1
44
+ - 3
45
+ version: 0.19.3
46
46
  type: :runtime
47
47
  version_requirements: *id002
48
48
  - !ruby/object:Gem::Dependency
@@ -160,6 +160,7 @@ files:
160
160
  - lib/mongo_mapper/plugins/modifiers.rb
161
161
  - lib/mongo_mapper/plugins/pagination.rb
162
162
  - lib/mongo_mapper/plugins/pagination/proxy.rb
163
+ - lib/mongo_mapper/plugins/persistence.rb
163
164
  - lib/mongo_mapper/plugins/protected.rb
164
165
  - lib/mongo_mapper/plugins/rails.rb
165
166
  - lib/mongo_mapper/plugins/serialization.rb