mongoid 0.2.5

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.
@@ -0,0 +1,7 @@
1
+ class Array #:nodoc:
2
+ include Mongoid::Extensions::Array::Conversions
3
+ end
4
+
5
+ class Object #:nodoc:
6
+ include Mongoid::Extensions::Object::Conversions
7
+ end
@@ -0,0 +1,13 @@
1
+ module Mongoid #:nodoc:
2
+ module Extensions #:nodoc:
3
+ module Object #:nodoc:
4
+ # This module converts objects into mongoid related objects.
5
+ module Conversions #:nodoc:
6
+ # Converts this object to a hash of attributes
7
+ def mongoidize
8
+ self.attributes
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Mongoid #:nodoc:
2
+ module Extensions #:nodoc:
3
+ module Array #:nodoc:
4
+ # This module converts arrays into mongoid related objects.
5
+ module Conversions #:nodoc:
6
+ # Converts this array into an array of hashes.
7
+ def mongoidize
8
+ collect { |obj| obj.attributes }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ module Mongoid #:nodoc:
2
+ class Paginator #:nodoc:
3
+
4
+ attr_reader :limit, :offset
5
+
6
+ # Create the new Paginator with the supplied options.
7
+ # * Will default to offset 0 if no page defined in the options.
8
+ # * Will default to limit 20 if no per_page defined in the options.
9
+ def initialize(options = {})
10
+ page = options[:page]
11
+ @limit = options[:per_page] || 20
12
+ @offset = page ? (page - 1) * @limit : 0
13
+ end
14
+
15
+ # Generate the options needed for returning the correct
16
+ # results given the supplied parameters
17
+ def options
18
+ { :limit => @limit, :offset => @offset }
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,80 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mongoid}
8
+ s.version = "0.2.5"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Durran Jordan"]
12
+ s.date = %q{2009-10-01}
13
+ s.email = %q{durran@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "README.textile"
16
+ ]
17
+ s.files = [
18
+ ".gitignore",
19
+ "History.txt",
20
+ "MIT_LICENSE",
21
+ "README.textile",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "lib/mongoid.rb",
25
+ "lib/mongoid/associations/association_factory.rb",
26
+ "lib/mongoid/associations/belongs_to_association.rb",
27
+ "lib/mongoid/associations/has_many_association.rb",
28
+ "lib/mongoid/associations/has_one_association.rb",
29
+ "lib/mongoid/document.rb",
30
+ "lib/mongoid/extensions.rb",
31
+ "lib/mongoid/extensions/array/conversions.rb",
32
+ "lib/mongoid/extensions/object/conversions.rb",
33
+ "lib/mongoid/paginator.rb",
34
+ "mongoid.gemspec",
35
+ "spec/integration/mongoid/document_spec.rb",
36
+ "spec/spec.opts",
37
+ "spec/spec_helper.rb",
38
+ "spec/unit/mongoid/associations/association_factory_spec.rb",
39
+ "spec/unit/mongoid/associations/belongs_to_association_spec.rb",
40
+ "spec/unit/mongoid/associations/has_many_association_spec.rb",
41
+ "spec/unit/mongoid/associations/has_one_association_spec.rb",
42
+ "spec/unit/mongoid/document_spec.rb",
43
+ "spec/unit/mongoid/extensions/array/conversions_spec.rb",
44
+ "spec/unit/mongoid/extensions/object/conversions_spec.rb",
45
+ "spec/unit/mongoid/paginator_spec.rb"
46
+ ]
47
+ s.homepage = %q{http://github.com/durran/mongoid}
48
+ s.rdoc_options = ["--charset=UTF-8"]
49
+ s.require_paths = ["lib"]
50
+ s.rubygems_version = %q{1.3.5}
51
+ s.summary = %q{Mongoid}
52
+ s.test_files = [
53
+ "spec/integration/mongoid/document_spec.rb",
54
+ "spec/spec_helper.rb",
55
+ "spec/unit/mongoid/associations/association_factory_spec.rb",
56
+ "spec/unit/mongoid/associations/belongs_to_association_spec.rb",
57
+ "spec/unit/mongoid/associations/has_many_association_spec.rb",
58
+ "spec/unit/mongoid/associations/has_one_association_spec.rb",
59
+ "spec/unit/mongoid/document_spec.rb",
60
+ "spec/unit/mongoid/extensions/array/conversions_spec.rb",
61
+ "spec/unit/mongoid/extensions/object/conversions_spec.rb",
62
+ "spec/unit/mongoid/paginator_spec.rb"
63
+ ]
64
+
65
+ if s.respond_to? :specification_version then
66
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
67
+ s.specification_version = 3
68
+
69
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
70
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
71
+ s.add_runtime_dependency(%q<mongodb-mongo>, [">= 0"])
72
+ else
73
+ s.add_dependency(%q<activesupport>, [">= 0"])
74
+ s.add_dependency(%q<mongodb-mongo>, [">= 0"])
75
+ end
76
+ else
77
+ s.add_dependency(%q<activesupport>, [">= 0"])
78
+ s.add_dependency(%q<mongodb-mongo>, [">= 0"])
79
+ end
80
+ end
@@ -0,0 +1,95 @@
1
+ require File.join(File.dirname(__FILE__), "/../../spec_helper.rb")
2
+
3
+ describe Mongoid::Document do
4
+
5
+ before do
6
+ Mongoid.database.collection(:people).drop
7
+ end
8
+
9
+ describe "#new" do
10
+
11
+ it "gets a new or current database connection" do
12
+ person = Person.new
13
+ person.collection.should be_a_kind_of(Mongo::Collection)
14
+ end
15
+
16
+ end
17
+
18
+ describe "#create" do
19
+
20
+ it "persists a new record to the database" do
21
+ person = Person.create(:test => "Test")
22
+ person.id.should be_a_kind_of(Mongo::ObjectID)
23
+ person.attributes[:test].should == "Test"
24
+ end
25
+
26
+ end
27
+
28
+ describe "#find" do
29
+
30
+ before do
31
+ @person = Person.create(:title => "Test", :document_class => "Person")
32
+ end
33
+
34
+ context "finding all documents" do
35
+
36
+ it "returns an array of documents based on the selector provided" do
37
+ documents = Person.find(:all, :title => "Test")
38
+ documents[0].title.should == "Test"
39
+ end
40
+
41
+ end
42
+
43
+ context "finding first document" do
44
+
45
+ it "returns the first document based on the selector provided" do
46
+ person = Person.find(:first, :title => "Test")
47
+ person.title.should == "Test"
48
+ end
49
+
50
+ end
51
+
52
+ context "finding by id" do
53
+
54
+ it "finds the document by the supplied id" do
55
+ person = Person.find(@person.id)
56
+ person.id.should == @person.id
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ describe "#group_by" do
64
+
65
+ before do
66
+ 30.times do |num|
67
+ Person.create(:title => "Sir", :age => num, :document_class => "Person")
68
+ end
69
+ end
70
+
71
+ it "returns grouped documents" do
72
+ grouped = Person.group_by([:title], {})
73
+ people = grouped.first["group"]
74
+ person = people.first
75
+ person.should be_a_kind_of(Person)
76
+ person.title.should == "Sir"
77
+ end
78
+
79
+ end
80
+
81
+ describe "#paginate" do
82
+
83
+ before do
84
+ 30.times do |num|
85
+ Person.create(:title => "Test-#{num}", :document_class => "Person")
86
+ end
87
+ end
88
+
89
+ it "returns paginated documents" do
90
+ Person.paginate({}, { :per_page => 20, :page => 2 }).length.should == 10
91
+ end
92
+
93
+ end
94
+
95
+ end
@@ -0,0 +1,5 @@
1
+ --colour
2
+ -fs
3
+ --loadby
4
+ mtime
5
+ --reverse
@@ -0,0 +1,41 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+ require 'rubygems'
4
+
5
+ gem "mocha", "0.9.8"
6
+
7
+ require "mocha"
8
+ require "mongoid"
9
+ require "spec"
10
+
11
+ Mongoid.connect_to("mongoid_test")
12
+
13
+ Spec::Runner.configure do |config|
14
+ config.mock_with :mocha
15
+ Mocha::Configuration.prevent(:stubbing_non_existent_method)
16
+ end
17
+
18
+ class Person < Mongoid::Document
19
+ fields \
20
+ :title,
21
+ :terms,
22
+ :age
23
+ has_many :addresses
24
+ has_one :name
25
+ end
26
+
27
+ class Address < Mongoid::Document
28
+ fields \
29
+ :street,
30
+ :city,
31
+ :state,
32
+ :post_code
33
+ belongs_to :person
34
+ end
35
+
36
+ class Name < Mongoid::Document
37
+ fields \
38
+ :first_name,
39
+ :last_name
40
+ belongs_to :person
41
+ end
@@ -0,0 +1,48 @@
1
+ require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
2
+
3
+ describe Mongoid::Associations::AssociationFactory do
4
+
5
+ describe "#create" do
6
+
7
+ before do
8
+ @document = Person.new
9
+ end
10
+
11
+ context "when type is has_many" do
12
+
13
+ it "returns a HasManyAssociationProxy" do
14
+ association = Mongoid::Associations::AssociationFactory.create(:has_many, :addresses, @document)
15
+ association.should be_a_kind_of(Mongoid::Associations::HasManyAssociation)
16
+ end
17
+
18
+ end
19
+
20
+ context "when type is has_one" do
21
+
22
+ it "returns a HashOneAssociationProxy" do
23
+ association = Mongoid::Associations::AssociationFactory.create(:has_one, :name, @document)
24
+ association.should be_a_kind_of(Mongoid::Associations::HasOneAssociation)
25
+ end
26
+
27
+ end
28
+
29
+ context "when type is belongs_to" do
30
+
31
+ it "returns a BelongsToAssociationProxy" do
32
+ association = Mongoid::Associations::AssociationFactory.create(:belongs_to, :person, @document)
33
+ association.should be_a_kind_of(Mongoid::Associations::BelongsToAssociation)
34
+ end
35
+
36
+ end
37
+
38
+ context "when type is invalid" do
39
+
40
+ it "should raise a InvalidAssociationError" do
41
+ lambda { Mongoid::Associations::AssociationFactory.create(:something, :person, @document) }.should raise_error
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,35 @@
1
+ require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
2
+
3
+ describe Mongoid::Associations::BelongsToAssociation do
4
+
5
+ before do
6
+ @parent = Name.new(:first_name => "Drexel")
7
+ @document = stub(:parent => @parent)
8
+ end
9
+
10
+ describe "#method_missing" do
11
+
12
+ before do
13
+ @association = Mongoid::Associations::BelongsToAssociation.new(@document)
14
+ end
15
+
16
+ context "when getting values" do
17
+
18
+ it "delegates to the document" do
19
+ @association.first_name.should == "Drexel"
20
+ end
21
+
22
+ end
23
+
24
+ context "when setting values" do
25
+
26
+ it "delegates to the document" do
27
+ @association.first_name = "Test"
28
+ @association.first_name.should == "Test"
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,126 @@
1
+ require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
2
+
3
+ describe Mongoid::Associations::HasManyAssociation do
4
+
5
+ before do
6
+ @attributes = { :addresses => [
7
+ { :street => "Street 1", :document_class => "Address" },
8
+ { :street => "Street 2", :document_class => "Address" } ] }
9
+ @document = stub(:attributes => @attributes)
10
+ end
11
+
12
+ describe "#[]" do
13
+
14
+ before do
15
+ @association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
16
+ end
17
+
18
+ context "when the index is present in the association" do
19
+
20
+ it "returns the document at the index" do
21
+ @association[0].should be_a_kind_of(Address)
22
+ @association[0].street.should == "Street 1"
23
+ end
24
+
25
+ end
26
+
27
+ context "when the index is not present in the association" do
28
+
29
+ it "returns nil" do
30
+ @association[3].should be_nil
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ describe "#<<" do
38
+
39
+ before do
40
+ @association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
41
+ end
42
+
43
+ it "appends the document to the end of the array" do
44
+ @association << Address.new
45
+ @association.length.should == 3
46
+ end
47
+
48
+ end
49
+
50
+ describe "#build" do
51
+
52
+ before do
53
+ @association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
54
+ end
55
+
56
+ it "adds a new document to the array with the suppied parameters" do
57
+ @association.build({ :street => "Street 1", :document_class => "Address" })
58
+ @association.length.should == 3
59
+ @association[2].should be_a_kind_of(Address)
60
+ @association[2].street.should == "Street 1"
61
+ end
62
+
63
+ it "returns the newly built object in the association" do
64
+ address = @association.build({ :street => "Yet Another", :document_class => "Address" })
65
+ address.should be_a_kind_of(Address)
66
+ address.street.should == "Yet Another"
67
+ end
68
+
69
+ end
70
+
71
+ describe "#first" do
72
+
73
+ context "when there are elements in the array" do
74
+
75
+ before do
76
+ @association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
77
+ end
78
+
79
+ it "returns the first element" do
80
+ @association.first.should be_a_kind_of(Address)
81
+ @association.first.street.should == "Street 1"
82
+ end
83
+
84
+ end
85
+
86
+ context "when the array is empty" do
87
+
88
+ before do
89
+ @association = Mongoid::Associations::HasManyAssociation.new(:addresses, Person.new)
90
+ end
91
+
92
+ it "returns nil" do
93
+ @association.first.should be_nil
94
+ end
95
+
96
+ end
97
+
98
+ end
99
+
100
+ describe "#length" do
101
+
102
+ context "#length" do
103
+
104
+ it "returns the length of the delegated array" do
105
+ @association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
106
+ @association.length.should == 2
107
+ end
108
+
109
+ end
110
+
111
+ end
112
+
113
+ describe "#push" do
114
+
115
+ before do
116
+ @association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
117
+ end
118
+
119
+ it "appends the document to the end of the array" do
120
+ @association.push(Address.new)
121
+ @association.length.should == 3
122
+ end
123
+
124
+ end
125
+
126
+ end