mongoid 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.4.2
@@ -32,9 +32,10 @@ require "delegate"
32
32
  require "will_paginate/collection"
33
33
  require "mongo"
34
34
  require "mongoid/associations"
35
+ require "mongoid/commands"
35
36
  require "mongoid/criteria"
36
37
  require "mongoid/extensions"
37
- require "mongoid/commands"
38
+ require "mongoid/field"
38
39
  require "mongoid/document"
39
40
 
40
41
  module Mongoid
@@ -1,6 +1,6 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Commands
3
- class Create #:nodoc:
3
+ class Create
4
4
  # Performs a create of the supplied Document, with the necessary
5
5
  # callbacks. It then delegates to the Save command.
6
6
  #
@@ -1,6 +1,6 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Commands
3
- class Delete #:nodoc:
3
+ class Delete
4
4
  # Performs a delete of the supplied +Document+ without any callbacks.
5
5
  #
6
6
  # Options:
@@ -1,6 +1,6 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Commands
3
- class DeleteAll #:nodoc:
3
+ class DeleteAll
4
4
  # Performs a delete of the all the +Documents+ that match the criteria
5
5
  # supplied.
6
6
  #
@@ -1,6 +1,6 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Commands
3
- class Destroy #:nodoc:
3
+ class Destroy
4
4
  # Performs a destroy of the supplied +Document+, with the necessary
5
5
  # callbacks. It then deletes the record from the collection.
6
6
  #
@@ -1,6 +1,6 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Commands
3
- class DestroyAll #:nodoc:
3
+ class DestroyAll
4
4
  # Performs a destroy of the all the +Documents+ that match the criteria
5
5
  # supplied. Will execute all the destroy callbacks for each +Document+.
6
6
  #
@@ -1,6 +1,6 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Commands
3
- class Save #:nodoc:
3
+ class Save
4
4
  # Performs a save of the supplied +Document+, handling all associated
5
5
  # callbacks and validation.
6
6
  #
@@ -72,6 +72,22 @@ module Mongoid #:nodoc:
72
72
  return klass.collection.find(@selector, @options).collect { |doc| klass.new(doc) }
73
73
  end
74
74
 
75
+ # Adds a criterion to the +Criteria+ that specifies additional options
76
+ # to be passed to the Ruby driver, in the exact format for the driver.
77
+ #
78
+ # Options:
79
+ #
80
+ # extras: A +Hash+ that gets set to the driver options.
81
+ #
82
+ # Example:
83
+ #
84
+ # <tt>criteria.extras(:limit => 20, :skip => 40)</tt>
85
+ #
86
+ # Returns: <tt>self</tt>
87
+ def extras(extras)
88
+ @options = extras; self
89
+ end
90
+
75
91
  # Adds a criterion to the +Criteria+ that specifies values where any can
76
92
  # be matched in order to return results. This is similar to an SQL "IN"
77
93
  # clause. The MongoDB conditional operator that will be used is "$in".
@@ -173,36 +189,19 @@ module Mongoid #:nodoc:
173
189
  exclusions.each { |key, value| @selector[key] = { "$nin" => value } }; self
174
190
  end
175
191
 
176
- # Adds a criterion to the +Criteria+ that specifies additional options
177
- # to be passed to the Ruby driver, in the exact format for the driver.
178
- #
179
- # Options:
180
- #
181
- # extras: A +Hash+ that gets set to the driver options.
182
- #
183
- # Example:
184
- #
185
- # <tt>criteria.extras(:limit => 20, :skip => 40)</tt>
186
- #
187
- # Returns: <tt>self</tt>
188
- def extras(extras)
189
- @options = extras; self
190
- end
191
-
192
192
  # Adds a criterion to the +Criteria+ that specifies the sort order of
193
193
  # the returned documents in the database. Similar to a SQL "ORDER BY".
194
194
  #
195
195
  # Options:
196
196
  #
197
- # params: A +Hash+ where the key is the field name and the value is an
198
- # integer specifying the direction: 1 for ascending, -1 for descending.
197
+ # params: An +Array+ of [field, direction] sorting pairs.
199
198
  #
200
199
  # Example:
201
200
  #
202
- # <tt>criteria.order_by(:field1 => 1, :field2 => -1)</tt>
201
+ # <tt>criteria.order_by([[:field1, :asc], [:field2, :desc]])</tt>
203
202
  #
