mongo_mapper_ign 0.7.7 → 0.7.8
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -20
- data/README.rdoc +34 -34
- data/bin/mmconsole +60 -60
- data/lib/mongo_mapper.rb +123 -123
- data/lib/mongo_mapper/document.rb +292 -292
- data/lib/mongo_mapper/embedded_document.rb +71 -71
- data/lib/mongo_mapper/plugins.rb +36 -36
- data/lib/mongo_mapper/plugins/associations.rb +115 -115
- data/lib/mongo_mapper/plugins/associations/base.rb +124 -124
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +31 -31
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +26 -26
- data/lib/mongo_mapper/plugins/associations/collection.rb +21 -21
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +39 -39
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +160 -144
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +29 -29
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +130 -130
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -32
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -24
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -14
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +42 -42
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +70 -70
- data/lib/mongo_mapper/plugins/associations/proxy.rb +125 -125
- data/lib/mongo_mapper/plugins/callbacks.rb +241 -241
- data/lib/mongo_mapper/plugins/clone.rb +13 -13
- data/lib/mongo_mapper/plugins/descendants.rb +16 -16
- data/lib/mongo_mapper/plugins/dirty.rb +119 -119
- data/lib/mongo_mapper/plugins/equality.rb +23 -23
- data/lib/mongo_mapper/plugins/identity_map.rb +123 -123
- data/lib/mongo_mapper/plugins/inspect.rb +14 -14
- data/lib/mongo_mapper/plugins/keys.rb +322 -322
- data/lib/mongo_mapper/plugins/keys/key.rb +53 -53
- data/lib/mongo_mapper/plugins/logger.rb +17 -17
- data/lib/mongo_mapper/plugins/modifiers.rb +111 -111
- data/lib/mongo_mapper/plugins/pagination.rb +24 -24
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -72
- data/lib/mongo_mapper/plugins/persistence.rb +96 -96
- data/lib/mongo_mapper/plugins/protected.rb +46 -46
- data/lib/mongo_mapper/plugins/rails.rb +57 -57
- data/lib/mongo_mapper/plugins/serialization.rb +92 -92
- data/lib/mongo_mapper/plugins/serialization/array.rb +56 -56
- data/lib/mongo_mapper/plugins/serialization/xml_serializer.rb +239 -239
- data/lib/mongo_mapper/plugins/timestamps.rb +21 -21
- data/lib/mongo_mapper/plugins/userstamps.rb +14 -14
- data/lib/mongo_mapper/plugins/validations.rb +46 -46
- data/lib/mongo_mapper/query.rb +28 -28
- data/lib/mongo_mapper/support.rb +197 -197
- data/lib/mongo_mapper/support/descendant_appends.rb +46 -46
- data/lib/mongo_mapper/support/find.rb +77 -77
- data/lib/mongo_mapper/version.rb +3 -3
- metadata +4 -25
@@ -1,31 +1,31 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
module MongoMapper
|
3
|
-
module Plugins
|
4
|
-
module Associations
|
5
|
-
class BelongsToPolymorphicProxy < Proxy
|
6
|
-
undef_method :object_id
|
7
|
-
|
8
|
-
def replace(doc)
|
9
|
-
if doc
|
10
|
-
doc.save if doc.new?
|
11
|
-
id, type = doc.id, doc.class.name
|
12
|
-
end
|
13
|
-
|
14
|
-
owner[association.foreign_key] = id
|
15
|
-
owner[association.type_key_name] = type
|
16
|
-
reset
|
17
|
-
end
|
18
|
-
|
19
|
-
protected
|
20
|
-
def find_target
|
21
|
-
return nil if association_class.nil? || owner[association.foreign_key].nil?
|
22
|
-
association_class.find_by_id(owner[association.foreign_key])
|
23
|
-
end
|
24
|
-
|
25
|
-
def association_class
|
26
|
-
proxy_owner[association.type_key_name] ? proxy_owner[association.type_key_name].constantize : nil
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class BelongsToPolymorphicProxy < Proxy
|
6
|
+
undef_method :object_id
|
7
|
+
|
8
|
+
def replace(doc)
|
9
|
+
if doc
|
10
|
+
doc.save if doc.new?
|
11
|
+
id, type = doc.id, doc.class.name
|
12
|
+
end
|
13
|
+
|
14
|
+
owner[association.foreign_key] = id
|
15
|
+
owner[association.type_key_name] = type
|
16
|
+
reset
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
def find_target
|
21
|
+
return nil if association_class.nil? || owner[association.foreign_key].nil?
|
22
|
+
association_class.find_by_id(owner[association.foreign_key])
|
23
|
+
end
|
24
|
+
|
25
|
+
def association_class
|
26
|
+
proxy_owner[association.type_key_name] ? proxy_owner[association.type_key_name].constantize : nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -1,26 +1,26 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
module MongoMapper
|
3
|
-
module Plugins
|
4
|
-
module Associations
|
5
|
-
class BelongsToProxy < Proxy
|
6
|
-
undef_method :object_id
|
7
|
-
|
8
|
-
def replace(doc)
|
9
|
-
if doc
|
10
|
-
doc.save if doc.new?
|
11
|
-
id = doc.id
|
12
|
-
end
|
13
|
-
|
14
|
-
owner[association.foreign_key] = id
|
15
|
-
reset
|
16
|
-
end
|
17
|
-
|
18
|
-
protected
|
19
|
-
def find_target
|
20
|
-
return nil if owner[association.foreign_key].nil?
|
21
|
-
klass.find_by_id(owner[association.foreign_key])
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class BelongsToProxy < Proxy
|
6
|
+
undef_method :object_id
|
7
|
+
|
8
|
+
def replace(doc)
|
9
|
+
if doc
|
10
|
+
doc.save if doc.new?
|
11
|
+
id = doc.id
|
12
|
+
end
|
13
|
+
|
14
|
+
owner[association.foreign_key] = id
|
15
|
+
reset
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
def find_target
|
20
|
+
return nil if owner[association.foreign_key].nil?
|
21
|
+
klass.find_by_id(owner[association.foreign_key])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,22 +1,22 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
module MongoMapper
|
3
|
-
module Plugins
|
4
|
-
module Associations
|
5
|
-
class Collection < Proxy
|
6
|
-
def to_ary
|
7
|
-
load_target
|
8
|
-
if target.is_a?(Array)
|
9
|
-
target.to_ary
|
10
|
-
else
|
11
|
-
Array(target)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def reset
|
16
|
-
super
|
17
|
-
target = []
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class Collection < Proxy
|
6
|
+
def to_ary
|
7
|
+
load_target
|
8
|
+
if target.is_a?(Array)
|
9
|
+
target.to_ary
|
10
|
+
else
|
11
|
+
Array(target)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def reset
|
16
|
+
super
|
17
|
+
target = []
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
22
|
end
|
@@ -1,40 +1,40 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
module MongoMapper
|
3
|
-
module Plugins
|
4
|
-
module Associations
|
5
|
-
class EmbeddedCollection < Collection
|
6
|
-
def build(attributes={})
|
7
|
-
doc = klass.new(attributes)
|
8
|
-
assign_references(doc)
|
9
|
-
self << doc
|
10
|
-
doc
|
11
|
-
end
|
12
|
-
|
13
|
-
def find(id)
|
14
|
-
load_target
|
15
|
-
target.detect { |item| item.id.to_s == id || item.id == id }
|
16
|
-
end
|
17
|
-
|
18
|
-
def count
|
19
|
-
load_target
|
20
|
-
target.size
|
21
|
-
end
|
22
|
-
|
23
|
-
def <<(*docs)
|
24
|
-
load_target
|
25
|
-
docs.each do |doc|
|
26
|
-
assign_references(doc)
|
27
|
-
target << doc
|
28
|
-
end
|
29
|
-
end
|
30
|
-
alias_method :push, :<<
|
31
|
-
alias_method :concat, :<<
|
32
|
-
|
33
|
-
private
|
34
|
-
def assign_references(*docs)
|
35
|
-
docs.each { |doc| doc._parent_document = owner }
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class EmbeddedCollection < Collection
|
6
|
+
def build(attributes={})
|
7
|
+
doc = klass.new(attributes)
|
8
|
+
assign_references(doc)
|
9
|
+
self << doc
|
10
|
+
doc
|
11
|
+
end
|
12
|
+
|
13
|
+
def find(id)
|
14
|
+
load_target
|
15
|
+
target.detect { |item| item.id.to_s == id || item.id == id }
|
16
|
+
end
|
17
|
+
|
18
|
+
def count
|
19
|
+
load_target
|
20
|
+
target.size
|
21
|
+
end
|
22
|
+
|
23
|
+
def <<(*docs)
|
24
|
+
load_target
|
25
|
+
docs.each do |doc|
|
26
|
+
assign_references(doc)
|
27
|
+
target << doc
|
28
|
+
end
|
29
|
+
end
|
30
|
+
alias_method :push, :<<
|
31
|
+
alias_method :concat, :<<
|
32
|
+
|
33
|
+
private
|
34
|
+
def assign_references(*docs)
|
35
|
+
docs.each { |doc| doc._parent_document = owner }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
40
|
end
|
@@ -1,145 +1,161 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
module MongoMapper
|
3
|
-
module Plugins
|
4
|
-
module Associations
|
5
|
-
class InArrayProxy < Collection
|
6
|
-
include Support::Find
|
7
|
-
|
8
|
-
def find(*args)
|
9
|
-
options = args.extract_options!
|
10
|
-
|
11
|
-
case args.first
|
12
|
-
when :first
|
13
|
-
first(options)
|
14
|
-
when :last
|
15
|
-
last(options)
|
16
|
-
when :all
|
17
|
-
all(options)
|
18
|
-
else
|
19
|
-
klass.find(*scoped_ids(args) << scoped_options(options))
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def find!(*args)
|
24
|
-
options = args.extract_options!
|
25
|
-
|
26
|
-
case args.first
|
27
|
-
when :first
|
28
|
-
first(options)
|
29
|
-
when :last
|
30
|
-
last(options)
|
31
|
-
when :all
|
32
|
-
all(options)
|
33
|
-
else
|
34
|
-
klass.find!(*scoped_ids(args) << scoped_options(options))
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def paginate(options)
|
39
|
-
klass.paginate(scoped_options(options))
|
40
|
-
end
|
41
|
-
|
42
|
-
def all(options={})
|
43
|
-
klass.all(
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
end
|
65
|
-
|
66
|
-
def
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
doc
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Associations
|
5
|
+
class InArrayProxy < Collection
|
6
|
+
include Support::Find
|
7
|
+
|
8
|
+
def find(*args)
|
9
|
+
options = args.extract_options!
|
10
|
+
|
11
|
+
case args.first
|
12
|
+
when :first
|
13
|
+
first(options)
|
14
|
+
when :last
|
15
|
+
last(options)
|
16
|
+
when :all
|
17
|
+
all(options)
|
18
|
+
else
|
19
|
+
klass.find(*scoped_ids(args) << scoped_options(options))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def find!(*args)
|
24
|
+
options = args.extract_options!
|
25
|
+
|
26
|
+
case args.first
|
27
|
+
when :first
|
28
|
+
first(options)
|
29
|
+
when :last
|
30
|
+
last(options)
|
31
|
+
when :all
|
32
|
+
all(options)
|
33
|
+
else
|
34
|
+
klass.find!(*scoped_ids(args) << scoped_options(options))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def paginate(options)
|
39
|
+
klass.paginate(scoped_options(options))
|
40
|
+
end
|
41
|
+
|
42
|
+
def all(options={})
|
43
|
+
random_order = klass.all(scoped_conditions)
|
44
|
+
|
45
|
+
if (scoped_options(options)[:preserve_order])
|
46
|
+
preserve_order = Array.new
|
47
|
+
scoped_conditions[:_id].each do |id|
|
48
|
+
random_order.each do |element|
|
49
|
+
if (id.eql?(element._id))
|
50
|
+
preserve_order << element
|
51
|
+
break
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
return_value = preserve_order
|
56
|
+
else
|
57
|
+
return_value = random_order
|
58
|
+
end
|
59
|
+
return return_value
|
60
|
+
end
|
61
|
+
|
62
|
+
def first(options={})
|
63
|
+
klass.first(scoped_options(options))
|
64
|
+
end
|
65
|
+
|
66
|
+
def last(options={})
|
67
|
+
klass.last(scoped_options(options))
|
68
|
+
end
|
69
|
+
|
70
|
+
def count(options={})
|
71
|
+
options.blank? ? ids.size : klass.count(scoped_options(options))
|
72
|
+
end
|
73
|
+
|
74
|
+
def destroy_all(options={})
|
75
|
+
all(options).each do |doc|
|
76
|
+
ids.delete(doc.id)
|
77
|
+
doc.destroy
|
78
|
+
end
|
79
|
+
reset
|
80
|
+
end
|
81
|
+
|
82
|
+
def delete_all(options={})
|
83
|
+
docs = all(options.merge(:select => ['_id']))
|
84
|
+
docs.each { |doc| ids.delete(doc.id) }
|
85
|
+
klass.delete(docs.map(&:id))
|
86
|
+
reset
|
87
|
+
end
|
88
|
+
|
89
|
+
def nullify
|
90
|
+
replace([])
|
91
|
+
reset
|
92
|
+
end
|
93
|
+
|
94
|
+
def create(attrs={})
|
95
|
+
doc = klass.create(attrs)
|
96
|
+
unless doc.new?
|
97
|
+
ids << doc.id
|
98
|
+
owner.save
|
99
|
+
reset
|
100
|
+
end
|
101
|
+
doc
|
102
|
+
end
|
103
|
+
|
104
|
+
def create!(attrs={})
|
105
|
+
doc = klass.create!(attrs)
|
106
|
+
unless doc.new?
|
107
|
+
ids << doc.id
|
108
|
+
owner.save
|
109
|
+
reset
|
110
|
+
end
|
111
|
+
doc
|
112
|
+
end
|
113
|
+
|
114
|
+
def <<(*docs)
|
115
|
+
flatten_deeper(docs).each do |doc|
|
116
|
+
doc.save if doc.new?
|
117
|
+
unless ids.include?(doc.id)
|
118
|
+
ids << doc.id
|
119
|
+
end
|
120
|
+
end
|
121
|
+
reset
|
122
|
+
end
|
123
|
+
alias_method :push, :<<
|
124
|
+
alias_method :concat, :<<
|
125
|
+
|
126
|
+
def replace(docs)
|
127
|
+
doc_ids = docs.map do |doc|
|
128
|
+
doc.save if doc.new?
|
129
|
+
doc.id
|
130
|
+
end
|
131
|
+
ids.replace(doc_ids.uniq)
|
132
|
+
reset
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
def scoped_conditions
|
137
|
+
{:_id => ids}
|
138
|
+
end
|
139
|
+
|
140
|
+
def scoped_options(options)
|
141
|
+
association.query_options.merge(options).merge(scoped_conditions)
|
142
|
+
end
|
143
|
+
|
144
|
+
def scoped_ids(args)
|
145
|
+
args.flatten.select do |id|
|
146
|
+
id = ObjectId.to_mongo(id) if klass.using_object_id?
|
147
|
+
ids.include?(id)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def find_target
|
152
|
+
ids.blank? ? [] : all
|
153
|
+
end
|
154
|
+
|
155
|
+
def ids
|
156
|
+
owner[options[:in]]
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
145
161
|
end
|