mongoid 2.0.0.beta.20 → 2.0.0.rc.1
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.
- data/README.rdoc +8 -0
- data/Rakefile +51 -0
- data/lib/config/locales/nl.yml +39 -0
- data/lib/config/locales/ro.yml +1 -1
- data/lib/mongoid.rb +17 -17
- data/lib/mongoid/atomicity.rb +54 -22
- data/lib/mongoid/attributes.rb +145 -125
- data/lib/mongoid/callbacks.rb +7 -2
- data/lib/mongoid/collection.rb +49 -32
- data/lib/mongoid/collections.rb +0 -1
- data/lib/mongoid/components.rb +34 -29
- data/lib/mongoid/config.rb +207 -193
- data/lib/mongoid/config/database.rb +167 -0
- data/lib/mongoid/contexts.rb +2 -5
- data/lib/mongoid/contexts/enumerable.rb +30 -4
- data/lib/mongoid/contexts/ids.rb +2 -2
- data/lib/mongoid/contexts/mongo.rb +30 -5
- data/lib/mongoid/copyable.rb +44 -0
- data/lib/mongoid/criteria.rb +110 -56
- data/lib/mongoid/criterion/creational.rb +34 -0
- data/lib/mongoid/criterion/destructive.rb +37 -0
- data/lib/mongoid/criterion/exclusion.rb +3 -1
- data/lib/mongoid/criterion/inclusion.rb +59 -64
- data/lib/mongoid/criterion/inspection.rb +22 -0
- data/lib/mongoid/criterion/optional.rb +42 -54
- data/lib/mongoid/criterion/selector.rb +9 -0
- data/lib/mongoid/default_scope.rb +28 -0
- data/lib/mongoid/deprecation.rb +5 -5
- data/lib/mongoid/dirty.rb +4 -5
- data/lib/mongoid/document.rb +161 -114
- data/lib/mongoid/extensions.rb +7 -11
- data/lib/mongoid/extensions/array/parentization.rb +2 -2
- data/lib/mongoid/extensions/date/conversions.rb +1 -1
- data/lib/mongoid/extensions/hash/conversions.rb +0 -23
- data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
- data/lib/mongoid/extensions/object/reflections.rb +17 -0
- data/lib/mongoid/extensions/object/yoda.rb +27 -0
- data/lib/mongoid/extensions/string/conversions.rb +23 -4
- data/lib/mongoid/extensions/time_conversions.rb +4 -4
- data/lib/mongoid/field.rb +30 -19
- data/lib/mongoid/fields.rb +15 -5
- data/lib/mongoid/finders.rb +19 -11
- data/lib/mongoid/hierarchy.rb +34 -28
- data/lib/mongoid/identity.rb +62 -20
- data/lib/mongoid/inspection.rb +58 -0
- data/lib/mongoid/matchers.rb +20 -0
- data/lib/mongoid/multi_database.rb +11 -0
- data/lib/mongoid/nested_attributes.rb +41 -0
- data/lib/mongoid/paranoia.rb +3 -4
- data/lib/mongoid/paths.rb +1 -1
- data/lib/mongoid/persistence.rb +89 -90
- data/lib/mongoid/persistence/command.rb +20 -4
- data/lib/mongoid/persistence/insert.rb +13 -11
- data/lib/mongoid/persistence/insert_embedded.rb +8 -6
- data/lib/mongoid/persistence/remove.rb +6 -4
- data/lib/mongoid/persistence/remove_all.rb +6 -4
- data/lib/mongoid/persistence/remove_embedded.rb +8 -6
- data/lib/mongoid/persistence/update.rb +12 -10
- data/lib/mongoid/railtie.rb +2 -2
- data/lib/mongoid/railties/database.rake +10 -9
- data/lib/mongoid/relations.rb +104 -0
- data/lib/mongoid/relations/accessors.rb +154 -0
- data/lib/mongoid/relations/auto_save.rb +34 -0
- data/lib/mongoid/relations/binding.rb +24 -0
- data/lib/mongoid/relations/bindings.rb +9 -0
- data/lib/mongoid/relations/bindings/embedded/in.rb +77 -0
- data/lib/mongoid/relations/bindings/embedded/many.rb +93 -0
- data/lib/mongoid/relations/bindings/embedded/one.rb +65 -0
- data/lib/mongoid/relations/bindings/referenced/in.rb +78 -0
- data/lib/mongoid/relations/bindings/referenced/many.rb +93 -0
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +94 -0
- data/lib/mongoid/relations/bindings/referenced/one.rb +63 -0
- data/lib/mongoid/relations/builder.rb +41 -0
- data/lib/mongoid/relations/builders.rb +79 -0
- data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
- data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
- data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +116 -0
- data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
- data/lib/mongoid/relations/builders/referenced/in.rb +32 -0
- data/lib/mongoid/relations/builders/referenced/many.rb +26 -0
- data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
- data/lib/mongoid/relations/builders/referenced/one.rb +30 -0
- data/lib/mongoid/relations/cascading.rb +55 -0
- data/lib/mongoid/relations/cascading/delete.rb +19 -0
- data/lib/mongoid/relations/cascading/destroy.rb +19 -0
- data/lib/mongoid/relations/cascading/nullify.rb +18 -0
- data/lib/mongoid/relations/cascading/strategy.rb +26 -0
- data/lib/mongoid/relations/cyclic.rb +97 -0
- data/lib/mongoid/relations/embedded/in.rb +172 -0
- data/lib/mongoid/relations/embedded/many.rb +450 -0
- data/lib/mongoid/relations/embedded/one.rb +169 -0
- data/lib/mongoid/relations/macros.rb +302 -0
- data/lib/mongoid/relations/many.rb +185 -0
- data/lib/mongoid/relations/metadata.rb +529 -0
- data/lib/mongoid/relations/nested_builder.rb +52 -0
- data/lib/mongoid/relations/one.rb +29 -0
- data/lib/mongoid/relations/polymorphic.rb +54 -0
- data/lib/mongoid/relations/proxy.rb +122 -0
- data/lib/mongoid/relations/referenced/in.rb +214 -0
- data/lib/mongoid/relations/referenced/many.rb +358 -0
- data/lib/mongoid/relations/referenced/many_to_many.rb +379 -0
- data/lib/mongoid/relations/referenced/one.rb +204 -0
- data/lib/mongoid/relations/reflections.rb +45 -0
- data/lib/mongoid/safe.rb +11 -1
- data/lib/mongoid/safety.rb +122 -97
- data/lib/mongoid/scope.rb +14 -9
- data/lib/mongoid/state.rb +37 -3
- data/lib/mongoid/timestamps.rb +11 -0
- data/lib/mongoid/validations.rb +42 -3
- data/lib/mongoid/validations/associated.rb +8 -5
- data/lib/mongoid/validations/uniqueness.rb +23 -2
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/versioning.rb +25 -16
- data/lib/rails/generators/mongoid/model/templates/model.rb +3 -1
- metadata +95 -80
- data/lib/mongoid/associations.rb +0 -364
- data/lib/mongoid/associations/embedded_in.rb +0 -74
- data/lib/mongoid/associations/embeds_many.rb +0 -299
- data/lib/mongoid/associations/embeds_one.rb +0 -111
- data/lib/mongoid/associations/foreign_key.rb +0 -35
- data/lib/mongoid/associations/meta_data.rb +0 -38
- data/lib/mongoid/associations/options.rb +0 -78
- data/lib/mongoid/associations/proxy.rb +0 -60
- data/lib/mongoid/associations/referenced_in.rb +0 -70
- data/lib/mongoid/associations/references_many.rb +0 -254
- data/lib/mongoid/associations/references_many_as_array.rb +0 -128
- data/lib/mongoid/associations/references_one.rb +0 -104
- data/lib/mongoid/extensions/array/accessors.rb +0 -17
- data/lib/mongoid/extensions/array/assimilation.rb +0 -26
- data/lib/mongoid/extensions/hash/accessors.rb +0 -42
- data/lib/mongoid/extensions/hash/assimilation.rb +0 -40
- data/lib/mongoid/extensions/nil/assimilation.rb +0 -17
- data/lib/mongoid/memoization.rb +0 -33
@@ -21,6 +21,15 @@ module Mongoid #:nodoc:
|
|
21
21
|
end
|
22
22
|
alias update merge!
|
23
23
|
|
24
|
+
if RUBY_VERSION < '1.9'
|
25
|
+
def inspect
|
26
|
+
ret = self.keys.inject([]) do |ret, key|
|
27
|
+
ret << "#{key.inspect}=>#{self[key].inspect}"
|
28
|
+
end
|
29
|
+
"{#{ret.sort.join(', ')}}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
24
33
|
private
|
25
34
|
|
26
35
|
def try_to_typecast(key, value)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
|
4
|
+
# This module handles functionality for creating default scopes.
|
5
|
+
module DefaultScope
|
6
|
+
|
7
|
+
# Creates a default_scope for the +Document+, similar to ActiveRecord's
|
8
|
+
# default_scope. +DefaultScopes+ are proxied +Criteria+ objects that are
|
9
|
+
# applied by default to all queries for the class.
|
10
|
+
#
|
11
|
+
# @example Create a default scope.
|
12
|
+
#
|
13
|
+
# class Person
|
14
|
+
# include Mongoid::Document
|
15
|
+
# field :active, :type => Boolean
|
16
|
+
# field :count, :type => Integer
|
17
|
+
#
|
18
|
+
# default_scope :where => { :active => true }
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# @param [ Hash ] conditions The conditions to create with.
|
22
|
+
#
|
23
|
+
# @since 2.0.0.rc.1
|
24
|
+
def default_scope(conditions = {}, &block)
|
25
|
+
self.scope_stack << criteria.fuse(Scope.new(conditions, &block).conditions.scoped)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/mongoid/deprecation.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module Mongoid #:nodoc:
|
3
|
-
|
4
|
-
|
3
|
+
module Deprecation #:nodoc
|
4
|
+
extend self
|
5
5
|
|
6
6
|
# Alert of a deprecation. This will delegate to the logger and call warn on
|
7
7
|
# it.
|
@@ -10,12 +10,12 @@ module Mongoid #:nodoc:
|
|
10
10
|
#
|
11
11
|
# <tt>deprecation.alert("Method no longer used")</tt>
|
12
12
|
def alert(message)
|
13
|
-
|
13
|
+
logger.warn("Deprecation: #{message}")
|
14
14
|
end
|
15
15
|
|
16
16
|
protected
|
17
|
-
def
|
18
|
-
@logger
|
17
|
+
def logger
|
18
|
+
@logger ||= Mongoid::Logger.new
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
data/lib/mongoid/dirty.rb
CHANGED
@@ -46,7 +46,7 @@ module Mongoid #:nodoc:
|
|
46
46
|
# The old field value.
|
47
47
|
def attribute_was(name)
|
48
48
|
change = modifications[name]
|
49
|
-
change ? change[0] :
|
49
|
+
change ? change[0] : @attributes[name]
|
50
50
|
end
|
51
51
|
|
52
52
|
# Gets the names of all the fields that have changed in the document.
|
@@ -156,10 +156,8 @@ module Mongoid #:nodoc:
|
|
156
156
|
# The old field value.
|
157
157
|
def reset_attribute!(name)
|
158
158
|
value = attribute_was(name)
|
159
|
-
|
160
|
-
|
161
|
-
modifications.delete(name)
|
162
|
-
end
|
159
|
+
value ? @attributes[name] = value : @attributes.delete(name)
|
160
|
+
modifications.delete(name)
|
163
161
|
end
|
164
162
|
|
165
163
|
# Sets up the modifications hash. This occurs just after the document is
|
@@ -208,6 +206,7 @@ module Mongoid #:nodoc:
|
|
208
206
|
#
|
209
207
|
# All changes to the document.
|
210
208
|
def modifications
|
209
|
+
reset_modifications unless @modifications && @accessed
|
211
210
|
@accessed.each_pair do |field, value|
|
212
211
|
current = @attributes[field]
|
213
212
|
@modifications[field] = [ value, current ] if current != value
|
data/lib/mongoid/document.rb
CHANGED
@@ -1,90 +1,99 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module Mongoid #:nodoc:
|
3
|
+
|
4
|
+
# This is the base module for all domain objects that need to be persisted to
|
5
|
+
# the database as documents.
|
3
6
|
module Document
|
4
7
|
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
|
8
|
+
include Mongoid::Components
|
9
|
+
include Mongoid::MultiDatabase
|
7
10
|
|
8
|
-
|
11
|
+
included do
|
9
12
|
attr_reader :new_record
|
10
13
|
end
|
11
14
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
if attributes["_id"] || allocating
|
23
|
-
document = allocate
|
24
|
-
document.instance_variable_set(:@attributes, attributes)
|
25
|
-
document.setup_modifications
|
26
|
-
document
|
27
|
-
else
|
28
|
-
new(attrs)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
# Returns all types to query for when using this class as the base.
|
33
|
-
# *subclasses* is from activesupport. Note that a bug in *subclasses*
|
34
|
-
# causes the first call to only return direct children, hence
|
35
|
-
# the double call and unique.
|
36
|
-
def _types
|
37
|
-
@_type ||= [descendants + [self]].flatten.uniq.map(&:to_s)
|
38
|
-
end
|
15
|
+
# Default comparison is via the string version of the id.
|
16
|
+
#
|
17
|
+
# @example Compare two documents.
|
18
|
+
# person <=> other_person
|
19
|
+
#
|
20
|
+
# @param [ Document ] other The document to compare with.
|
21
|
+
#
|
22
|
+
# @return [ Integer ] -1, 0, 1.
|
23
|
+
def <=>(other)
|
24
|
+
id.to_s <=> other.id.to_s
|
39
25
|
end
|
40
26
|
|
41
27
|
# Performs equality checking on the document ids. For more robust
|
42
28
|
# equality checking please override this method.
|
29
|
+
#
|
30
|
+
# @example Compare for equality.
|
31
|
+
# document == other
|
32
|
+
#
|
33
|
+
# @param [ Document, Object ] other The other object to compare with.
|
34
|
+
#
|
35
|
+
# @return [ true, false ] True if the ids are equal, false if not.
|
43
36
|
def ==(other)
|
44
37
|
return false unless other.is_a?(Document)
|
45
|
-
id == other.id
|
38
|
+
id == other.id || equal?(other)
|
46
39
|
end
|
47
40
|
|
48
|
-
#
|
49
|
-
|
50
|
-
|
41
|
+
# Performs class equality checking.
|
42
|
+
#
|
43
|
+
# @example Compare the classes.
|
44
|
+
# document === other
|
45
|
+
#
|
46
|
+
# @param [ Document, Object ] other The other object to compare with.
|
47
|
+
#
|
48
|
+
# @return [ true, false ] True if the classes are equal, false if not.
|
49
|
+
def ===(other)
|
50
|
+
self.class == other.class
|
51
|
+
end
|
52
|
+
|
53
|
+
# Delegates to ==. Used when needing checks in hashes.
|
54
|
+
#
|
55
|
+
# @example Perform equality checking.
|
56
|
+
# document.eql?(other)
|
57
|
+
#
|
58
|
+
# @param [ Document, Object ] other The object to check against.
|
59
|
+
#
|
60
|
+
# @return [ true, false ] True if equal, false if not.
|
61
|
+
def eql?(other)
|
62
|
+
self == (other)
|
51
63
|
end
|
52
64
|
|
53
65
|
# Delegates to id in order to allow two records of the same type and id to
|
54
66
|
# work with something like:
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
67
|
+
#
|
68
|
+
# [ Person.find(1), Person.find(2), Person.find(3) ] &
|
69
|
+
# [ Person.find(1), Person.find(4) ] # => [ Person.find(1) ]
|
70
|
+
#
|
71
|
+
# @example Get the hash.
|
72
|
+
# document.hash
|
73
|
+
#
|
74
|
+
# @return [ Integer ] The hash of the document's id.
|
60
75
|
def hash
|
61
76
|
id.hash
|
62
77
|
end
|
63
78
|
|
64
|
-
#
|
65
|
-
#
|
66
|
-
#
|
79
|
+
# Return the attributes hash with indifferent access. Used mostly for
|
80
|
+
# convenience - use +Document#raw_attributes+ where you dont care if the
|
81
|
+
# keys are all strings.
|
67
82
|
#
|
68
|
-
#
|
83
|
+
# @example Get the attributes.
|
84
|
+
# person.attributes
|
69
85
|
#
|
70
|
-
#
|
71
|
-
# options: The association +Options+ for the child.
|
72
|
-
def assimilate(parent, options)
|
73
|
-
parentize(parent, options.name); notify; self
|
74
|
-
end
|
75
|
-
|
76
|
-
# Return the attributes hash with indifferent access.
|
86
|
+
# @return [ HashWithIndifferentAccess ] The attributes.
|
77
87
|
def attributes
|
78
88
|
@attributes.with_indifferent_access
|
79
89
|
end
|
80
90
|
|
81
|
-
# Clone the current +Document+. This will return all attributes with the
|
82
|
-
# exception of the document's id and versions.
|
83
|
-
def clone
|
84
|
-
self.class.instantiate(@attributes.except("_id").except("versions").dup, true)
|
85
|
-
end
|
86
|
-
|
87
91
|
# Generate an id for this +Document+.
|
92
|
+
#
|
93
|
+
# @example Create the id.
|
94
|
+
# person.identify
|
95
|
+
#
|
96
|
+
# @return [ BSON::ObjectId, String ] A newly created id.
|
88
97
|
def identify
|
89
98
|
Identity.new(self).create
|
90
99
|
end
|
@@ -96,96 +105,134 @@ module Mongoid #:nodoc:
|
|
96
105
|
# If a primary key is defined, the document's id will be set to that key,
|
97
106
|
# otherwise it will be set to a fresh +BSON::ObjectId+ string.
|
98
107
|
#
|
99
|
-
#
|
108
|
+
# @example Create a new document.
|
109
|
+
# Person.new(:title => "Sir")
|
110
|
+
#
|
111
|
+
# @param [ Hash ] attrs The attributes to set up the document with.
|
100
112
|
#
|
101
|
-
#
|
113
|
+
# @return [ Document ] A new document.
|
102
114
|
def initialize(attrs = nil)
|
103
115
|
@new_record = true
|
104
116
|
@attributes = default_attributes
|
105
|
-
process(attrs)
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
document
|
117
|
+
process(attrs) do |document|
|
118
|
+
yield self if block_given?
|
119
|
+
identify
|
120
|
+
run_callbacks(:initialize) { document }
|
110
121
|
end
|
111
122
|
end
|
112
123
|
|
113
|
-
#
|
114
|
-
def inspect
|
115
|
-
attrs = fields.map { |name, field| "#{name}: #{@attributes[name].inspect}" }
|
116
|
-
if Mongoid.allow_dynamic_fields
|
117
|
-
dynamic_keys = @attributes.keys - fields.keys - associations.keys - ["_id", "_type"]
|
118
|
-
attrs += dynamic_keys.map { |name| "#{name}: #{@attributes[name].inspect}" }
|
119
|
-
end
|
120
|
-
"#<#{self.class.name} _id: #{id}, #{attrs * ', '}>"
|
121
|
-
end
|
122
|
-
|
123
|
-
# Notify parent of an update.
|
124
|
+
# Return the attributes hash.
|
124
125
|
#
|
125
|
-
#
|
126
|
+
# @example Get the untouched attributes.
|
127
|
+
# person.raw_attributes
|
126
128
|
#
|
127
|
-
#
|
128
|
-
def notify
|
129
|
-
_parent.update_child(self) if _parent
|
130
|
-
end
|
131
|
-
|
132
|
-
# Return the attributes hash.
|
129
|
+
# @return [ Hash ] This document's attributes.
|
133
130
|
def raw_attributes
|
134
131
|
@attributes
|
135
132
|
end
|
136
133
|
|
137
|
-
# Reloads the +Document+ attributes from the database.
|
134
|
+
# Reloads the +Document+ attributes from the database. If the document has
|
135
|
+
# not been saved then an error will get raised if the configuration option
|
136
|
+
# was set.
|
137
|
+
#
|
138
|
+
# @example Reload the document.
|
139
|
+
# person.reload
|
140
|
+
#
|
141
|
+
# @raise [ Errors::DocumentNotFound ] If the document was deleted.
|
142
|
+
#
|
143
|
+
# @return [ Document ] The document, reloaded.
|
138
144
|
def reload
|
139
145
|
reloaded = collection.find_one(:_id => id)
|
140
146
|
if Mongoid.raise_not_found_error
|
141
147
|
raise Errors::DocumentNotFound.new(self.class, id) if reloaded.nil?
|
142
148
|
end
|
143
149
|
@attributes = {}.merge(reloaded || {})
|
144
|
-
|
150
|
+
tap do
|
151
|
+
relations.keys.each do |name|
|
152
|
+
if relation_exists?(name)
|
153
|
+
remove_instance_variable("@#{name}")
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
145
157
|
end
|
146
158
|
|
147
|
-
# Remove a child document from this parent
|
148
|
-
#
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
159
|
+
# Remove a child document from this parent. If an embeds one then set to
|
160
|
+
# nil, otherwise remove from the embeds many.
|
161
|
+
#
|
162
|
+
# This is called from the +RemoveEmbedded+ persistence command.
|
163
|
+
#
|
164
|
+
# @example Remove the child.
|
165
|
+
# document.remove_child(child)
|
166
|
+
#
|
167
|
+
# @param [ Document ] child The child (embedded) document to remove.
|
168
|
+
def remove_child(child)
|
169
|
+
name = child.metadata.name
|
170
|
+
if child.embedded_one?
|
171
|
+
remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}")
|
153
172
|
else
|
154
|
-
|
155
|
-
@attributes.remove(name, child.raw_attributes)
|
156
|
-
@attributes[name]
|
157
|
-
end
|
158
|
-
notify
|
173
|
+
send(name).delete(child)
|
159
174
|
end
|
160
175
|
end
|
161
176
|
|
162
|
-
# def remove_without_reset
|
163
|
-
# name = child.association_name
|
164
|
-
# @attributes.remove(name, child.raw_attributes)
|
165
|
-
# notify
|
166
|
-
# end
|
167
|
-
|
168
177
|
# Return an array with this +Document+ only in it.
|
178
|
+
#
|
179
|
+
# @example Return the document in an array.
|
180
|
+
# document.to_a
|
181
|
+
#
|
182
|
+
# @return [ Array<Document> ] An array with the document as its only item.
|
169
183
|
def to_a
|
170
184
|
[ self ]
|
171
185
|
end
|
172
186
|
|
173
|
-
#
|
174
|
-
#
|
175
|
-
# the
|
187
|
+
# Return a hash of the entire document hierarchy from this document and
|
188
|
+
# below. Used when the attributes are needed for everything and not just
|
189
|
+
# the current document.
|
176
190
|
#
|
177
|
-
#
|
191
|
+
# @example Get the full hierarchy.
|
192
|
+
# person.to_hash
|
178
193
|
#
|
179
|
-
#
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
194
|
+
# @return [ Hash ] A hash of all attributes in the hierarchy.
|
195
|
+
def to_hash
|
196
|
+
attributes = @attributes
|
197
|
+
attributes.tap do |attrs|
|
198
|
+
relations.select { |name, meta| meta.embedded? }.each do |name, meta|
|
199
|
+
relation = send(name, false, :continue => false)
|
200
|
+
attrs[name] = relation.to_hash unless relation.blank?
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
module ClassMethods #:nodoc:
|
206
|
+
|
207
|
+
# Instantiate a new object, only when loaded from the database or when
|
208
|
+
# the attributes have already been typecast.
|
209
|
+
#
|
210
|
+
# @example Create the document.
|
211
|
+
# Person.instantiate(:title => "Sir", :age => 30)
|
212
|
+
#
|
213
|
+
# @param [ Hash ] attrs The hash of attributes to instantiate with.
|
214
|
+
#
|
215
|
+
# @return [ Document ] A new document.
|
216
|
+
def instantiate(attrs = nil)
|
217
|
+
attributes = attrs || {}
|
218
|
+
if attributes["_id"]
|
219
|
+
allocate.tap do |doc|
|
220
|
+
doc.instance_variable_set(:@attributes, attributes)
|
221
|
+
doc.setup_modifications
|
222
|
+
end
|
223
|
+
else
|
224
|
+
new(attrs)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
# Returns all types to query for when using this class as the base.
|
229
|
+
#
|
230
|
+
# @example Get the types.
|
231
|
+
# document._types
|
232
|
+
#
|
233
|
+
# @return [ Array<Class> ] All subclasses of the current document.
|
234
|
+
def _types
|
235
|
+
@_type ||= [descendants + [self]].flatten.uniq.map(&:to_s)
|
189
236
|
end
|
190
237
|
end
|
191
238
|
end
|
data/lib/mongoid/extensions.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require "mongoid/extensions/time_conversions"
|
3
|
-
require "mongoid/extensions/array/accessors"
|
4
|
-
require "mongoid/extensions/array/assimilation"
|
5
3
|
require "mongoid/extensions/array/conversions"
|
6
4
|
require "mongoid/extensions/array/parentization"
|
7
5
|
require "mongoid/extensions/set/conversions"
|
@@ -12,14 +10,14 @@ require "mongoid/extensions/date/conversions"
|
|
12
10
|
require "mongoid/extensions/datetime/conversions"
|
13
11
|
require "mongoid/extensions/false_class/equality"
|
14
12
|
require "mongoid/extensions/float/conversions"
|
15
|
-
require "mongoid/extensions/hash/accessors"
|
16
|
-
require "mongoid/extensions/hash/assimilation"
|
17
13
|
require "mongoid/extensions/hash/conversions"
|
18
14
|
require "mongoid/extensions/hash/criteria_helpers"
|
19
15
|
require "mongoid/extensions/hash/scoping"
|
20
16
|
require "mongoid/extensions/integer/conversions"
|
21
|
-
require "mongoid/extensions/nil/
|
17
|
+
require "mongoid/extensions/nil/collectionization"
|
22
18
|
require "mongoid/extensions/object/conversions"
|
19
|
+
require "mongoid/extensions/object/reflections"
|
20
|
+
require "mongoid/extensions/object/yoda"
|
23
21
|
require "mongoid/extensions/proc/scoping"
|
24
22
|
require "mongoid/extensions/string/conversions"
|
25
23
|
require "mongoid/extensions/string/inflections"
|
@@ -29,8 +27,6 @@ require "mongoid/extensions/true_class/equality"
|
|
29
27
|
require "mongoid/extensions/object_id/conversions"
|
30
28
|
|
31
29
|
class Array #:nodoc
|
32
|
-
include Mongoid::Extensions::Array::Accessors
|
33
|
-
include Mongoid::Extensions::Array::Assimilation
|
34
30
|
include Mongoid::Extensions::Array::Conversions
|
35
31
|
include Mongoid::Extensions::Array::Parentization
|
36
32
|
end
|
@@ -70,8 +66,6 @@ class Float #:nodoc
|
|
70
66
|
end
|
71
67
|
|
72
68
|
class Hash #:nodoc
|
73
|
-
include Mongoid::Extensions::Hash::Accessors
|
74
|
-
include Mongoid::Extensions::Hash::Assimilation
|
75
69
|
include Mongoid::Extensions::Hash::CriteriaHelpers
|
76
70
|
include Mongoid::Extensions::Hash::Scoping
|
77
71
|
include Mongoid::Extensions::Hash::Conversions
|
@@ -82,11 +76,13 @@ class Integer #:nodoc
|
|
82
76
|
end
|
83
77
|
|
84
78
|
class NilClass #:nodoc
|
85
|
-
include Mongoid::Extensions::Nil::
|
79
|
+
include Mongoid::Extensions::Nil::Collectionization
|
86
80
|
end
|
87
81
|
|
88
82
|
class Object #:nodoc:
|
89
83
|
include Mongoid::Extensions::Object::Conversions
|
84
|
+
include Mongoid::Extensions::Object::Reflections
|
85
|
+
include Mongoid::Extensions::Object::Yoda
|
90
86
|
end
|
91
87
|
|
92
88
|
class Proc #:nodoc:
|
@@ -95,7 +91,7 @@ end
|
|
95
91
|
|
96
92
|
class String #:nodoc
|
97
93
|
include Mongoid::Extensions::String::Inflections
|
98
|
-
|
94
|
+
include Mongoid::Extensions::String::Conversions
|
99
95
|
end
|
100
96
|
|
101
97
|
class Symbol #:nodoc
|