204
203
  # Returns: <tt>self</tt>
205
- def order_by(params = {})
204
+ def order_by(params = [])
206
205
  @options[:sort] = params; self
207
206
  end
208
207
 
@@ -1,5 +1,5 @@
1
1
  module Mongoid #:nodoc:
2
- class Document #:nodoc:
2
+ class Document
3
3
  include ActiveSupport::Callbacks
4
4
  include Validatable
5
5
  include Commands
@@ -26,6 +26,7 @@ module Mongoid #:nodoc:
26
26
  collection.group(fields, selector, { :count => 0 }, AGGREGATE_REDUCE)
27
27
  end
28
28
 
29
+ # Adds the association back to the parent document.
29
30
  def belongs_to(association_name)
30
31
  add_association(:belongs_to, association_name.to_s.classify, association_name)
31
32
  end
@@ -112,7 +113,7 @@ module Mongoid #:nodoc:
112
113
  params[:page] || 1,
113
114
  params[:per_page] || 20,
114
115
  0) do |pager|
115
- results = collection.find(selector, { :sort => (params[:sort] || {}),
116
+ results = collection.find(selector, { :sort => params[:sort],
116
117
  :limit => pager.per_page,
117
118
  :offset => pager.offset })
118
119
  pager.total_entries = results.count
@@ -1,11 +1,11 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Extensions #:nodoc:
3
- module Object #:nodoc:
4
- # This module converts objects into mongoid related objects.
3
+ module Array #:nodoc:
4
+ # This module converts arrays into mongoid related objects.
5
5
  module Conversions #:nodoc:
6
- # Converts this object to a hash of attributes
6
+ # Converts this array into an array of hashes.
7
7
  def mongoidize
8
- self.attributes
8
+ collect { |obj| obj.attributes }
9
9
  end
10
10
  end
11
11
  end
@@ -1,11 +1,11 @@
1
1
  module Mongoid #:nodoc:
2
2
  module Extensions #:nodoc:
3
- module Array #:nodoc:
4
- # This module converts arrays into mongoid related objects.
3
+ module Object #:nodoc:
4
+ # This module converts objects into mongoid related objects.
5
5
  module Conversions #:nodoc:
6
- # Converts this array into an array of hashes.
6
+ # Converts this object to a hash of attributes
7
7
  def mongoidize
8
- collect { |obj| obj.attributes }
8
+ self.attributes
9
9
  end
10
10
  end
11
11
  end
@@ -0,0 +1,25 @@
1
+ module Mongoid #:nodoc:
2
+ class Field
3
+
4
+ attr_reader \
5
+ :default,
6
+ :name
7
+
8
+ # Create the new field with a name and optional additional options. Valid
9
+ # options are :default
10
+ #
11
+ # Options:
12
+ #
13
+ # name: The name of the field as a +Symbol+.
14
+ # options: A +Hash+ of options for the field.
15
+ #
16
+ # Example:
17
+ #
18
+ # <tt>Field.new(:score, :default => 0)</tt>
19
+ def initialize(name, options = {})
20
+ @name = name
21
+ @default = options[:default]
22
+ end
23
+
24
+ end
25
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid}
8
- s.version = "0.4.1"
8
+ s.version = "0.4.2"
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"]
@@ -40,6 +40,7 @@ Gem::Specification.new do |s|
40
40
  "lib/mongoid/extensions.rb",
41
41
  "lib/mongoid/extensions/array/conversions.rb",
42
42
  "lib/mongoid/extensions/object/conversions.rb",
43
+ "lib/mongoid/field.rb",
43
44
  "mongoid.gemspec",
44
45
  "spec/integration/mongoid/document_spec.rb",
45
46
  "spec/spec.opts",
@@ -55,10 +56,12 @@ Gem::Specification.new do |s|
55
56
  "spec/unit/mongoid/commands/destroy_all_spec.rb",
56
57
  "spec/unit/mongoid/commands/destroy_spec.rb",
57
58
  "spec/unit/mongoid/commands/save_spec.rb",
59
+ "spec/unit/mongoid/commands_spec.rb",
58
60
  "spec/unit/mongoid/criteria_spec.rb",
59
61
  "spec/unit/mongoid/document_spec.rb",
60
62
  "spec/unit/mongoid/extensions/array/conversions_spec.rb",
61
- "spec/unit/mongoid/extensions/object/conversions_spec.rb"
63
+ "spec/unit/mongoid/extensions/object/conversions_spec.rb",
64
+ "spec/unit/mongoid/field_spec.rb"
62
65
  ]
