lookout-mongo_mapper 0.11.3
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +33 -0
- data/UPGRADES +26 -0
- data/bin/mmconsole +59 -0
- data/examples/attr_accessible.rb +22 -0
- data/examples/attr_protected.rb +22 -0
- data/examples/cache_key.rb +24 -0
- data/examples/custom_types.rb +24 -0
- data/examples/identity_map.rb +33 -0
- data/examples/identity_map/automatic.rb +2 -0
- data/examples/keys.rb +40 -0
- data/examples/modifiers/set.rb +25 -0
- data/examples/plugins.rb +38 -0
- data/examples/querying.rb +35 -0
- data/examples/safe.rb +43 -0
- data/examples/scopes.rb +52 -0
- data/examples/validating/embedded_docs.rb +29 -0
- data/lib/mongo_mapper.rb +94 -0
- data/lib/mongo_mapper/connection.rb +96 -0
- data/lib/mongo_mapper/document.rb +42 -0
- data/lib/mongo_mapper/embedded_document.rb +32 -0
- data/lib/mongo_mapper/exceptions.rb +30 -0
- data/lib/mongo_mapper/extensions/array.rb +19 -0
- data/lib/mongo_mapper/extensions/binary.rb +22 -0
- data/lib/mongo_mapper/extensions/boolean.rb +44 -0
- data/lib/mongo_mapper/extensions/date.rb +25 -0
- data/lib/mongo_mapper/extensions/float.rb +14 -0
- data/lib/mongo_mapper/extensions/hash.rb +14 -0
- data/lib/mongo_mapper/extensions/integer.rb +19 -0
- data/lib/mongo_mapper/extensions/kernel.rb +9 -0
- data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
- data/lib/mongo_mapper/extensions/object.rb +26 -0
- data/lib/mongo_mapper/extensions/object_id.rb +32 -0
- data/lib/mongo_mapper/extensions/set.rb +20 -0
- data/lib/mongo_mapper/extensions/string.rb +18 -0
- data/lib/mongo_mapper/extensions/time.rb +28 -0
- data/lib/mongo_mapper/locale/en.yml +5 -0
- data/lib/mongo_mapper/middleware/identity_map.rb +16 -0
- data/lib/mongo_mapper/plugins.rb +22 -0
- data/lib/mongo_mapper/plugins/accessible.rb +52 -0
- data/lib/mongo_mapper/plugins/active_model.rb +18 -0
- data/lib/mongo_mapper/plugins/associations.rb +90 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +92 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_association.rb +54 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +34 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +52 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +27 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +44 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +133 -0
- data/lib/mongo_mapper/plugins/associations/many_association.rb +63 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +118 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
- data/lib/mongo_mapper/plugins/associations/one_as_proxy.rb +22 -0
- data/lib/mongo_mapper/plugins/associations/one_association.rb +48 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy.rb +30 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +44 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +95 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +134 -0
- data/lib/mongo_mapper/plugins/associations/single_association.rb +46 -0
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +29 -0
- data/lib/mongo_mapper/plugins/clone.rb +22 -0
- data/lib/mongo_mapper/plugins/dirty.rb +60 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +45 -0
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
- data/lib/mongo_mapper/plugins/embedded_callbacks.rb +56 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +53 -0
- data/lib/mongo_mapper/plugins/equality.rb +23 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
- data/lib/mongo_mapper/plugins/indexes.rb +13 -0
- data/lib/mongo_mapper/plugins/inspect.rb +16 -0
- data/lib/mongo_mapper/plugins/keys.rb +313 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +61 -0
- data/lib/mongo_mapper/plugins/logger.rb +18 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +134 -0
- data/lib/mongo_mapper/plugins/pagination.rb +16 -0
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +45 -0
- data/lib/mongo_mapper/plugins/querying.rb +165 -0
- data/lib/mongo_mapper/plugins/querying/decorator.rb +36 -0
- data/lib/mongo_mapper/plugins/rails.rb +58 -0
- data/lib/mongo_mapper/plugins/rails/active_record_association_adapter.rb +33 -0
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +36 -0
- data/lib/mongo_mapper/plugins/scopes.rb +27 -0
- data/lib/mongo_mapper/plugins/serialization.rb +109 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
- data/lib/mongo_mapper/plugins/touch.rb +18 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +18 -0
- data/lib/mongo_mapper/plugins/validations.rb +86 -0
- data/lib/mongo_mapper/railtie.rb +48 -0
- data/lib/mongo_mapper/railtie/database.rake +65 -0
- data/lib/mongo_mapper/translation.rb +10 -0
- data/lib/mongo_mapper/version.rb +4 -0
- data/lib/rails/generators/mongo_mapper/config/config_generator.rb +24 -0
- data/lib/rails/generators/mongo_mapper/config/templates/mongo.yml +18 -0
- data/lib/rails/generators/mongo_mapper/model/model_generator.rb +23 -0
- data/lib/rails/generators/mongo_mapper/model/templates/model.rb +13 -0
- data/test/_NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +64 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +238 -0
- data/test/functional/associations/test_in_array_proxy.rb +349 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +231 -0
- data/test/functional/associations/test_many_documents_proxy.rb +866 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +239 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +289 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +303 -0
- data/test/functional/associations/test_one_as_proxy.rb +491 -0
- data/test/functional/associations/test_one_embedded_polymorphic_proxy.rb +208 -0
- data/test/functional/associations/test_one_embedded_proxy.rb +100 -0
- data/test/functional/associations/test_one_proxy.rb +383 -0
- data/test/functional/test_accessible.rb +198 -0
- data/test/functional/test_associations.rb +46 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_caching.rb +77 -0
- data/test/functional/test_callbacks.rb +232 -0
- data/test/functional/test_dirty.rb +301 -0
- data/test/functional/test_document.rb +282 -0
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +288 -0
- data/test/functional/test_equality.rb +20 -0
- data/test/functional/test_identity_map.rb +513 -0
- data/test/functional/test_indexes.rb +50 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +537 -0
- data/test/functional/test_pagination.rb +91 -0
- data/test/functional/test_protected.rb +201 -0
- data/test/functional/test_querying.rb +935 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +240 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_timestamps.rb +62 -0
- data/test/functional/test_touch.rb +125 -0
- data/test/functional/test_userstamps.rb +44 -0
- data/test/functional/test_validations.rb +414 -0
- data/test/models.rb +261 -0
- data/test/support/railtie.rb +4 -0
- data/test/support/railtie/autoloaded.rb +2 -0
- data/test/support/railtie/not_autoloaded.rb +3 -0
- data/test/support/railtie/parent.rb +3 -0
- data/test/test_active_model_lint.rb +18 -0
- data/test/test_helper.rb +93 -0
- data/test/unit/associations/test_base.rb +146 -0
- data/test/unit/associations/test_belongs_to_association.rb +29 -0
- data/test/unit/associations/test_many_association.rb +63 -0
- data/test/unit/associations/test_one_association.rb +47 -0
- data/test/unit/associations/test_proxy.rb +100 -0
- data/test/unit/serializers/test_json_serializer.rb +216 -0
- data/test/unit/serializers/test_xml_serializer.rb +196 -0
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_document.rb +249 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +682 -0
- data/test/unit/test_equality.rb +38 -0
- data/test/unit/test_exceptions.rb +12 -0
- data/test/unit/test_extensions.rb +380 -0
- data/test/unit/test_identity_map_middleware.rb +34 -0
- data/test/unit/test_inspect.rb +47 -0
- data/test/unit/test_key.rb +205 -0
- data/test/unit/test_keys.rb +65 -0
- data/test/unit/test_mongo_mapper.rb +143 -0
- data/test/unit/test_pagination.rb +11 -0
- data/test/unit/test_plugins.rb +89 -0
- data/test/unit/test_rails.rb +183 -0
- data/test/unit/test_rails_compatibility.rb +38 -0
- data/test/unit/test_rails_reflect_on_association.rb +118 -0
- data/test/unit/test_railtie.rb +66 -0
- data/test/unit/test_serialization.rb +166 -0
- data/test/unit/test_time_zones.rb +44 -0
- data/test/unit/test_translation.rb +27 -0
- data/test/unit/test_validations.rb +562 -0
- metadata +285 -0
data/examples/scopes.rb
ADDED
@@ -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,29 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
require 'mongo_mapper'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
MongoMapper.database = 'testing'
|
6
|
+
|
7
|
+
class Field
|
8
|
+
include MongoMapper::EmbeddedDocument
|
9
|
+
key :name
|
10
|
+
validates_presence_of :name
|
11
|
+
end
|
12
|
+
|
13
|
+
class Template
|
14
|
+
include MongoMapper::Document
|
15
|
+
key :name
|
16
|
+
many :fields
|
17
|
+
|
18
|
+
# This tells the template to validate all
|
19
|
+
# fields when validating the template.
|
20
|
+
validates_associated :fields
|
21
|
+
end
|
22
|
+
|
23
|
+
# Name is missing on embedded field
|
24
|
+
template = Template.new(:fields => [Field.new])
|
25
|
+
puts template.valid? # false
|
26
|
+
|
27
|
+
# Name is present on embedded field
|
28
|
+
template = Template.new(:fields => [Field.new(:name => 'Yay')])
|
29
|
+
puts template.valid? # true
|
data/lib/mongo_mapper.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'plucky'
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
require 'active_model'
|
5
|
+
require "mongo_mapper/railtie" if defined?(Rails)
|
6
|
+
|
7
|
+
I18n.load_path << File.expand_path('../mongo_mapper/locale/en.yml', __FILE__)
|
8
|
+
|
9
|
+
module MongoMapper
|
10
|
+
autoload :Connection, 'mongo_mapper/connection'
|
11
|
+
|
12
|
+
autoload :Error, 'mongo_mapper/exceptions'
|
13
|
+
autoload :DocumentNotFound, 'mongo_mapper/exceptions'
|
14
|
+
autoload :InvalidScheme, 'mongo_mapper/exceptions'
|
15
|
+
autoload :DocumentNotValid, 'mongo_mapper/exceptions'
|
16
|
+
autoload :AccessibleOrProtected, 'mongo_mapper/exceptions'
|
17
|
+
|
18
|
+
autoload :Document, 'mongo_mapper/document'
|
19
|
+
autoload :EmbeddedDocument, 'mongo_mapper/embedded_document'
|
20
|
+
autoload :Plugins, 'mongo_mapper/plugins'
|
21
|
+
autoload :Translation, 'mongo_mapper/translation'
|
22
|
+
autoload :Version, 'mongo_mapper/version'
|
23
|
+
|
24
|
+
module Middleware
|
25
|
+
autoload :IdentityMap, 'mongo_mapper/middleware/identity_map'
|
26
|
+
end
|
27
|
+
|
28
|
+
module Plugins
|
29
|
+
autoload :ActiveModel, 'mongo_mapper/plugins/active_model'
|
30
|
+
autoload :Associations, 'mongo_mapper/plugins/associations'
|
31
|
+
autoload :Accessible, 'mongo_mapper/plugins/accessible'
|
32
|
+
autoload :Callbacks, 'mongo_mapper/plugins/callbacks'
|
33
|
+
autoload :Caching, 'mongo_mapper/plugins/caching'
|
34
|
+
autoload :Clone, 'mongo_mapper/plugins/clone'
|
35
|
+
autoload :Dirty, 'mongo_mapper/plugins/dirty'
|
36
|
+
autoload :Document, 'mongo_mapper/plugins/document'
|
37
|
+
autoload :DynamicQuerying, 'mongo_mapper/plugins/dynamic_querying'
|
38
|
+
autoload :EmbeddedCallbacks, 'mongo_mapper/plugins/embedded_callbacks'
|
39
|
+
autoload :EmbeddedDocument, 'mongo_mapper/plugins/embedded_document'
|
40
|
+
autoload :Equality, 'mongo_mapper/plugins/equality'
|
41
|
+
autoload :IdentityMap, 'mongo_mapper/plugins/identity_map'
|
42
|
+
autoload :Inspect, 'mongo_mapper/plugins/inspect'
|
43
|
+
autoload :Indexes, 'mongo_mapper/plugins/indexes'
|
44
|
+
autoload :Keys, 'mongo_mapper/plugins/keys'
|
45
|
+
autoload :Logger, 'mongo_mapper/plugins/logger'
|
46
|
+
autoload :Modifiers, 'mongo_mapper/plugins/modifiers'
|
47
|
+
autoload :Pagination, 'mongo_mapper/plugins/pagination'
|
48
|
+
autoload :Persistence, 'mongo_mapper/plugins/persistence'
|
49
|
+
autoload :Protected, 'mongo_mapper/plugins/protected'
|
50
|
+
autoload :Querying, 'mongo_mapper/plugins/querying'
|
51
|
+
autoload :Rails, 'mongo_mapper/plugins/rails'
|
52
|
+
autoload :Safe, 'mongo_mapper/plugins/safe'
|
53
|
+
autoload :Sci, 'mongo_mapper/plugins/sci'
|
54
|
+
autoload :Scopes, 'mongo_mapper/plugins/scopes'
|
55
|
+
autoload :Serialization, 'mongo_mapper/plugins/serialization'
|
56
|
+
autoload :Timestamps, 'mongo_mapper/plugins/timestamps'
|
57
|
+
autoload :Userstamps, 'mongo_mapper/plugins/userstamps'
|
58
|
+
autoload :Validations, 'mongo_mapper/plugins/validations'
|
59
|
+
autoload :Touch, 'mongo_mapper/plugins/touch'
|
60
|
+
|
61
|
+
module Associations
|
62
|
+
autoload :Base, 'mongo_mapper/plugins/associations/base'
|
63
|
+
autoload :Collection, 'mongo_mapper/plugins/associations/collection'
|
64
|
+
autoload :EmbeddedCollection, 'mongo_mapper/plugins/associations/embedded_collection'
|
65
|
+
autoload :ManyAssociation, 'mongo_mapper/plugins/associations/many_association'
|
66
|
+
autoload :SingleAssociation, 'mongo_mapper/plugins/associations/single_association'
|
67
|
+
autoload :BelongsToAssociation, 'mongo_mapper/plugins/associations/belongs_to_association'
|
68
|
+
autoload :OneAssociation, 'mongo_mapper/plugins/associations/one_association'
|
69
|
+
autoload :ManyDocumentsProxy, 'mongo_mapper/plugins/associations/many_documents_proxy'
|
70
|
+
autoload :BelongsToProxy, 'mongo_mapper/plugins/associations/belongs_to_proxy'
|
71
|
+
autoload :BelongsToPolymorphicProxy, 'mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy'
|
72
|
+
autoload :ManyPolymorphicProxy, 'mongo_mapper/plugins/associations/many_polymorphic_proxy'
|
73
|
+
autoload :ManyEmbeddedProxy, 'mongo_mapper/plugins/associations/many_embedded_proxy'
|
74
|
+
autoload :ManyEmbeddedPolymorphicProxy, 'mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy'
|
75
|
+
autoload :ManyDocumentsAsProxy, 'mongo_mapper/plugins/associations/many_documents_as_proxy'
|
76
|
+
autoload :OneProxy, 'mongo_mapper/plugins/associations/one_proxy'
|
77
|
+
autoload :OneAsProxy, 'mongo_mapper/plugins/associations/one_as_proxy'
|
78
|
+
autoload :OneEmbeddedProxy, 'mongo_mapper/plugins/associations/one_embedded_proxy'
|
79
|
+
autoload :OneEmbeddedPolymorphicProxy, 'mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy'
|
80
|
+
autoload :InArrayProxy, 'mongo_mapper/plugins/associations/in_array_proxy'
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
extend Connection
|
85
|
+
end
|
86
|
+
|
87
|
+
Dir[File.join(File.dirname(__FILE__), 'mongo_mapper', 'extensions', '*.rb')].each do |extension|
|
88
|
+
require extension
|
89
|
+
end
|
90
|
+
|
91
|
+
# FIXME: autoload with proxy is failing, need to investigate
|
92
|
+
require 'mongo_mapper/plugins/associations/proxy'
|
93
|
+
|
94
|
+
ActiveSupport.run_load_hooks(:mongo_mapper, MongoMapper)
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module MongoMapper
|
5
|
+
module Connection
|
6
|
+
@@connection = nil
|
7
|
+
@@database = nil
|
8
|
+
@@database_name = nil
|
9
|
+
@@config = nil
|
10
|
+
|
11
|
+
# @api public
|
12
|
+
def connection
|
13
|
+
@@connection ||= Mongo::Connection.new
|
14
|
+
end
|
15
|
+
|
16
|
+
# @api public
|
17
|
+
def connection=(new_connection)
|
18
|
+
@@connection = new_connection
|
19
|
+
end
|
20
|
+
|
21
|
+
# @api public
|
22
|
+
def logger
|
23
|
+
connection.logger
|
24
|
+
end
|
25
|
+
|
26
|
+
# @api public
|
27
|
+
def database=(name)
|
28
|
+
@@database = nil
|
29
|
+
@@database_name = name
|
30
|
+
end
|
31
|
+
|
32
|
+
# @api public
|
33
|
+
def database
|
34
|
+
return nil if @@database_name.blank?
|
35
|
+
|
36
|
+
@@database ||= MongoMapper.connection.db(@@database_name)
|
37
|
+
end
|
38
|
+
|
39
|
+
def config=(hash)
|
40
|
+
@@config = hash
|
41
|
+
end
|
42
|
+
|
43
|
+
def config
|
44
|
+
raise 'Set config before connecting. MongoMapper.config = {...}' unless defined?(@@config)
|
45
|
+
@@config
|
46
|
+
end
|
47
|
+
|
48
|
+
# @api private
|
49
|
+
def config_for_environment(environment)
|
50
|
+
env = config[environment.to_s] || {}
|
51
|
+
return env if env['uri'].blank?
|
52
|
+
|
53
|
+
uri = URI.parse(env['uri'])
|
54
|
+
raise InvalidScheme.new('must be mongodb') unless uri.scheme == 'mongodb'
|
55
|
+
{
|
56
|
+
'host' => uri.host,
|
57
|
+
'port' => uri.port,
|
58
|
+
'database' => uri.path.gsub(/^\//, ''),
|
59
|
+
'username' => uri.user,
|
60
|
+
'password' => uri.password,
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
def connect(environment, options={})
|
65
|
+
raise 'Set config before connecting. MongoMapper.config = {...}' if config.blank?
|
66
|
+
env = config_for_environment(environment)
|
67
|
+
|
68
|
+
if env['options'].is_a? Hash
|
69
|
+
options = env['options'].symbolize_keys.merge(options)
|
70
|
+
end
|
71
|
+
|
72
|
+
MongoMapper.connection = if env['hosts']
|
73
|
+
Mongo::ReplSetConnection.new( *env['hosts'].push(options) )
|
74
|
+
else
|
75
|
+
Mongo::Connection.new(env['host'], env['port'], options)
|
76
|
+
end
|
77
|
+
|
78
|
+
MongoMapper.database = env['database']
|
79
|
+
MongoMapper.database.authenticate(env['username'], env['password']) if env['username'] && env['password']
|
80
|
+
end
|
81
|
+
|
82
|
+
def setup(config, environment, options={})
|
83
|
+
handle_passenger_forking
|
84
|
+
self.config = config
|
85
|
+
connect(environment, options)
|
86
|
+
end
|
87
|
+
|
88
|
+
def handle_passenger_forking
|
89
|
+
if defined?(PhusionPassenger)
|
90
|
+
PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
91
|
+
connection.connect if forked
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Document
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
extend Plugins
|
6
|
+
|
7
|
+
include Plugins::ActiveModel
|
8
|
+
include Plugins::Document
|
9
|
+
include Plugins::Querying # for now needs to be before associations (save_to_collection)
|
10
|
+
include Plugins::Associations
|
11
|
+
include Plugins::Caching
|
12
|
+
include Plugins::Clone
|
13
|
+
include Plugins::DynamicQuerying
|
14
|
+
include Plugins::Equality
|
15
|
+
include Plugins::Inspect
|
16
|
+
include Plugins::Indexes
|
17
|
+
include Plugins::Keys
|
18
|
+
include Plugins::Dirty # for now dirty needs to be after keys
|
19
|
+
include Plugins::Logger
|
20
|
+
include Plugins::Modifiers
|
21
|
+
include Plugins::Pagination
|
22
|
+
include Plugins::Persistence
|
23
|
+
include Plugins::Accessible
|
24
|
+
include Plugins::Protected
|
25
|
+
include Plugins::Rails
|
26
|
+
include Plugins::Safe # needs to be after querying (save_to_collection)
|
27
|
+
include Plugins::Sci
|
28
|
+
include Plugins::Scopes
|
29
|
+
include Plugins::Serialization
|
30
|
+
include Plugins::Timestamps
|
31
|
+
include Plugins::Userstamps
|
32
|
+
include Plugins::Touch
|
33
|
+
include Plugins::Validations
|
34
|
+
include Plugins::EmbeddedCallbacks
|
35
|
+
include Plugins::Callbacks # for now callbacks needs to be after validations
|
36
|
+
|
37
|
+
included do
|
38
|
+
extend Plugins
|
39
|
+
extend Translation
|
40
|
+
end
|
41
|
+
end # Document
|
42
|
+
end # MongoMapper
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module EmbeddedDocument
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
extend Plugins
|
6
|
+
|
7
|
+
include Plugins::ActiveModel
|
8
|
+
include Plugins::EmbeddedDocument
|
9
|
+
include Plugins::Associations
|
10
|
+
include Plugins::Caching
|
11
|
+
include Plugins::Clone
|
12
|
+
include Plugins::Equality
|
13
|
+
include Plugins::Inspect
|
14
|
+
include Plugins::Keys
|
15
|
+
include Plugins::Logger
|
16
|
+
include Plugins::Persistence
|
17
|
+
include Plugins::Accessible
|
18
|
+
include Plugins::Protected
|
19
|
+
include Plugins::Rails
|
20
|
+
include Plugins::Sci
|
21
|
+
include Plugins::Serialization
|
22
|
+
include Plugins::Timestamps
|
23
|
+
include Plugins::Validations
|
24
|
+
include Plugins::EmbeddedCallbacks
|
25
|
+
include Plugins::Touch
|
26
|
+
|
27
|
+
included do
|
28
|
+
extend Plugins
|
29
|
+
extend Translation
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
# generic MM error
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
# raised when document expected but not found
|
7
|
+
class DocumentNotFound < Error; end
|
8
|
+
|
9
|
+
# raised when trying to connect using uri with incorrect scheme
|
10
|
+
class InvalidScheme < Error; end
|
11
|
+
|
12
|
+
# raised when trying to do something not supported, mostly for edocs
|
13
|
+
class NotSupported < Error; end
|
14
|
+
|
15
|
+
# raised when document not valid and using !
|
16
|
+
class DocumentNotValid < Error
|
17
|
+
attr_reader :document
|
18
|
+
|
19
|
+
def initialize(document)
|
20
|
+
@document = document
|
21
|
+
super("Validation failed: #{document.errors.full_messages.join(", ")}")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class AccessibleOrProtected < Error
|
26
|
+
def initialize(name)
|
27
|
+
super("Declare either attr_protected or attr_accessible for #{name}, but not both.")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module Array
|
5
|
+
def to_mongo(value)
|
6
|
+
value = value.respond_to?(:lines) ? value.lines : value
|
7
|
+
value.to_a
|
8
|
+
end
|
9
|
+
|
10
|
+
def from_mongo(value)
|
11
|
+
value || []
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Array
|
18
|
+
extend MongoMapper::Extensions::Array
|
19
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module Binary
|
5
|
+
def to_mongo(value)
|
6
|
+
if value.is_a?(::BSON::Binary)
|
7
|
+
value
|
8
|
+
else
|
9
|
+
value.nil? ? nil : ::BSON::Binary.new(value)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def from_mongo(value)
|
14
|
+
value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Binary
|
21
|
+
extend MongoMapper::Extensions::Binary
|
22
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module Boolean
|
5
|
+
Mapping = {
|
6
|
+
true => true,
|
7
|
+
'true' => true,
|
8
|
+
'TRUE' => true,
|
9
|
+
'True' => true,
|
10
|
+
't' => true,
|
11
|
+
'T' => true,
|
12
|
+
'1' => true,
|
13
|
+
1 => true,
|
14
|
+
1.0 => true,
|
15
|
+
false => false,
|
16
|
+
'false' => false,
|
17
|
+
'FALSE' => false,
|
18
|
+
'False' => false,
|
19
|
+
'f' => false,
|
20
|
+
'F' => false,
|
21
|
+
'0' => false,
|
22
|
+
0 => false,
|
23
|
+
0.0 => false,
|
24
|
+
nil => nil
|
25
|
+
}
|
26
|
+
|
27
|
+
def to_mongo(value)
|
28
|
+
if value.is_a?(Boolean)
|
29
|
+
value
|
30
|
+
else
|
31
|
+
Mapping[value]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def from_mongo(value)
|
36
|
+
value.nil? ? nil : !!value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Boolean; end unless defined?(Boolean)
|
43
|
+
|
44
|
+
Boolean.extend MongoMapper::Extensions::Boolean
|