mongo_mapper-unstable 2009.11.18 → 2009.12.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ Jeweler::Tasks.new do |gem|
12
12
  gem.authors = ["John Nunemaker"]
13
13
 
14
14
  gem.add_dependency('activesupport', '>= 2.3')
15
- gem.add_dependency('mongo', '0.17.1')
15
+ gem.add_dependency('mongo', '0.18')
16
16
  gem.add_dependency('jnunemaker-validatable', '1.8.1')
17
17
 
18
18
  gem.add_development_dependency('jnunemaker-matchy', '0.4.0')
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2009.11.18
1
+ 2009.12.04
data/lib/mongo_mapper.rb CHANGED
@@ -108,7 +108,6 @@ module MongoMapper
108
108
  end
109
109
 
110
110
  require 'mongo_mapper/support'
111
- require 'mongo_mapper/types'
112
111
  require 'mongo_mapper/associations'
113
112
  require 'mongo_mapper/associations/base'
114
113
  require 'mongo_mapper/associations/proxy'
@@ -16,7 +16,7 @@ module MongoMapper
16
16
  # TODO: test that both string and oid version work
17
17
  def find(id)
18
18
  load_target
19
- @target.detect { |item| item.id == id || item._id == id }
19
+ @target.detect { |item| item.id.to_s == id || item.id == id }
20
20
  end
21
21
 
22
22
  def <<(*docs)
@@ -219,7 +219,7 @@ module MongoMapper
219
219
  end
220
220
 
221
221
  def to_param
222
- id
222
+ id.to_s
223
223
  end
224
224
 
225
225
  def attributes=(attrs)
@@ -292,7 +292,7 @@ module MongoMapper
292
292
  end
293
293
 
294
294
  def id
295
- read_attribute(:_id).to_s
295
+ read_attribute(:_id)
296
296
  end
297
297
 
298
298
  def id=(value)
@@ -14,8 +14,7 @@ module MongoMapper
14
14
 
15
15
  def initialize(model, options)
16
16
  raise ArgumentError, "Options must be a hash" unless options.is_a?(Hash)
17
- options = options.clone
18
- options.symbolize_keys!
17
+ options = options.symbolize_keys
19
18
 
20
19
  @model = model
21
20
  @options = {}
@@ -41,12 +40,10 @@ module MongoMapper
41
40
 
42
41
  # @return [Hash] Mongo compatible options
43
42
  def options
44
- options = @options.dup
45
-
46
- fields = options.delete(:fields) || options.delete(:select)
47
- skip = options.delete(:skip) || options.delete(:offset) || 0
48
- limit = options.delete(:limit) || 0
49
- sort = options.delete(:sort) || convert_order_to_sort(options.delete(:order))
43
+ fields = @options.delete(:fields) || @options.delete(:select)
44
+ skip = @options.delete(:skip) || @options.delete(:offset) || 0
45
+ limit = @options.delete(:limit) || 0
46
+ sort = @options.delete(:sort) || convert_order_to_sort(@options.delete(:order))
50
47
 
51
48
  {:fields => to_mongo_fields(fields), :skip => skip.to_i, :limit => limit.to_i, :sort => sort}
52
49
  end
@@ -74,7 +71,6 @@ module MongoMapper
74
71
 
75
72
  case value
76
73
  when Array
77
- operator_present = field.to_s =~ /^\$/
78
74
  criteria[field] = operator?(field) ? value : {'$in' => value}
79
75
  when Hash
80
76
  criteria[field] = to_mongo_criteria(value, field)
@@ -15,6 +15,34 @@ class Array
15
15
  end
16
16
  end
17
17
 
18
+ class Binary
19
+ def self.to_mongo(value)
20
+ if value.is_a?(ByteBuffer)
21
+ value
22
+ else
23
+ value.nil? ? nil : ByteBuffer.new(value)
24
+ end
25
+ end
26
+
27
+ def self.from_mongo(value)
28
+ value
29
+ end
30
+ end
31
+
32
+ class Boolean
33
+ def self.to_mongo(value)
34
+ if value.is_a?(Boolean)
35
+ value
36
+ else
37
+ ['true', 't', '1'].include?(value.to_s.downcase)
38
+ end
39
+ end
40
+
41
+ def self.from_mongo(value)
42
+ !!value
43
+ end
44
+ end
45
+
18
46
  class Date
19
47
  def self.to_mongo(value)
20
48
  date = Date.parse(value.to_s)
@@ -94,6 +122,22 @@ class Object
94
122
  end
95
123
  end
96
124
 
125
+ class ObjectId
126
+ def self.to_mongo(value)
127
+ if value.nil?
128
+ nil
129
+ elsif value.is_a?(Mongo::ObjectID)
130
+ value
131
+ else
132
+ Mongo::ObjectID.from_string(value.to_s)
133
+ end
134
+ end
135
+
136
+ def self.from_mongo(value)
137
+ value
138
+ end
139
+ end
140
+
97
141
  class Set
98
142
  def self.to_mongo(value)
99
143
  value.to_a
