mongo_mapper-unstable 2010.08.08 → 2010.08.09
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongo_mapper/plugins/keys/key.rb +4 -0
- data/lib/mongo_mapper/plugins/keys.rb +7 -2
- data/lib/mongo_mapper/version.rb +1 -1
- data/test/test_helper.rb +3 -2
- data/test/unit/test_keys.rb +34 -0
- metadata +28 -18
@@ -44,6 +44,11 @@ module MongoMapper
|
|
44
44
|
object_id_keys.include?(name.to_sym)
|
45
45
|
end
|
46
46
|
|
47
|
+
# API Private
|
48
|
+
def can_default_id?
|
49
|
+
keys['_id'].can_default_id?
|
50
|
+
end
|
51
|
+
|
47
52
|
def to_mongo(instance)
|
48
53
|
return nil if instance.nil?
|
49
54
|
instance.to_mongo
|
@@ -268,8 +273,8 @@ module MongoMapper
|
|
268
273
|
|
269
274
|
def default_id_value(attrs)
|
270
275
|
unless attrs.nil?
|
271
|
-
|
272
|
-
|
276
|
+
id_provided = attrs.keys.map { |k| k.to_s }.detect { |k| k == 'id' || k == '_id' }
|
277
|
+
if !id_provided && self.class.can_default_id?
|
273
278
|
write_key :_id, BSON::ObjectID.new
|
274
279
|
end
|
275
280
|
end
|
data/lib/mongo_mapper/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
gem 'activesupport', ENV['ACTIVE_SUPPORT_VERSION']
|
3
|
-
gem 'json'
|
3
|
+
gem 'json'
|
4
4
|
gem 'jnunemaker-matchy', '~> 0.4.0'
|
5
5
|
gem 'shoulda', '~> 2.10.2'
|
6
6
|
gem 'timecop', '~> 0.3.1'
|
@@ -10,10 +10,10 @@ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
|
10
10
|
require 'mongo_mapper'
|
11
11
|
require 'fileutils'
|
12
12
|
require 'ostruct'
|
13
|
-
require 'pp'
|
14
13
|
|
15
14
|
require 'active_support/version'
|
16
15
|
require 'json'
|
16
|
+
require 'log_buddy'
|
17
17
|
require 'matchy'
|
18
18
|
require 'shoulda'
|
19
19
|
require 'timecop'
|
@@ -97,6 +97,7 @@ log_dir = File.expand_path('../../log', __FILE__)
|
|
97
97
|
FileUtils.mkdir_p(log_dir) unless File.exist?(log_dir)
|
98
98
|
logger = Logger.new(log_dir + '/test.log')
|
99
99
|
|
100
|
+
LogBuddy.init(:logger => logger)
|
100
101
|
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => logger)
|
101
102
|
MongoMapper.database = "mm-test-#{RUBY_VERSION.gsub('.', '-')}"
|
102
103
|
MongoMapper.database.collections.each { |c| c.drop_indexes }
|
data/test/unit/test_keys.rb
CHANGED
@@ -4,6 +4,40 @@ require 'models'
|
|
4
4
|
class KeyTest < Test::Unit::TestCase
|
5
5
|
include MongoMapper::Plugins::Keys
|
6
6
|
|
7
|
+
context ".new with no id and _id of type integer" do
|
8
|
+
should "not error" do
|
9
|
+
lambda {
|
10
|
+
klass = Doc() do
|
11
|
+
key :_id, Integer
|
12
|
+
end
|
13
|
+
# No sensible default id for integer, people better pass them in if they user this
|
14
|
+
klass.new.id.should be_nil
|
15
|
+
}.should_not raise_error
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context ".new with no id and _id of type string" do
|
20
|
+
should "not error" do
|
21
|
+
lambda {
|
22
|
+
klass = Doc() do
|
23
|
+
key :_id, String
|
24
|
+
end
|
25
|
+
klass.new.id.should_not be_nil
|
26
|
+
}.should_not raise_error
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context ".new with no id and _id of type object id" do
|
31
|
+
should "not error" do
|
32
|
+
lambda {
|
33
|
+
klass = Doc() do
|
34
|
+
key :_id, ObjectId
|
35
|
+
end
|
36
|
+
klass.new.should_not be_nil
|
37
|
+
}.should_not raise_error
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
7
41
|
context ".key?(:symbol)" do
|
8
42
|
should "be true if document has key" do
|
9
43
|
Address.key?(:city).should be_true
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 2010
|
7
7
|
- 8
|
8
|
-
-
|
9
|
-
version: 2010.08.
|
8
|
+
- 9
|
9
|
+
version: 2010.08.09
|
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-08-
|
17
|
+
date: 2010-08-09 00:00:00 -05:00
|
18
18
|
default_executable: mmconsole
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -55,8 +55,8 @@ dependencies:
|
|
55
55
|
segments:
|
56
56
|
- 0
|
57
57
|
- 3
|
58
|
-
-
|
59
|
-
version: 0.3.
|
58
|
+
- 3
|
59
|
+
version: 0.3.3
|
60
60
|
type: :runtime
|
61
61
|
version_requirements: *id003
|
62
62
|
- !ruby/object:Gem::Dependency
|
@@ -64,19 +64,29 @@ dependencies:
|
|
64
64
|
prerelease: false
|
65
65
|
requirement: &id004 !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- -
|
67
|
+
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
segments:
|
70
|
-
-
|
71
|
-
|
72
|
-
- 3
|
73
|
-
version: 1.2.3
|
70
|
+
- 0
|
71
|
+
version: "0"
|
74
72
|
type: :development
|
75
73
|
version_requirements: *id004
|
76
74
|
- !ruby/object:Gem::Dependency
|
77
|
-
name:
|
75
|
+
name: log_buddy
|
78
76
|
prerelease: false
|
79
77
|
requirement: &id005 !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
type: :development
|
85
|
+
version_requirements: *id005
|
86
|
+
- !ruby/object:Gem::Dependency
|
87
|
+
name: jnunemaker-matchy
|
88
|
+
prerelease: false
|
89
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
80
90
|
requirements:
|
81
91
|
- - ~>
|
82
92
|
- !ruby/object:Gem::Version
|
@@ -86,11 +96,11 @@ dependencies:
|
|
86
96
|
- 0
|
87
97
|
version: 0.4.0
|
88
98
|
type: :development
|
89
|
-
version_requirements: *
|
99
|
+
version_requirements: *id006
|
90
100
|
- !ruby/object:Gem::Dependency
|
91
101
|
name: shoulda
|
92
102
|
prerelease: false
|
93
|
-
requirement: &
|
103
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
94
104
|
requirements:
|
95
105
|
- - ~>
|
96
106
|
- !ruby/object:Gem::Version
|
@@ -100,11 +110,11 @@ dependencies:
|
|
100
110
|
- 2
|
101
111
|
version: 2.10.2
|
102
112
|
type: :development
|
103
|
-
version_requirements: *
|
113
|
+
version_requirements: *id007
|
104
114
|
- !ruby/object:Gem::Dependency
|
105
115
|
name: timecop
|
106
116
|
prerelease: false
|
107
|
-
requirement: &
|
117
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
108
118
|
requirements:
|
109
119
|
- - ~>
|
110
120
|
- !ruby/object:Gem::Version
|
@@ -114,11 +124,11 @@ dependencies:
|
|
114
124
|
- 1
|
115
125
|
version: 0.3.1
|
116
126
|
type: :development
|
117
|
-
version_requirements: *
|
127
|
+
version_requirements: *id008
|
118
128
|
- !ruby/object:Gem::Dependency
|
119
129
|
name: mocha
|
120
130
|
prerelease: false
|
121
|
-
requirement: &
|
131
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
122
132
|
requirements:
|
123
133
|
- - ~>
|
124
134
|
- !ruby/object:Gem::Version
|
@@ -128,7 +138,7 @@ dependencies:
|
|
128
138
|
- 8
|
129
139
|
version: 0.9.8
|
130
140
|
type: :development
|
131
|
-
version_requirements: *
|
141
|
+
version_requirements: *id009
|
132
142
|
description:
|
133
143
|
email:
|
134
144
|
- nunemaker@gmail.com
|