63
66
  s.homepage = %q{http://github.com/durran/mongoid}
64
67
  s.rdoc_options = ["--charset=UTF-8"]
@@ -79,10 +82,12 @@ Gem::Specification.new do |s|
79
82
  "spec/unit/mongoid/commands/destroy_all_spec.rb",
80
83
  "spec/unit/mongoid/commands/destroy_spec.rb",
81
84
  "spec/unit/mongoid/commands/save_spec.rb",
85
+ "spec/unit/mongoid/commands_spec.rb",
82
86
  "spec/unit/mongoid/criteria_spec.rb",
83
87
  "spec/unit/mongoid/document_spec.rb",
84
88
  "spec/unit/mongoid/extensions/array/conversions_spec.rb",
85
- "spec/unit/mongoid/extensions/object/conversions_spec.rb"
89
+ "spec/unit/mongoid/extensions/object/conversions_spec.rb",
90
+ "spec/unit/mongoid/field_spec.rb"
86
91
  ]
87
92
 
88
93
  if s.respond_to? :specification_version then
@@ -0,0 +1,153 @@
1
+ require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
2
+
3
+ describe Mongoid::Commands do
4
+
5
+ context "instance methods" do
6
+
7
+ before do
8
+ @person = Person.new(:_id => Mongo::ObjectID.new)
9
+ end
10
+
11
+ describe "#delete" do
12
+
13
+ it "delegates to the Delete command" do
14
+ Mongoid::Commands::Delete.expects(:execute).with(@person)
15
+ @person.delete
16
+ end
17
+
18
+ end
19
+
20
+ describe "#destroy" do
21
+
22
+ it "delegates to the Destroy command" do
23
+ Mongoid::Commands::Destroy.expects(:execute).with(@person)
24
+ @person.destroy
25
+ end
26
+
27
+ end
28
+
29
+ describe "#save" do
30
+
31
+ it "delegates to the Save command" do
32
+ Mongoid::Commands::Save.expects(:execute).with(@person).returns(true)
33
+ @person.save
34
+ end
35
+
36
+ end
37
+
38
+ describe "#save!" do
39
+
40
+ context "when validation passes" do
41
+
42
+ it "it returns the person" do
43
+ Mongoid::Commands::Save.expects(:execute).with(@person).returns(@person)
44
+ @person.save!.should == @person
45
+ end
46
+
47
+ end
48
+
49
+ context "when validation fails" do
50
+
51
+ it "it raises a ValidationsError" do
52
+ Mongoid::Commands::Save.expects(:execute).with(@person).returns(false)
53
+ lambda { @person.save! }.should raise_error
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+
60
+ describe "#update_attributes" do
61
+
62
+ it "delegates to the Save command" do
63
+ Mongoid::Commands::Save.expects(:execute).with(@person).returns(@person)
64
+ @person.update_attributes({})
65
+ end
66
+
67
+ end
68
+
69
+ describe "#update_attributes!" do
70
+
71
+ context "when validation passes" do
72
+
73
+ it "it returns the person" do
74
+ Mongoid::Commands::Save.expects(:execute).with(@person).returns(@person)
75
+ @person.update_attributes({}).should == @person
76
+ end
77
+
78
+ end
79
+
80
+ context "when validation fails" do
81
+
82
+ it "it raises a ValidationsError" do
83
+ Mongoid::Commands::Save.expects(:execute).with(@person).returns(false)
84
+ lambda { @person.update_attributes!({}) }.should raise_error
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+
91
+ end
92
+
93
+ context "class methods" do
94
+
95
+ describe "#create" do
96
+
97
+ it "delegates to the Create command" do
98
+ Mongoid::Commands::Create.expects(:execute)
99
+ Person.create
100
+ end
101
+
102
+ it "returns the document" do
103
+ Mongoid::Commands::Create.expects(:execute).returns(Person.new)
104
+ Person.create.should_not be_nil
105
+ end
106
+
107
+ end
108
+
109
+ describe "#create!" do
110
+
111
+ it "delegates to the Create command" do
112
+ Mongoid::Commands::Create.expects(:execute).returns(Person.new)
113
+ Person.create!
114
+ end
115
+
116
+ it "returns the document" do
117
+ Mongoid::Commands::Create.expects(:execute).returns(Person.new)
118
+ Person.create!.should_not be_nil
119
+ end
120
+
121
+ context "when validation fails" do
122
+
123
+ it "raises an exception" do
124
+ person = stub(:errors => stub(:empty? => false))
125
+ Mongoid::Commands::Create.expects(:execute).returns(person)
126
+ lambda { Person.create! }.should raise_error
127
+ end
128
+
129
+ end
130
+
131
+ end
132
+
133
+ describe "#delete_all" do
134
+
135
+ it "delegates to the DeleteAll command" do
136
+ Mongoid::Commands::DeleteAll.expects(:execute).with(Person, {})
137
+ Person.delete_all
138
+ end
139
+
140
+ end
141
+
142
+ describe "#destroy_all" do
143
+
144
+ it "delegates to the DestroyAll command" do
145
+ Mongoid::Commands::DestroyAll.expects(:execute).with(Person, {})
146
+ Person.destroy_all
147
+ end
148
+
149
+ end
150
+
151
+ end
152
+
153
+ end
@@ -158,8 +158,8 @@ describe Mongoid::Criteria do
158
158
  context "when field names and direction specified" do