@@ -139,4 +183,11 @@ class Time
139
183
  value
140
184
  end
141
185
  end
186
+ end
187
+
188
+ # TODO: Remove when patch accepted into driver
189
+ class Mongo::ObjectID
190
+ def to_json(options = nil)
191
+ to_s
192
+ end
142
193
  end
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.6.1"
8
+ s.version = "0.6.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-11-17}
12
+ s.date = %q{2009-12-01}
13
13
  s.default_executable = %q{mmconsole}
14
14
  s.email = %q{nunemaker@gmail.com}
15
15
  s.executables = ["mmconsole"]
@@ -50,7 +50,6 @@ Gem::Specification.new do |s|
50
50
  "lib/mongo_mapper/serialization.rb",
51
51
  "lib/mongo_mapper/serializers/json_serializer.rb",
52
52
  "lib/mongo_mapper/support.rb",
53
- "lib/mongo_mapper/types.rb",
54
53
  "lib/mongo_mapper/validations.rb",
55
54
  "mongo_mapper.gemspec",
56
55
  "specs.watchr",
@@ -144,7 +143,7 @@ Gem::Specification.new do |s|
144
143
 
145
144
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
146
145
  s.add_runtime_dependency(%q<activesupport>, [">= 2.3"])
147
- s.add_runtime_dependency(%q<mongo>, ["= 0.17.1"])
146
+ s.add_runtime_dependency(%q<mongo>, ["= 0.18"])
148
147
  s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
149
148
  s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
150
149
  s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -152,7 +151,7 @@ Gem::Specification.new do |s|
152
151
  s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
153
152
  else
154
153
  s.add_dependency(%q<activesupport>, [">= 2.3"])
155
- s.add_dependency(%q<mongo>, ["= 0.17.1"])
154
+ s.add_dependency(%q<mongo>, ["= 0.18"])
156
155
  s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
157
156
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
158
157
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -161,7 +160,7 @@ Gem::Specification.new do |s|
161
160
  end
162
161
  else
163
162
  s.add_dependency(%q<activesupport>, [">= 2.3"])
164
- s.add_dependency(%q<mongo>, ["= 0.17.1"])
163
+ s.add_dependency(%q<mongo>, ["= 0.18"])
165
164
  s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.1"])
166
165
  s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
167
166
  s.add_dependency(%q<shoulda>, ["= 2.10.2"])
@@ -138,8 +138,10 @@ class ManyEmbeddedProxyTest < Test::Unit::TestCase
138
138
 
139
139
  should "allow finding by id" do
140
140
  sparky = Pet.new(:name => "Sparky", :species => "Dog")
141
- meg = Person.new(:name => "Meg", :pets => [sparky])
142
- meg.pets.find(sparky._id).should == sparky
141
+ meg = Person.new(:name => "Meg", :pets => [sparky])
142
+
143
+ meg.pets.find(sparky._id).should == sparky # oid
144
+ meg.pets.find(sparky.id.to_s).should == sparky # string
143
145
  end
144
146
 
145
147
  context "extending the association" do
@@ -365,7 +365,11 @@ class ManyProxyTest < Test::Unit::TestCase
365
365
  status3 = Status.new(:name => "Closed")
366
366
  project.statuses = [status1, status2, status3]
367
367
  project.save
368
- project.statuses.open.should == [status1, status2]
368
+
369
+ open_statuses = project.statuses.open
370
+ open_statuses.should include(status1)
371
+ open_statuses.should include(status2)
372
+ open_statuses.should_not include(status3)
369
373
  end
370
374
 
371
375
  should "work using many's :extend option" do
@@ -172,9 +172,7 @@ class DocumentTest < Test::Unit::TestCase
172
172
  end
173
173
 
174
174
  should "automatically set id" do
175
- @doc_instance.id.should_not be_nil
176
- @doc_instance.id.size.should == 24
177
- @doc_instance.id.should be_instance_of(String)
175
+ @doc_instance.id.should be_instance_of(Mongo::ObjectID)
178
176
  @doc_instance._id.should be_instance_of(Mongo::ObjectID)
179
177
  end
180
178
 
@@ -777,8 +775,7 @@ class DocumentTest < Test::Unit::TestCase
777
775
  end
778
776
 
779
777
  should "assign an id for the document" do
780
- @doc.id.should_not be(nil)
781
- @doc.id.size.should == 24
778
+ @doc.id.should be_instance_of(Mongo::ObjectID)
782
779
  end
783
780
 
784
781
  should "save attributes" do
@@ -858,8 +855,7 @@ class DocumentTest < Test::Unit::TestCase
858
855
  end
859
856
 
860
857
  should "assign an id for the document" do
861
- @doc.id.should_not be(nil)
862
- @doc.id.size.should == 24
858
+ @doc.id.should be_instance_of(Mongo::ObjectID)
863
859
  end
864
860
 
865
861
  should "save attributes" do
@@ -307,9 +307,9 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
307
307
  end
308
308
  end
309
309
 
