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.
Files changed (50) hide show
  1. data/LICENSE +20 -20
  2. data/README.rdoc +34 -34
  3. data/bin/mmconsole +60 -60
  4. data/lib/mongo_mapper.rb +123 -123
  5. data/lib/mongo_mapper/document.rb +292 -292
  6. data/lib/mongo_mapper/embedded_document.rb +71 -71
  7. data/lib/mongo_mapper/plugins.rb +36 -36
  8. data/lib/mongo_mapper/plugins/associations.rb +115 -115
  9. data/lib/mongo_mapper/plugins/associations/base.rb +124 -124
  10. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +31 -31
  11. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +26 -26
  12. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -21
  13. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +39 -39
  14. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +160 -144
  15. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +29 -29
  16. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +130 -130
  17. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -32
  18. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -24
  19. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -14
  20. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +42 -42
  21. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +70 -70
  22. data/lib/mongo_mapper/plugins/associations/proxy.rb +125 -125
  23. data/lib/mongo_mapper/plugins/callbacks.rb +241 -241
  24. data/lib/mongo_mapper/plugins/clone.rb +13 -13
  25. data/lib/mongo_mapper/plugins/descendants.rb +16 -16
  26. data/lib/mongo_mapper/plugins/dirty.rb +119 -119
  27. data/lib/mongo_mapper/plugins/equality.rb +23 -23
  28. data/lib/mongo_mapper/plugins/identity_map.rb +123 -123
  29. data/lib/mongo_mapper/plugins/inspect.rb +14 -14
  30. data/lib/mongo_mapper/plugins/keys.rb +322 -322
  31. data/lib/mongo_mapper/plugins/keys/key.rb +53 -53
  32. data/lib/mongo_mapper/plugins/logger.rb +17 -17
  33. data/lib/mongo_mapper/plugins/modifiers.rb +111 -111
  34. data/lib/mongo_mapper/plugins/pagination.rb +24 -24
  35. data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -72
  36. data/lib/mongo_mapper/plugins/persistence.rb +96 -96
  37. data/lib/mongo_mapper/plugins/protected.rb +46 -46
  38. data/lib/mongo_mapper/plugins/rails.rb +57 -57
  39. data/lib/mongo_mapper/plugins/serialization.rb +92 -92
  40. data/lib/mongo_mapper/plugins/serialization/array.rb +56 -56
  41. data/lib/mongo_mapper/plugins/serialization/xml_serializer.rb +239 -239
  42. data/lib/mongo_mapper/plugins/timestamps.rb +21 -21
  43. data/lib/mongo_mapper/plugins/userstamps.rb +14 -14
  44. data/lib/mongo_mapper/plugins/validations.rb +46 -46
  45. data/lib/mongo_mapper/query.rb +28 -28
  46. data/lib/mongo_mapper/support.rb +197 -197
  47. data/lib/mongo_mapper/support/descendant_appends.rb +46 -46
  48. data/lib/mongo_mapper/support/find.rb +77 -77
  49. data/lib/mongo_mapper/version.rb +3 -3
  50. 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(scoped_options(options))
44
- end
45
-
46
- def first(options={})
47
- klass.first(scoped_options(options))
48
- end
49
-
50
- def last(options={})
51
- klass.last(scoped_options(options))
52
- end
53
-
54
- def count(options={})
55
- options.blank? ? ids.size : klass.count(scoped_options(options))
56
- end
57
-
58
- def destroy_all(options={})
59
- all(options).each do |doc|
60
- ids.delete(doc.id)
61
- doc.destroy
62
- end
63
- reset
64
- end
65
-
66
- def delete_all(options={})
67
- docs = all(options.merge(:select => ['_id']))
68
- docs.each { |doc| ids.delete(doc.id) }
69
- klass.delete(docs.map(&:id))
70
- reset
71
- end
72
-
73
- def nullify
74
- replace([])
75
- reset
76
- end
77
-
78
- def create(attrs={})
79
- doc = klass.create(attrs)
80
- unless doc.new?
81
- ids << doc.id
82
- owner.save
83
- reset
84
- end
85
- doc
86
- end
87
-
88
- def create!(attrs={})
89
- doc = klass.create!(attrs)
90
- unless doc.new?
91
- ids << doc.id
92
- owner.save
93
- reset
94
- end
95
- doc
96
- end
97
-
98
- def <<(*docs)
99
- flatten_deeper(docs).each do |doc|
100
- doc.save if doc.new?
101
- unless ids.include?(doc.id)
102
- ids << doc.id
103
- end
104
- end
105
- reset
106
- end
107
- alias_method :push, :<<
108
- alias_method :concat, :<<
109
-
110
- def replace(docs)
111
- doc_ids = docs.map do |doc|
112
- doc.save if doc.new?
113
- doc.id
114
- end
115
- ids.replace(doc_ids.uniq)
116
- reset
117
- end
118
-
119
- private
120
- def scoped_conditions
121
- {:_id => ids}
122
- end
123
-
124
- def scoped_options(options)
125
- association.query_options.merge(options).merge(scoped_conditions)
126
- end
127
-
128
- def scoped_ids(args)
129
- args.flatten.select do |id|
130
- id = ObjectId.to_mongo(id) if klass.using_object_id?
131
- ids.include?(id)
132
- end
133
- end
134
-
135
- def find_target
136
- ids.blank? ? [] : all
137
- end
138
-
139
- def ids
140
- owner[options[:in]]
141
- end
142
- end
143
- end
144
- end
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