159
159
 
160
160
  it "adds the sort to the options" do
161
- @criteria.order_by(:title => 1, :text => -1)
162
- @criteria.options.should == { :sort => { :title => 1, :text => -1 }}
161
+ @criteria.order_by([[:title, :asc], [:text, :desc]])
162
+ @criteria.options.should == { :sort => [[:title, :asc], [:text, :desc]] }
163
163
  end
164
164
 
165
165
  end
@@ -55,54 +55,6 @@ describe Mongoid::Document do
55
55
 
56
56
  end
57
57
 
58
- describe "#create" do
59
-
60
- context "with no attributes" do
61
-
62
- it "creates a new saved document" do
63
- @collection.expects(:save).with({})
64
- person = Person.create
65
- person.should_not be_nil
66
- end
67
-
68
- end
69
-
70
- context "with attributes" do
71
-
72
- it "creates a new saved document" do
73
- @collection.expects(:save).with({:test => "test"})
74
- person = Person.create(:test => "test")
75
- person.should_not be_nil
76
- end
77
-
78
- end
79
-
80
- end
81
-
82
- describe "#destroy" do
83
-
84
- context "when the Document is remove from the database" do
85
-
86
- it "returns nil" do
87
- id = Mongo::ObjectID.new
88
- person = Person.new(:_id => id)
89
- Mongoid::Commands::Destroy.expects(:execute).with(person)
90
- person.destroy.should be_nil
91
- end
92
-
93
- end
94
-
95
- end
96
-
97
- describe "#destroy_all" do
98
-
99
- it "deletes all documents from the collection" do
100
- Mongoid::Commands::DestroyAll.expects(:execute).with(Person, {})
101
- Person.destroy_all
102
- end
103
-
104
- end
105
-
106
58
  describe "#fields" do
107
59
 
108
60
  before do
@@ -424,7 +376,7 @@ describe Mongoid::Document do
424
376
  context "when pagination parameters are passed" do
425
377
 
426
378
  it "delegates to will paginate with the results" do
427
- @collection.expects(:find).with({ :test => "Test" }, { :sort => {}, :limit => 20, :offset => 20}).returns(@cursor)
379
+ @collection.expects(:find).with({ :test => "Test" }, { :sort => nil, :limit => 20, :offset => 20}).returns(@cursor)
428
380
  Person.paginate(:conditions => { :test => "Test" }, :page => 2, :per_page => 20)
429
381
  end
430
382
 
@@ -433,7 +385,7 @@ describe Mongoid::Document do
433
385
  context "when pagination parameters are not passed" do
434
386
 
435
387
  it "delegates to will paginate with default values" do
436
- @collection.expects(:find).with({ :test => "Test" }, { :sort => {}, :limit => 20, :offset => 0}).returns(@cursor)
388
+ @collection.expects(:find).with({ :test => "Test" }, { :sort => nil, :limit => 20, :offset => 0}).returns(@cursor)
437
389
  Person.paginate(:conditions => { :test => "Test" })
