thorsson-mongo_mapper 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +27 -0
  3. data/bin/mmconsole +60 -0
  4. data/examples/keys.rb +40 -0
  5. data/examples/modifiers/set.rb +25 -0
  6. data/examples/plugins.rb +41 -0
  7. data/examples/querying.rb +35 -0
  8. data/examples/scopes.rb +52 -0
  9. data/lib/mongo_mapper.rb +79 -0
  10. data/lib/mongo_mapper/connection.rb +83 -0
  11. data/lib/mongo_mapper/document.rb +41 -0
  12. data/lib/mongo_mapper/embedded_document.rb +31 -0
  13. data/lib/mongo_mapper/exceptions.rb +27 -0
  14. data/lib/mongo_mapper/extensions/array.rb +19 -0
  15. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  16. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  17. data/lib/mongo_mapper/extensions/date.rb +25 -0
  18. data/lib/mongo_mapper/extensions/float.rb +14 -0
  19. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  20. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  21. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  22. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  23. data/lib/mongo_mapper/extensions/object.rb +27 -0
  24. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  25. data/lib/mongo_mapper/extensions/set.rb +20 -0
  26. data/lib/mongo_mapper/extensions/string.rb +18 -0
  27. data/lib/mongo_mapper/extensions/time.rb +29 -0
  28. data/lib/mongo_mapper/plugins.rb +16 -0
  29. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  30. data/lib/mongo_mapper/plugins/associations.rb +97 -0
  31. data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
  32. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
  33. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
  34. data/lib/mongo_mapper/plugins/associations/collection.rb +22 -0
  35. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
  36. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +127 -0
  37. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  38. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +110 -0
  39. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  40. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  41. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  42. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  43. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
  44. data/lib/mongo_mapper/plugins/associations/proxy.rb +126 -0
  45. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  46. data/lib/mongo_mapper/plugins/callbacks.rb +241 -0
  47. data/lib/mongo_mapper/plugins/clone.rb +19 -0
  48. data/lib/mongo_mapper/plugins/descendants.rb +17 -0
  49. data/lib/mongo_mapper/plugins/dirty.rb +120 -0
  50. data/lib/mongo_mapper/plugins/document.rb +41 -0
  51. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  52. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  53. data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
  54. data/lib/mongo_mapper/plugins/equality.rb +17 -0
  55. data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
  56. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  57. data/lib/mongo_mapper/plugins/inspect.rb +15 -0
  58. data/lib/mongo_mapper/plugins/keys.rb +309 -0
  59. data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
  60. data/lib/mongo_mapper/plugins/logger.rb +18 -0
  61. data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
  62. data/lib/mongo_mapper/plugins/pagination.rb +14 -0
  63. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  64. data/lib/mongo_mapper/plugins/protected.rb +53 -0
  65. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  66. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  67. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  68. data/lib/mongo_mapper/plugins/rails.rb +58 -0
  69. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  70. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  71. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  72. data/lib/mongo_mapper/plugins/serialization.rb +76 -0
  73. data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
  74. data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
  75. data/lib/mongo_mapper/plugins/validations.rb +50 -0
  76. data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
  77. data/lib/mongo_mapper/version.rb +4 -0
  78. data/test/_NOTE_ON_TESTING +1 -0
  79. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  80. data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
  81. data/test/functional/associations/test_in_array_proxy.rb +319 -0
  82. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  83. data/test/functional/associations/test_many_documents_proxy.rb +575 -0
  84. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  85. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  86. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  87. data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
  88. data/test/functional/associations/test_one_proxy.rb +182 -0
  89. data/test/functional/test_accessible.rb +168 -0
  90. data/test/functional/test_associations.rb +44 -0
  91. data/test/functional/test_binary.rb +27 -0
  92. data/test/functional/test_caching.rb +76 -0
  93. data/test/functional/test_callbacks.rb +151 -0
  94. data/test/functional/test_dirty.rb +163 -0
  95. data/test/functional/test_document.rb +253 -0
  96. data/test/functional/test_dynamic_querying.rb +75 -0
  97. data/test/functional/test_embedded_document.rb +210 -0
  98. data/test/functional/test_identity_map.rb +506 -0
  99. data/test/functional/test_indexes.rb +42 -0
  100. data/test/functional/test_logger.rb +20 -0
  101. data/test/functional/test_modifiers.rb +416 -0
  102. data/test/functional/test_pagination.rb +91 -0
  103. data/test/functional/test_protected.rb +175 -0
  104. data/test/functional/test_querying.rb +873 -0
  105. data/test/functional/test_safe.rb +76 -0
  106. data/test/functional/test_sci.rb +230 -0
  107. data/test/functional/test_scopes.rb +171 -0
  108. data/test/functional/test_string_id_compatibility.rb +67 -0
  109. data/test/functional/test_timestamps.rb +62 -0
  110. data/test/functional/test_userstamps.rb +27 -0
  111. data/test/functional/test_validations.rb +342 -0
  112. data/test/models.rb +233 -0
  113. data/test/test_active_model_lint.rb +13 -0
  114. data/test/test_helper.rb +104 -0
  115. data/test/unit/associations/test_base.rb +212 -0
  116. data/test/unit/associations/test_proxy.rb +105 -0
  117. data/test/unit/serializers/test_json_serializer.rb +202 -0
  118. data/test/unit/test_clone.rb +69 -0
  119. data/test/unit/test_descendant_appends.rb +71 -0
  120. data/test/unit/test_document.rb +213 -0
  121. data/test/unit/test_dynamic_finder.rb +125 -0
  122. data/test/unit/test_embedded_document.rb +644 -0
  123. data/test/unit/test_extensions.rb +380 -0
  124. data/test/unit/test_key.rb +185 -0
  125. data/test/unit/test_keys.rb +55 -0
  126. data/test/unit/test_mongo_mapper.rb +110 -0
  127. data/test/unit/test_pagination.rb +11 -0
  128. data/test/unit/test_plugins.rb +50 -0
  129. data/test/unit/test_rails.rb +181 -0
  130. data/test/unit/test_rails_compatibility.rb +52 -0
  131. data/test/unit/test_serialization.rb +51 -0
  132. data/test/unit/test_time_zones.rb +39 -0
  133. data/test/unit/test_validations.rb +544 -0
  134. metadata +316 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 John Nunemaker
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,27 @@
1
+ = MongoMapper
2
+
3
+ A Ruby Object Mapper for Mongo.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
10
+ * Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself in another branch so I can ignore when I pull)
11
+ * Send me a pull request. Bonus points for topic branches.
12
+
13
+ == Install
14
+
15
+ $ gem install mongo_mapper
16
+
17
+ == Problems or Questions?
18
+
19
+ Hit up the google group:
20
+ http://groups.google.com/group/mongomapper
21
+
22
+ Hop on IRC:
23
+ irc://chat.freenode.net/#mongomapper
24
+
25
+ == Copyright
26
+
27
+ See LICENSE for details.
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
+
4
+ begin
5
+ require 'mongo_mapper'
6
+ require 'irb'
7
+ rescue LoadError
8
+ require 'rubygems'
9
+ retry
10
+ end
11
+
12
+ IRB.setup(nil)
13
+ irb = IRB::Irb.new
14
+
15
+ IRB.conf[:MAIN_CONTEXT] = irb.context
16
+
17
+ irb.context.evaluate("require 'irb/completion'", 0)
18
+ irb.context.evaluate(%@
19
+ include MongoMapper
20
+
21
+ MongoMapper.database = "mm-test"
22
+ $db = MongoMapper.database
23
+
24
+ @, 0)
25
+
26
+ puts %@
27
+ Welcome to the MongoMapper Console!
28
+
29
+ Example 1:
30
+ things = $db.collection("things")
31
+ things.insert("name" => "Raw Thing")
32
+ things.insert("name" => "Another Thing", "date" => Time.now)
33
+
34
+ cursor = things.find("name" => "Raw Thing")
35
+ puts cursor.next_object.inspect
36
+
37
+ Example 2:
38
+ class Thing
39
+ include MongoMapper::Document
40
+ key :name, String, :required => true
41
+ key :date, Time
42
+ end
43
+
44
+ thing = Thing.new
45
+ thing.name = "My thing"
46
+ thing.date = Time.now
47
+ thing.save
48
+
49
+ all_things = Thing.all
50
+ puts all_things.map { |object| object.name }.inspect
51
+ @
52
+
53
+ trap("SIGINT") do
54
+ irb.signal_handle
55
+ end
56
+
57
+ catch(:IRB_EXIT) do
58
+ irb.eval_input
59
+ end
60
+
@@ -0,0 +1,40 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
2
+ require 'mongo_mapper'
3
+ require 'pp'
4
+
5
+ MongoMapper.database = 'testing'
6
+
7
+ class User
8
+ include MongoMapper::Document
9
+
10
+ key :first_name, String, :required => true
11
+ key :last_name, String, :required => true
12
+ key :token, String, :default => lambda { 'some random string' }
13
+ key :age, Integer
14
+ key :skills, Array
15
+ key :friend_ids, Array, :typecast => 'ObjectId'
16
+ key :links, Hash
17
+ timestamps!
18
+ end
19
+ User.collection.remove # empties collection
20
+
21
+ john = User.create({
22
+ :first_name => 'John',
23
+ :last_name => 'Nunemaker',
24
+ :age => 28,
25
+ :skills => ['ruby', 'mongo', 'javascript'],
26
+ :links => {"Google" => "http://www.google.com"}
27
+ })
28
+
29
+ steve = User.create({
30
+ :first_name => 'Steve',
31
+ :last_name => 'Smith',
32
+ :age => 29,
33
+ :skills => ['html', 'css', 'javascript', 'design'],
34
+ })
35
+
36
+ john.friend_ids << steve.id.to_s # will get typecast to ObjectId
37
+ john.links["Ruby on Rails"] = "http://www.rubyonrails.com"
38
+ john.save
39
+
40
+ pp john
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../../../lib', __FILE__))
2
+ require 'mongo_mapper'
3
+ require 'pp'
4
+
5
+ MongoMapper.database = 'testing'
6
+
7
+ class User
8
+ include MongoMapper::Document
9
+
10
+ key :name, String
11
+ key :tags, Array
12
+ end
13
+ User.collection.remove # empties collection
14
+
15
+ john = User.create(:name => 'John', :tags => %w[ruby mongo], :age => 28)
16
+ bill = User.create(:name => 'Bill', :tags => %w[ruby mongo], :age => 30)
17
+
18
+ User.set({:name => 'John'}, :tags => %[ruby])
19
+ pp john.reload
20
+
21
+ User.set(john.id, :tags => %w[ruby mongo])
22
+ pp john.reload
23
+
24
+ john.set(:tags => %w[something different])
25
+ pp john.reload
@@ -0,0 +1,41 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
2
+ require 'mongo_mapper'
3
+ require 'pp'
4
+
5
+ MongoMapper.database = 'testing'
6
+
7
+ # To create your own plugin, just create a module.
8
+ module FooPlugin
9
+
10
+ # ClassMethods module will automatically get extended
11
+ module ClassMethods
12
+ def foo
13
+ 'Foo class method!'
14
+ end
15
+ end
16
+
17
+ # InstanceMethods module will automatically get included
18
+ module InstanceMethods
19
+ def foo
20
+ 'Foo instance method!'
21
+ end
22
+ end
23
+
24
+ # If present, configure method will be called and passed the
25
+ # model as an argument. Feel free to class_eval or add keys.
26
+ # if method is not present, it doesn't call it.
27
+ def self.configure(model)
28
+ puts "Configuring #{model}..."
29
+ model.key :foo, String
30
+ end
31
+
32
+ end
33
+
34
+ class User
35
+ include MongoMapper::Document
36
+ plugin FooPlugin
37
+ end
38
+
39
+ puts User.foo
40
+ puts User.new.foo
41
+ puts User.key?(:foo)
@@ -0,0 +1,35 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
2
+ require 'mongo_mapper'
3
+ require 'pp'
4
+
5
+ MongoMapper.database = 'testing'
6
+
7
+ class User
8
+ include MongoMapper::Document
9
+
10
+ key :name, String
11
+ key :tags, Array
12
+ end
13
+ User.collection.remove # empties collection
14
+
15
+ User.create(:name => 'John', :tags => %w[ruby mongo], :age => 28)
16
+ User.create(:name => 'Bill', :tags => %w[ruby mongo], :age => 30)
17
+ User.create(:name => 'Frank', :tags => %w[mongo], :age => 35)
18
+ User.create(:name => 'Steve', :tags => %w[html5 css3], :age => 27)
19
+
20
+ [
21
+
22
+ User.all(:name => 'John'),
23
+ User.all(:tags => %w[mongo]),
24
+ User.all(:tags.all => %w[ruby mongo]),
25
+ User.all(:age.gte => 30),
26
+
27
+ User.where(:age.gt => 27).sort(:age).all,
28
+ User.where(:age.gt => 27).sort(:age.desc).all,
29
+ User.where(:age.gt => 27).sort(:age).limit(1).all,
30
+ User.where(:age.gt => 27).sort(:age).skip(1).limit(1).all,
31
+
32
+ ].each do |result|
33
+ pp result
34
+ puts
35
+ end
@@ -0,0 +1,52 @@
1
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
2
+ require 'mongo_mapper'
3
+ require 'pp'
4
+
5
+ MongoMapper.database = 'testing'
6
+
7
+ class User
8
+ include MongoMapper::Document
9
+
10
+ # plain old vanilla scopes with fancy queries
11
+ scope :johns, where(:name => 'John')
12
+
13
+ # plain old vanilla scopes with hashes
14
+ scope :bills, :name => 'Bill'
15
+
16
+ # dynamic scopes with parameters
17
+ scope :by_name, lambda { |name| where(:name => name) }
18
+ scope :by_ages, lambda { |low, high| where(:age.gte => low, :age.lte => high) }
19
+
20
+ # Yep, even plain old methods work as long as they return a query
21
+ def self.by_tag(tag)
22
+ where(:tags => tag)
23
+ end
24
+
25
+ # You can even make a method that returns a scope
26
+ def self.twenties; by_ages(20, 29) end
27
+
28
+ key :name, String
29
+ key :tags, Array
30
+ end
31
+ User.collection.remove # empties collection
32
+
33
+ User.create(:name => 'John', :tags => %w[ruby mongo], :age => 28)
34
+ User.create(:name => 'Bill', :tags => %w[ruby mongo], :age => 30)
35
+ User.create(:name => 'Frank', :tags => %w[mongo], :age => 35)
36
+ User.create(:name => 'Steve', :tags => %w[html5 css3], :age => 27)
37
+
38
+ # simple scopes
39
+ pp User.johns.first
40
+ pp User.bills.first
41
+
42
+ # scope with arg
43
+ pp User.by_name('Frank').first
44
+
45
+ # scope with two args
46
+ pp User.by_ages(20, 29).all
47
+
48
+ # chaining class methods on scopes
49
+ pp User.by_ages(20, 40).by_tag('ruby').all
50
+
51
+ # scope made using method that returns scope
52
+ pp User.twenties.all
@@ -0,0 +1,79 @@
1
+ # encoding: UTF-8
2
+ # Make sure you have the correct versions of the gems (see gemspec) in your load path.
3
+ require 'plucky'
4
+ require 'validatable'
5
+ require 'active_support/all'
6
+
7
+ module MongoMapper
8
+ autoload :Connection, 'mongo_mapper/connection'
9
+
10
+ autoload :Error, 'mongo_mapper/exceptions'
11
+ autoload :DocumentNotFound, 'mongo_mapper/exceptions'
12
+ autoload :InvalidScheme, 'mongo_mapper/exceptions'
13
+ autoload :DocumentNotValid, 'mongo_mapper/exceptions'
14
+ autoload :AccessibleOrProtected, 'mongo_mapper/exceptions'
15
+
16
+ autoload :Document, 'mongo_mapper/document'
17
+ autoload :EmbeddedDocument, 'mongo_mapper/embedded_document'
18
+ autoload :Plugins, 'mongo_mapper/plugins'
19
+ autoload :Version, 'mongo_mapper/version'
20
+
21
+ module Plugins
22
+ autoload :Associations, 'mongo_mapper/plugins/associations'
23
+ autoload :Accessible, 'mongo_mapper/plugins/accessible'
24
+ autoload :Callbacks, 'mongo_mapper/plugins/callbacks'
25
+ autoload :Caching, 'mongo_mapper/plugins/caching'
26
+ autoload :Clone, 'mongo_mapper/plugins/clone'
27
+ autoload :Descendants, 'mongo_mapper/plugins/descendants'
28
+ autoload :Dirty, 'mongo_mapper/plugins/dirty'
29
+ autoload :Document, 'mongo_mapper/plugins/document'
30
+ autoload :DynamicQuerying, 'mongo_mapper/plugins/dynamic_querying'
31
+ autoload :EmbeddedDocument, 'mongo_mapper/plugins/embedded_document'
32
+ autoload :Equality, 'mongo_mapper/plugins/equality'
33
+ autoload :IdentityMap, 'mongo_mapper/plugins/identity_map'
34
+ autoload :Inspect, 'mongo_mapper/plugins/inspect'
35
+ autoload :Indexes, 'mongo_mapper/plugins/indexes'
36
+ autoload :Keys, 'mongo_mapper/plugins/keys'
37
+ autoload :Logger, 'mongo_mapper/plugins/logger'
38
+ autoload :Modifiers, 'mongo_mapper/plugins/modifiers'
39
+ autoload :Pagination, 'mongo_mapper/plugins/pagination'
40
+ autoload :Persistence, 'mongo_mapper/plugins/persistence'
41
+ autoload :Protected, 'mongo_mapper/plugins/protected'
42
+ autoload :Querying, 'mongo_mapper/plugins/querying'
43
+ autoload :Rails, 'mongo_mapper/plugins/rails'
44
+ autoload :Safe, 'mongo_mapper/plugins/safe'
45
+ autoload :Sci, 'mongo_mapper/plugins/sci'
46
+ autoload :Scopes, 'mongo_mapper/plugins/scopes'
47
+ autoload :Serialization, 'mongo_mapper/plugins/serialization'
48
+ autoload :Timestamps, 'mongo_mapper/plugins/timestamps'
49
+ autoload :Userstamps, 'mongo_mapper/plugins/userstamps'
50
+ autoload :Validations, 'mongo_mapper/plugins/validations'
51
+
52
+ module Associations
53
+ autoload :Base, 'mongo_mapper/plugins/associations/base'
54
+ autoload :Collection, 'mongo_mapper/plugins/associations/collection'
55
+ autoload :EmbeddedCollection, 'mongo_mapper/plugins/associations/embedded_collection'
56
+ autoload :ManyDocumentsProxy, 'mongo_mapper/plugins/associations/many_documents_proxy'
57
+ autoload :BelongsToProxy, 'mongo_mapper/plugins/associations/belongs_to_proxy'
58
+ autoload :BelongsToPolymorphicProxy, 'mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy'
59
+ autoload :ManyPolymorphicProxy, 'mongo_mapper/plugins/associations/many_polymorphic_proxy'
60
+ autoload :ManyEmbeddedProxy, 'mongo_mapper/plugins/associations/many_embedded_proxy'
61
+ autoload :ManyEmbeddedPolymorphicProxy, 'mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy'
62
+ autoload :ManyDocumentsAsProxy, 'mongo_mapper/plugins/associations/many_documents_as_proxy'
63
+ autoload :OneProxy, 'mongo_mapper/plugins/associations/one_proxy'
64
+ autoload :OneEmbeddedProxy, 'mongo_mapper/plugins/associations/one_embedded_proxy'
65
+ autoload :InArrayProxy, 'mongo_mapper/plugins/associations/in_array_proxy'
66
+ end
67
+ end
68
+
69
+ extend Connection
70
+ end
71
+
72
+ Dir[File.join(File.dirname(__FILE__), 'mongo_mapper', 'extensions', '*.rb')].each do |extension|
73
+ require extension
74
+ end
75
+
76
+ require 'mongo_mapper/support/descendant_appends'
77
+
78
+ # FIXME: autoload with proxy is failing, need to investigate
79
+ require 'mongo_mapper/plugins/associations/proxy'
@@ -0,0 +1,83 @@
1
+ # encoding: UTF-8
2
+ require 'uri'
3
+
4
+ module MongoMapper
5
+ module Connection
6
+ # @api public
7
+ def connection
8
+ @@connection ||= Mongo::Connection.new
9
+ end
10
+
11
+ # @api public
12
+ def connection=(new_connection)
13
+ @@connection = new_connection
14
+ end
15
+
16
+ # @api public
17
+ def logger
18
+ connection.logger
19
+ end
20
+
21
+ # @api public
22
+ def database=(name)
23
+ @@database = nil
24
+ @@database_name = name
25
+ end
26
+
27
+ # @api public
28
+ def database
29
+ if @@database_name.blank?
30
+ raise 'You forgot to set the default database name: MongoMapper.database = "foobar"'
31
+ end
32
+
33
+ @@database ||= MongoMapper.connection.db(@@database_name)
34
+ end
35
+
36
+ def config=(hash)
37
+ @@config = hash
38
+ end
39
+
40
+ def config
41
+ raise 'Set config before connecting. MongoMapper.config = {...}' unless defined?(@@config)
42
+ @@config
43
+ end
44
+
45
+ # @api private
46
+ def config_for_environment(environment)
47
+ env = config[environment]
48
+ return env if env['uri'].blank?
49
+
50
+ uri = URI.parse(env['uri'])
51
+ raise InvalidScheme.new('must be mongodb') unless uri.scheme == 'mongodb'
52
+ {
53
+ 'host' => uri.host,
54
+ 'port' => uri.port,
55
+ 'database' => uri.path.gsub(/^\//, ''),
56
+ 'username' => uri.user,
57
+ 'password' => uri.password,
58
+ }
59
+ end
60
+
61
+ def connect(environment, options={})
62
+ raise 'Set config before connecting. MongoMapper.config = {...}' if config.blank?
63
+ env = config_for_environment(environment)
64
+ MongoMapper.connection = Mongo::Connection.new(env['host'], env['port'], options)
65
+ MongoMapper.database = env['database']
66
+ MongoMapper.database.authenticate(env['username'], env['password']) if env['username'] && env['password']
67
+ end
68
+
69
+ def setup(config, environment, options={})
70
+ handle_passenger_forking
71
+ self.config = config
72
+ connect(environment, options)
73
+ end
74
+
75
+ def handle_passenger_forking
76
+ if defined?(PhusionPassenger)
77
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
78
+ connection.connect_to_master if forked
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end