ssherman-mongo_mapper 0.8.6
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 +7 -0
- data/bin/mmconsole +60 -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 +8 -0
- data/examples/identity_map/middleware.rb +14 -0
- data/examples/keys.rb +40 -0
- data/examples/modifiers/set.rb +25 -0
- data/examples/plugins.rb +41 -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 +79 -0
- data/lib/mongo_mapper/connection.rb +83 -0
- data/lib/mongo_mapper/document.rb +41 -0
- data/lib/mongo_mapper/embedded_document.rb +31 -0
- data/lib/mongo_mapper/exceptions.rb +27 -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 +27 -0
- data/lib/mongo_mapper/extensions/object_id.rb +30 -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 +29 -0
- data/lib/mongo_mapper/plugins.rb +15 -0
- data/lib/mongo_mapper/plugins.rb~ +15 -0
- data/lib/mongo_mapper/plugins/accessible.rb +44 -0
- data/lib/mongo_mapper/plugins/associations.rb +97 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +27 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +127 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +109 -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_embedded_proxy.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +139 -0
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +241 -0
- data/lib/mongo_mapper/plugins/clone.rb +22 -0
- data/lib/mongo_mapper/plugins/descendants.rb +17 -0
- data/lib/mongo_mapper/plugins/dirty.rb +124 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +48 -0
- data/lib/mongo_mapper/plugins/equality.rb +17 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
- data/lib/mongo_mapper/plugins/indexes.rb +12 -0
- data/lib/mongo_mapper/plugins/inspect.rb +15 -0
- data/lib/mongo_mapper/plugins/keys.rb +313 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +59 -0
- data/lib/mongo_mapper/plugins/logger.rb +18 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
- data/lib/mongo_mapper/plugins/pagination.rb +14 -0
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +53 -0
- data/lib/mongo_mapper/plugins/querying.rb +176 -0
- data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
- data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
- data/lib/mongo_mapper/plugins/rails.rb +58 -0
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +32 -0
- data/lib/mongo_mapper/plugins/scopes.rb +21 -0
- data/lib/mongo_mapper/plugins/serialization.rb +76 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
- data/lib/mongo_mapper/plugins/validations.rb +50 -0
- data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
- data/lib/mongo_mapper/version.rb +4 -0
- data/rails/init.rb +19 -0
- data/test/_NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
- data/test/functional/associations/test_in_array_proxy.rb +319 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
- data/test/functional/associations/test_many_documents_proxy.rb +615 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
- data/test/functional/associations/test_one_embedded_proxy.rb +81 -0
- data/test/functional/associations/test_one_proxy.rb +181 -0
- data/test/functional/test_accessible.rb +168 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_caching.rb +76 -0
- data/test/functional/test_callbacks.rb +151 -0
- data/test/functional/test_dirty.rb +163 -0
- data/test/functional/test_document.rb +253 -0
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +210 -0
- data/test/functional/test_identity_map.rb +514 -0
- data/test/functional/test_indexes.rb +42 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +416 -0
- data/test/functional/test_pagination.rb +91 -0
- data/test/functional/test_protected.rb +175 -0
- data/test/functional/test_querying.rb +873 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +230 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_string_id_compatibility.rb +67 -0
- data/test/functional/test_timestamps.rb +62 -0
- data/test/functional/test_userstamps.rb +27 -0
- data/test/functional/test_validations.rb +342 -0
- data/test/models.rb +233 -0
- data/test/test_active_model_lint.rb +13 -0
- data/test/test_helper.rb +100 -0
- data/test/unit/associations/test_base.rb +212 -0
- data/test/unit/associations/test_proxy.rb +105 -0
- data/test/unit/serializers/test_json_serializer.rb +217 -0
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_descendant_appends.rb +71 -0
- data/test/unit/test_document.rb +213 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +644 -0
- data/test/unit/test_extensions.rb +376 -0
- data/test/unit/test_key.rb +185 -0
- data/test/unit/test_keys.rb +89 -0
- data/test/unit/test_mongo_mapper.rb +110 -0
- data/test/unit/test_pagination.rb +11 -0
- data/test/unit/test_plugins.rb +50 -0
- data/test/unit/test_rails.rb +181 -0
- data/test/unit/test_rails_compatibility.rb +52 -0
- data/test/unit/test_serialization.rb +51 -0
- data/test/unit/test_time_zones.rb +39 -0
- data/test/unit/test_validations.rb +564 -0
- metadata +340 -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,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 if forked
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Document
|
4
|
+
extend Support::DescendantAppends
|
5
|
+
|
6
|
+
def self.included(model)
|
7
|
+
model.class_eval do
|
8
|
+
extend Plugins
|
9
|
+
|
10
|
+
plugin Plugins::Document
|
11
|
+
plugin Plugins::Querying # for now needs to be before associations (save_to_collection)
|
12
|
+
plugin Plugins::Associations
|
13
|
+
plugin Plugins::Caching
|
14
|
+
plugin Plugins::Clone
|
15
|
+
plugin Plugins::Descendants
|
16
|
+
plugin Plugins::DynamicQuerying
|
17
|
+
plugin Plugins::Equality
|
18
|
+
plugin Plugins::Inspect
|
19
|
+
plugin Plugins::Indexes
|
20
|
+
plugin Plugins::Keys
|
21
|
+
plugin Plugins::Dirty # for now dirty needs to be after keys
|
22
|
+
plugin Plugins::Logger
|
23
|
+
plugin Plugins::Modifiers
|
24
|
+
plugin Plugins::Pagination
|
25
|
+
plugin Plugins::Persistence
|
26
|
+
plugin Plugins::Accessible
|
27
|
+
plugin Plugins::Protected
|
28
|
+
plugin Plugins::Rails
|
29
|
+
plugin Plugins::Safe # needs to be after querying (save_to_collection)
|
30
|
+
plugin Plugins::Sci
|
31
|
+
plugin Plugins::Scopes
|
32
|
+
plugin Plugins::Serialization
|
33
|
+
plugin Plugins::Timestamps
|
34
|
+
plugin Plugins::Userstamps
|
35
|
+
plugin Plugins::Validations
|
36
|
+
plugin Plugins::Callbacks # for now callbacks needs to be after validations
|
37
|
+
end
|
38
|
+
super
|
39
|
+
end
|
40
|
+
end # Document
|
41
|
+
end # MongoMapper
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module EmbeddedDocument
|
4
|
+
extend Support::DescendantAppends
|
5
|
+
|
6
|
+
def self.included(model)
|
7
|
+
model.class_eval do
|
8
|
+
extend Plugins
|
9
|
+
|
10
|
+
plugin Plugins::EmbeddedDocument
|
11
|
+
plugin Plugins::Associations
|
12
|
+
plugin Plugins::Caching
|
13
|
+
plugin Plugins::Clone
|
14
|
+
plugin Plugins::Descendants
|
15
|
+
plugin Plugins::Equality
|
16
|
+
plugin Plugins::Inspect
|
17
|
+
plugin Plugins::Keys
|
18
|
+
plugin Plugins::Logger
|
19
|
+
plugin Plugins::Persistence
|
20
|
+
plugin Plugins::Accessible
|
21
|
+
plugin Plugins::Protected
|
22
|
+
plugin Plugins::Rails
|
23
|
+
plugin Plugins::Sci
|
24
|
+
plugin Plugins::Serialization
|
25
|
+
plugin Plugins::Validations
|
26
|
+
plugin Plugins::Callbacks
|
27
|
+
end
|
28
|
+
super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,27 @@
|
|
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
|
+
def initialize(document)
|
18
|
+
super("Validation failed: #{document.errors.full_messages.join(", ")}")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class AccessibleOrProtected < Error
|
23
|
+
def initialize(name)
|
24
|
+
super("Declare either attr_protected or attr_accessible for #{name}, but not both.")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
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
|
43
|
+
extend MongoMapper::Extensions::Boolean
|
44
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Extensions
|
4
|
+
module Date
|
5
|
+
def to_mongo(value)
|
6
|
+
if value.nil? || value == ''
|
7
|
+
nil
|
8
|
+
else
|
9
|
+
date = value.is_a?(::Date) || value.is_a?(::Time) ? value : ::Date.parse(value.to_s)
|
10
|
+
::Time.utc(date.year, date.month, date.day)
|
11
|
+
end
|
12
|
+
rescue
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def from_mongo(value)
|
17
|
+
value.to_date if value.present?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Date
|
24
|
+
extend MongoMapper::Extensions::Date
|
25
|
+
end
|