438
390
  end
439
391
 
@@ -478,41 +430,6 @@ describe Mongoid::Document do
478
430
 
479
431
  end
480
432
 
481
- describe "#save" do
482
-
483
- context "when the document is the root" do
484
-
485
- before do
486
- @attributes = { :test => "test" }
487
- @person = Person.new(@attributes)
488
- end
489
-
490
- it "persists the object to the MongoDB collection" do
491
- @collection.expects(:save).with(@person.attributes)
492
- @person.save.should be_true
493
- end
494
-
495
- end
496
-
497
- context "when the document is embedded" do
498
-
499
- before do
500
- @attributes = { :title => "Sir",
501
- :addresses => [
502
- { :street => "Street 1", :document_class => "Address" },
503
- { :street => "Street 2", :document_class => "Address" } ] }
504
- @person = Person.new(@attributes)
505
- end
506
-
507
- it "saves the root document" do
508
- @collection.expects(:save).with(@person.attributes)
509
- @person.addresses.first.save
510
- end
511
-
512
- end
513
-
514
- end
515
-
516
433
  describe "#to_param" do
517
434
 
518
435
  it "returns the id" do
@@ -522,20 +439,6 @@ describe Mongoid::Document do
522
439
 
523
440
  end
524
441
 
525
- describe "#update_attributes" do
526
-
527
- context "when attributes are provided" do
528
-
529
- it "saves and returns true" do
530
- person = Person.new
531
- person.expects(:save).returns(true)
532
- person.update_attributes(:test => "Test").should be_true
533
- end
534
-
535
- end
536
-
537
- end
538
-
539
442
  context "validations" do
540
443
 
541
444
  context "when defining using macros" do
@@ -0,0 +1,29 @@
1
+ require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
2
+
3
+ describe Mongoid::Field do
4
+
5
+ describe "#default" do
6
+
7
+ before do
8
+ @field = Mongoid::Field.new(:score, :default => 0)
9
+ end
10
+
11
+ it "returns the default option" do
12
+ @field.default.should == 0
13
+ end
14
+
15
+ end
16
+
17
+ describe "#name" do
18
+
19
+ before do
20
+ @field = Mongoid::Field.new(:score, :default => 0)
21
+ end
22
+
23
+ it "returns the name" do
24
+ @field.name.should == :score
25
+ end
26
+
27
+ end
28
+
29
+ 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.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -86,6 +86,7 @@ files:
86
86
  - lib/mongoid/extensions.rb
87
87
  - lib/mongoid/extensions/array/conversions.rb
88
88
  - lib/mongoid/extensions/object/conversions.rb
89
+ - lib/mongoid/field.rb
89
90
  - mongoid.gemspec
90
91
  - spec/integration/mongoid/document_spec.rb
91
92
  - spec/spec.opts
@@ -101,10 +102,12 @@ files:
101
102
  - spec/unit/mongoid/commands/destroy_all_spec.rb
102
103
  - spec/unit/mongoid/commands/destroy_spec.rb
103
104
  - spec/unit/mongoid/commands/save_spec.rb
105
+ - spec/unit/mongoid/commands_spec.rb
104
106
  - spec/unit/mongoid/criteria_spec.rb
105
107
  - spec/unit/mongoid/document_spec.rb
106
108
  - spec/unit/mongoid/extensions/array/conversions_spec.rb
107
109
  - spec/unit/mongoid/extensions/object/conversions_spec.rb
110
+ - spec/unit/mongoid/field_spec.rb
108
111
  has_rdoc: true
109
112
  homepage: http://github.com/durran/mongoid
110
113
  licenses: []
@@ -147,7 +150,9 @@ test_files:
147
150
  - spec/unit/mongoid/commands/destroy_all_spec.rb
148
151
  - spec/unit/mongoid/commands/destroy_spec.rb
149
152
  - spec/unit/mongoid/commands/save_spec.rb
153
+ - spec/unit/mongoid/commands_spec.rb
150
154
  - spec/unit/mongoid/criteria_spec.rb
151
155
  - spec/unit/mongoid/document_spec.rb
152
156
  - spec/unit/mongoid/extensions/array/conversions_spec.rb
153
157
  - spec/unit/mongoid/extensions/object/conversions_spec.rb
158
+ - spec/unit/mongoid/field_spec.rb