mongoid 0.4.2 → 0.4.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/README.textile +28 -9
- data/VERSION +1 -1
- data/lib/mongoid/document.rb +19 -9
- data/lib/mongoid/field.rb +6 -0
- data/mongoid.gemspec +2 -2
- data/spec/spec_helper.rb +9 -12
- data/spec/unit/mongoid/document_spec.rb +65 -42
- data/spec/unit/mongoid/field_spec.rb +24 -0
- metadata +2 -2
data/README.textile
CHANGED
@@ -52,36 +52,55 @@ Example of a simple domain model:
|
|
52
52
|
end
|
53
53
|
</pre>
|
54
54
|
|
55
|
-
|
55
|
+
Mongoid suuports all the basic ActiveRecord creation and persistence methods.
|
56
|
+
|
57
|
+
Creation:
|
56
58
|
|
57
59
|
<pre>
|
58
60
|
Person.create(:title => "Esquire")
|
61
|
+
Person.create!(:title => "Sir", :name => { :first_name => "Elton", :last_name => "John" })
|
59
62
|
</pre>
|
60
63
|
|
61
|
-
Save
|
64
|
+
Save:
|
62
65
|
|
63
66
|
<pre>
|
64
67
|
person.save
|
68
|
+
person.save!
|
65
69
|
</pre>
|
66
70
|
|
67
|
-
|
71
|
+
Updating:
|
68
72
|
|
69
73
|
<pre>
|
70
|
-
person.
|
74
|
+
person.update_attributes(:title => "Sir")
|
75
|
+
person.update_attributes!(:name => { :first_name => "Emmanuel", :last_name => "Zorg" })
|
71
76
|
</pre>
|
72
77
|
|
73
|
-
|
78
|
+
Deleting:
|
74
79
|
|
75
80
|
<pre>
|
76
|
-
person.
|
81
|
+
person.destroy
|
82
|
+
Person.destroy_all(:title => "Sir")
|
83
|
+
person.delete
|
84
|
+
Person.delete_all(:title => "Sir")
|
77
85
|
</pre>
|
78
86
|
|
79
|
-
|
87
|
+
Retrieval of Documents from the database can be done in the traditional ActiveRecord
|
88
|
+
manner or done using the Mongoid criteria DSL.
|
89
|
+
|
90
|
+
Old School:
|
80
91
|
|
81
92
|
<pre>
|
82
|
-
Person.find(:all, :conditions => {:title => "Esquire"})
|
93
|
+
Person.find(:all, :conditions => { :title => "Esquire" })
|
94
|
+
Person.find(:first, :conditions => { :title => "Esquire" })
|
95
|
+
Person.first(:conditions => { :title => "Sir" })
|
96
|
+
Person.all(:conditions => { :title => "Sir" })
|
97
|
+
</pre>
|
83
98
|
|
84
|
-
|
99
|
+
New School:
|
100
|
+
|
101
|
+
<pre>
|
102
|
+
Criteria.new.select(:title => "Sir").only(:first_name, :last_name).skip(10).limit(10).execute
|
103
|
+
Criteria.translate(:conditions => { :title => "Sir" }).execute
|
85
104
|
</pre>
|
86
105
|
|
87
106
|
Paginate Document search results:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.3
|
data/lib/mongoid/document.rb
CHANGED
@@ -40,13 +40,16 @@ module Mongoid #:nodoc:
|
|
40
40
|
# Defines all the fields that are accessable on the Document
|
41
41
|
# For each field that is defined, a getter and setter will be
|
42
42
|
# added as an instance method to the Document.
|
43
|
-
def
|
44
|
-
@fields ||=
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
43
|
+
def field(name, options = {})
|
44
|
+
@fields ||= {}
|
45
|
+
@fields[name] = Field.new(name, options)
|
46
|
+
define_method(name) { read_attribute(name) }
|
47
|
+
define_method("#{name}=") { |value| write_attribute(name, value) }
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns all the fields for the Document as a +Hash+ with names as keys.
|
51
|
+
def fields
|
52
|
+
@fields
|
50
53
|
end
|
51
54
|
|
52
55
|
# Find all Documents in several ways.
|
@@ -90,7 +93,8 @@ module Mongoid #:nodoc:
|
|
90
93
|
# Adds timestamps on the Document in the form of the fields 'created_on'
|
91
94
|
# and 'last_modified'
|
92
95
|
def has_timestamps
|
93
|
-
|
96
|
+
field :created_at
|
97
|
+
field :last_modified
|
94
98
|
class_eval do
|
95
99
|
before_create \
|
96
100
|
:update_created_at,
|
@@ -128,6 +132,11 @@ module Mongoid #:nodoc:
|
|
128
132
|
self.class.collection
|
129
133
|
end
|
130
134
|
|
135
|
+
# Get the fields for the Document class.
|
136
|
+
def fields
|
137
|
+
self.class.fields
|
138
|
+
end
|
139
|
+
|
131
140
|
# Get the Mongo::ObjectID associated with this object.
|
132
141
|
# This is in essence the primary key.
|
133
142
|
def id
|
@@ -170,7 +179,8 @@ module Mongoid #:nodoc:
|
|
170
179
|
|
171
180
|
# Read from the attributes hash.
|
172
181
|
def read_attribute(name)
|
173
|
-
|
182
|
+
symbol = name.to_sym
|
183
|
+
fields[symbol].value(@attributes[symbol])
|
174
184
|
end
|
175
185
|
|
176
186
|
# Update the created_at field on the Document to the current time. This is
|
data/lib/mongoid/field.rb
CHANGED
data/mongoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Durran Jordan"]
|
12
|
-
s.date = %q{2009-10-
|
12
|
+
s.date = %q{2009-10-17}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
data/spec/spec_helper.rb
CHANGED
@@ -16,27 +16,24 @@ Spec::Runner.configure do |config|
|
|
16
16
|
end
|
17
17
|
|
18
18
|
class Person < Mongoid::Document
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
:age
|
19
|
+
field :title
|
20
|
+
field :terms
|
21
|
+
field :age
|
23
22
|
has_many :addresses
|
24
23
|
has_one :name
|
25
24
|
end
|
26
25
|
|
27
26
|
class Address < Mongoid::Document
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
:post_code
|
27
|
+
field :street
|
28
|
+
field :city
|
29
|
+
field :state
|
30
|
+
field :post_code
|
33
31
|
belongs_to :person
|
34
32
|
end
|
35
33
|
|
36
34
|
class Name < Mongoid::Document
|
37
|
-
|
38
|
-
|
39
|
-
:last_name
|
35
|
+
field :first_name
|
36
|
+
field :last_name
|
40
37
|
belongs_to :person
|
41
38
|
end
|
42
39
|
|
@@ -29,6 +29,35 @@ describe Mongoid::Document do
|
|
29
29
|
|
30
30
|
end
|
31
31
|
|
32
|
+
describe "#all" do
|
33
|
+
|
34
|
+
before do
|
35
|
+
@cursor = mock
|
36
|
+
@people = []
|
37
|
+
end
|
38
|
+
|
39
|
+
context "when a selector is provided" do
|
40
|
+
|
41
|
+
it "finds from the collection and instantiate objects for each returned" do
|
42
|
+
@collection.expects(:find).with({ :test => "Test" }, {}).returns(@cursor)
|
43
|
+
@cursor.expects(:collect).returns(@people)
|
44
|
+
Person.all(:conditions => {:test => "Test"})
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
context "when a selector is not provided" do
|
50
|
+
|
51
|
+
it "finds from the collection and instantiate objects for each returned" do
|
52
|
+
@collection.expects(:find).with(nil, {}).returns(@cursor)
|
53
|
+
@cursor.expects(:collect).returns(@people)
|
54
|
+
Person.all
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
32
61
|
describe "#belongs_to" do
|
33
62
|
|
34
63
|
it "creates a reader for the association" do
|
@@ -55,21 +84,25 @@ describe Mongoid::Document do
|
|
55
84
|
|
56
85
|
end
|
57
86
|
|
58
|
-
describe "#
|
87
|
+
describe "#field" do
|
59
88
|
|
60
|
-
|
61
|
-
Person.fields([:testing])
|
62
|
-
end
|
89
|
+
context "with no options" do
|
63
90
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
91
|
+
before do
|
92
|
+
Person.field(:testing)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "adds a reader for the fields defined" do
|
96
|
+
@person = Person.new(:testing => "Test")
|
97
|
+
@person.testing.should == "Test"
|
98
|
+
end
|
99
|
+
|
100
|
+
it "adds a writer for the fields defined" do
|
101
|
+
@person = Person.new(:testing => "Test")
|
102
|
+
@person.testing = "Testy"
|
103
|
+
@person.testing.should == "Testy"
|
104
|
+
end
|
68
105
|
|
69
|
-
it "adds a writer for the fields defined" do
|
70
|
-
@person = Person.new(:testing => "Test")
|
71
|
-
@person.testing = "Testy"
|
72
|
-
@person.testing.should == "Testy"
|
73
106
|
end
|
74
107
|
|
75
108
|
end
|
@@ -162,35 +195,6 @@ describe Mongoid::Document do
|
|
162
195
|
|
163
196
|
end
|
164
197
|
|
165
|
-
describe "#all" do
|
166
|
-
|
167
|
-
before do
|
168
|
-
@cursor = mock
|
169
|
-
@people = []
|
170
|
-
end
|
171
|
-
|
172
|
-
context "when a selector is provided" do
|
173
|
-
|
174
|
-
it "finds from the collection and instantiate objects for each returned" do
|
175
|
-
@collection.expects(:find).with({ :test => "Test" }, {}).returns(@cursor)
|
176
|
-
@cursor.expects(:collect).returns(@people)
|
177
|
-
Person.all(:conditions => {:test => "Test"})
|
178
|
-
end
|
179
|
-
|
180
|
-
end
|
181
|
-
|
182
|
-
context "when a selector is not provided" do
|
183
|
-
|
184
|
-
it "finds from the collection and instantiate objects for each returned" do
|
185
|
-
@collection.expects(:find).with(nil, {}).returns(@cursor)
|
186
|
-
@cursor.expects(:collect).returns(@people)
|
187
|
-
Person.all
|
188
|
-
end
|
189
|
-
|
190
|
-
end
|
191
|
-
|
192
|
-
end
|
193
|
-
|
194
198
|
describe "#group_by" do
|
195
199
|
|
196
200
|
before do
|
@@ -286,7 +290,9 @@ describe Mongoid::Document do
|
|
286
290
|
end
|
287
291
|
|
288
292
|
it "adds created_on and last_modified to the document" do
|
289
|
-
Tester.instance_variable_get(:@fields)
|
293
|
+
fields = Tester.instance_variable_get(:@fields)
|
294
|
+
fields[:created_at].should_not be_nil
|
295
|
+
fields[:last_modified].should_not be_nil
|
290
296
|
end
|
291
297
|
|
292
298
|
end
|
@@ -430,6 +436,23 @@ describe Mongoid::Document do
|
|
430
436
|
|
431
437
|
end
|
432
438
|
|
439
|
+
describe "#read_attribute" do
|
440
|
+
|
441
|
+
context "when attribute does not exist" do
|
442
|
+
|
443
|
+
before do
|
444
|
+
Person.field :weight, :default => 100
|
445
|
+
@person = Person.new
|
446
|
+
end
|
447
|
+
|
448
|
+
it "returns the default value" do
|
449
|
+
@person.weight.should == 100
|
450
|
+
end
|
451
|
+
|
452
|
+
end
|
453
|
+
|
454
|
+
end
|
455
|
+
|
433
456
|
describe "#to_param" do
|
434
457
|
|
435
458
|
it "returns the id" do
|
@@ -26,4 +26,28 @@ describe Mongoid::Field do
|
|
26
26
|
|
27
27
|
end
|
28
28
|
|
29
|
+
describe "#value" do
|
30
|
+
|
31
|
+
before do
|
32
|
+
@field = Mongoid::Field.new(:score, :default => 10)
|
33
|
+
end
|
34
|
+
|
35
|
+
context "nil is provided" do
|
36
|
+
|
37
|
+
it "returns the default value" do
|
38
|
+
@field.value(nil).should == 10
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
context "value is provided" do
|
44
|
+
|
45
|
+
it "returns the value" do
|
46
|
+
@field.value(30).should == 30
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
29
53
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-17 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|