310
- should "have to_param that is id" do
310
+ should "have to_param that is string representation of id" do
311
311
  doc = @document.new
312
- doc.to_param.should == doc.id
312
+ doc.to_param.should == doc.id.to_s
313
313
  doc.to_param.should be_instance_of(String)
314
314
  end
315
315
 
@@ -323,9 +323,10 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
323
323
  @document.keys.keys.should include('_id')
324
324
  end
325
325
 
326
- should "have id method that sets _id" do
327
- doc = @document.new
328
- doc.id.should == doc._id.to_s
326
+ should "have id method returns _id" do
327
+ id = Mongo::ObjectID.new
328
+ doc = @document.new(:_id => id)
329
+ doc.id.should == id
329
330
  end
330
331
 
331
332
  context "assigning id with _id ObjectId type" do
@@ -338,18 +339,13 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
338
339
 
339
340
  should "convert string object id to mongo object id" do
340
341
  id = Mongo::ObjectID.new
341
- doc = @document.new
342
- doc.id = id.to_s
342
+ doc = @document.new(:id => id.to_s)
343
343
  doc._id.should == id
344
- doc.id.should == id.to_s
344
+ doc.id.should == id
345
345
  doc.using_custom_id?.should be_false
346
346
  end
347
347
  end
348
348
 
349
- should "have a nil _root_document" do
350
- @document.new._root_document.should be_nil
351
- end
352
-
353
349
  context "setting custom id" do
354
350
  should "set _id" do
355
351
  @document.key :_id, String
@@ -366,6 +362,10 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
366
362
  end
367
363
  end
368
364
 
365
+ should "have a nil _root_document" do
366
+ @document.new._root_document.should be_nil
367
+ end
368
+
369
369
  context "being initialized" do
370
370
  should "accept a hash that sets keys and values" do
371
371
  doc = @document.new(:name => 'John', :age => 23)
@@ -1,8 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class SupportTest < Test::Unit::TestCase
4
- include MongoMapper::Types
5
-
6
4
  context "Array#to_mongo" do
7
5
  should "convert value to_a" do
8
6
  Array.to_mongo([1, 2, 3, 4]).should == [1, 2, 3, 4]
@@ -333,4 +331,12 @@ class SupportTest < Test::Unit::TestCase
333
331
  Time.zone = nil
334
332
  end
335
333
  end
334
+
335
+ context "Mongo::ObjectID.to_json" do
336
+ should "convert object id to string" do
337
+ id = Mongo::ObjectID.new
338
+ id.to_json.should == id.to_s
339
+ end
340
+ end
341
+
336
342
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper-unstable
3
3
  version: !ruby/object:Gem::Version
4
- version: 2009.11.18
4
+ version: 2009.12.4
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-11-18 00:00:00 -05:00
12
+ date: 2009-12-04 00:00:00 -05:00
13
13
  default_executable: mmconsole
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - "="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.17.1
33
+ version: "0.18"
34
34
  version:
35
35
  - !ruby/object:Gem::Dependency
36
36
  name: jnunemaker-validatable
@@ -124,7 +124,6 @@ files:
124
124
  - lib/mongo_mapper/serialization.rb
125
125
  - lib/mongo_mapper/serializers/json_serializer.rb
126
126
  - lib/mongo_mapper/support.rb
127
- - lib/mongo_mapper/types.rb
128
127
  - lib/mongo_mapper/validations.rb
129
128
  - mongo_mapper.gemspec
130
129
  - specs.watchr
@@ -1,64 +0,0 @@
1
- module MongoMapper
2
- module Types
3
- class Binary
4
- def self.to_mongo(value)
5
- if value.is_a?(ByteBuffer)
6
- value
7
- else
8
- value.nil? ? nil : ByteBuffer.new(value)
9
- end
10
- end
11
-
12
- def self.from_mongo(value)
13
- value
14
- end
15
- end
16
-
17
- class Boolean
18
- def self.to_mongo(value)
19
- if value.is_a?(Boolean)
20
- value
21
- else
22
- ['true', 't', '1'].include?(value.to_s.downcase)
23
- end
24
- end
25
-
26
- def self.from_mongo(value)
27
- !!value
28
- end
29
- end
30
-
31
- class ObjectId
32
- def self.to_mongo(value)
33
- if value.nil?
34
- nil
35
- elsif value.is_a?(Mongo::ObjectID)
36
- value
37
- else
38
- Mongo::ObjectID.from_string(value.to_s)
39
- end
40
- end
41
-
42
- def self.from_mongo(value)
43
- value
44
- end
45
- end
46
-
47
- # This allows using just Boolean when defining
48
- # keys instead of MongoMapper::Types::Boolean
49
- module Lookup
50
- def const_missing(name)
51
- if MongoMapper::Types.const_defined?(name)
52
- MongoMapper::Types.const_get(name)
53
- else
54
- super
55
- end
56
- end
57
- end
58
- end
59
- end
60
-
61
- # This was required to get in front of ActiveSupports Class#const_missing
62
- Class.instance_eval do
63
- include MongoMapper::Types::Lookup
64
- end