durran-mongoid 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/MIT_LICENSE +20 -0
- data/README.textile +134 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/mongoid.rb +77 -0
- data/lib/mongoid/associations/association_factory.rb +21 -0
- data/lib/mongoid/associations/belongs_to_association.rb +23 -0
- data/lib/mongoid/associations/has_many_association.rb +25 -0
- data/lib/mongoid/associations/has_one_association.rb +26 -0
- data/lib/mongoid/document.rb +202 -0
- data/lib/mongoid/extensions.rb +7 -0
- data/lib/mongoid/extensions/array/conversions.rb +13 -0
- data/lib/mongoid/extensions/object/conversions.rb +13 -0
- data/lib/mongoid/paginator.rb +22 -0
- data/mongoid.gemspec +79 -0
- data/spec/integration/mongoid/document_spec.rb +95 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +40 -0
- data/spec/unit/mongoid/associations/association_factory_spec.rb +48 -0
- data/spec/unit/mongoid/associations/belongs_to_association_spec.rb +35 -0
- data/spec/unit/mongoid/associations/has_many_association_spec.rb +105 -0
- data/spec/unit/mongoid/associations/has_one_association_spec.rb +35 -0
- data/spec/unit/mongoid/document_spec.rb +559 -0
- data/spec/unit/mongoid/extensions/array/conversions_spec.rb +14 -0
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +13 -0
- data/spec/unit/mongoid/paginator_spec.rb +74 -0
- metadata +108 -0
@@ -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
|
data/mongoid.gemspec
ADDED
@@ -0,0 +1,79 @@
|
|
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.0"
|
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-09-23}
|
13
|
+
s.email = %q{durran@gmail.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README.textile"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
".gitignore",
|
19
|
+
"MIT_LICENSE",
|
20
|
+
"README.textile",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"lib/mongoid.rb",
|
24
|
+
"lib/mongoid/associations/association_factory.rb",
|
25
|
+
"lib/mongoid/associations/belongs_to_association.rb",
|
26
|
+
"lib/mongoid/associations/has_many_association.rb",
|
27
|
+
"lib/mongoid/associations/has_one_association.rb",
|
28
|
+
"lib/mongoid/document.rb",
|
29
|
+
"lib/mongoid/extensions.rb",
|
30
|
+
"lib/mongoid/extensions/array/conversions.rb",
|
31
|
+
"lib/mongoid/extensions/object/conversions.rb",
|
32
|
+
"lib/mongoid/paginator.rb",
|
33
|
+
"mongoid.gemspec",
|
34
|
+
"spec/integration/mongoid/document_spec.rb",
|
35
|
+
"spec/spec.opts",
|
36
|
+
"spec/spec_helper.rb",
|
37
|
+
"spec/unit/mongoid/associations/association_factory_spec.rb",
|
38
|
+
"spec/unit/mongoid/associations/belongs_to_association_spec.rb",
|
39
|
+
"spec/unit/mongoid/associations/has_many_association_spec.rb",
|
40
|
+
"spec/unit/mongoid/associations/has_one_association_spec.rb",
|
41
|
+
"spec/unit/mongoid/document_spec.rb",
|
42
|
+
"spec/unit/mongoid/extensions/array/conversions_spec.rb",
|
43
|
+
"spec/unit/mongoid/extensions/object/conversions_spec.rb",
|
44
|
+
"spec/unit/mongoid/paginator_spec.rb"
|
45
|
+
]
|
46
|
+
s.homepage = %q{http://github.com/durran/mongoid}
|
47
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
48
|
+
s.require_paths = ["lib"]
|
49
|
+
s.rubygems_version = %q{1.3.5}
|
50
|
+
s.summary = %q{Mongoid}
|
51
|
+
s.test_files = [
|
52
|
+
"spec/integration/mongoid/document_spec.rb",
|
53
|
+
"spec/spec_helper.rb",
|
54
|
+
"spec/unit/mongoid/associations/association_factory_spec.rb",
|
55
|
+
"spec/unit/mongoid/associations/belongs_to_association_spec.rb",
|
56
|
+
"spec/unit/mongoid/associations/has_many_association_spec.rb",
|
57
|
+
"spec/unit/mongoid/associations/has_one_association_spec.rb",
|
58
|
+
"spec/unit/mongoid/document_spec.rb",
|
59
|
+
"spec/unit/mongoid/extensions/array/conversions_spec.rb",
|
60
|
+
"spec/unit/mongoid/extensions/object/conversions_spec.rb",
|
61
|
+
"spec/unit/mongoid/paginator_spec.rb"
|
62
|
+
]
|
63
|
+
|
64
|
+
if s.respond_to? :specification_version then
|
65
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
66
|
+
s.specification_version = 3
|
67
|
+
|
68
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
69
|
+
s.add_runtime_dependency(%q<active_support>, [">= 0"])
|
70
|
+
s.add_runtime_dependency(%q<mongodb-mongo>, [">= 0"])
|
71
|
+
else
|
72
|
+
s.add_dependency(%q<active_support>, [">= 0"])
|
73
|
+
s.add_dependency(%q<mongodb-mongo>, [">= 0"])
|
74
|
+
end
|
75
|
+
else
|
76
|
+
s.add_dependency(%q<active_support>, [">= 0"])
|
77
|
+
s.add_dependency(%q<mongodb-mongo>, [">= 0"])
|
78
|
+
end
|
79
|
+
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
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
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
|
+
end
|
16
|
+
|
17
|
+
class Person < Mongoid::Document
|
18
|
+
fields \
|
19
|
+
:title,
|
20
|
+
:terms,
|
21
|
+
:age
|
22
|
+
has_many :addresses
|
23
|
+
has_one :name
|
24
|
+
end
|
25
|
+
|
26
|
+
class Address < Mongoid::Document
|
27
|
+
fields \
|
28
|
+
:street,
|
29
|
+
:city,
|
30
|
+
:state,
|
31
|
+
:post_code
|
32
|
+
belongs_to :person
|
33
|
+
end
|
34
|
+
|
35
|
+
class Name < Mongoid::Document
|
36
|
+
fields \
|
37
|
+
:first_name,
|
38
|
+
:last_name
|
39
|
+
belongs_to :person
|
40
|
+
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,105 @@
|
|
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 "#first" do
|
51
|
+
|
52
|
+
context "when there are elements in the array" do
|
53
|
+
|
54
|
+
before do
|
55
|
+
@association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns the first element" do
|
59
|
+
@association.first.should be_a_kind_of(Address)
|
60
|
+
@association.first.street.should == "Street 1"
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
context "when the array is empty" do
|
66
|
+
|
67
|
+
before do
|
68
|
+
@association = Mongoid::Associations::HasManyAssociation.new(:addresses, Person.new)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns nil" do
|
72
|
+
@association.first.should be_nil
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "#length" do
|
80
|
+
|
81
|
+
context "#length" do
|
82
|
+
|
83
|
+
it "returns the length of the delegated array" do
|
84
|
+
@association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
|
85
|
+
@association.length.should == 2
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "#push" do
|
93
|
+
|
94
|
+
before do
|
95
|
+
@association = Mongoid::Associations::HasManyAssociation.new(:addresses, @document)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "appends the document to the end of the array" do
|
99
|
+
@association.push(Address.new)
|
100
|
+
@association.length.should == 3
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|