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,14 +1,14 @@
1
- # encoding: UTF-8
2
- module MongoMapper
3
- module Plugins
4
- module Clone
5
- module InstanceMethods
6
- def clone
7
- clone_attributes = self.attributes
8
- clone_attributes.delete("_id")
9
- self.class.new(clone_attributes)
10
- end
11
- end
12
- end
13
- end
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Clone
5
+ module InstanceMethods
6
+ def clone
7
+ clone_attributes = self.attributes
8
+ clone_attributes.delete("_id")
9
+ self.class.new(clone_attributes)
10
+ end
11
+ end
12
+ end
13
+ end
14
14
  end
@@ -1,17 +1,17 @@
1
- # encoding: UTF-8
2
- module MongoMapper
3
- module Plugins
4
- module Descendants
5
- module ClassMethods
6
- def inherited(descendant)
7
- descendants << descendant
8
- super
9
- end
10
-
11
- def descendants
12
- @descendants ||= []
13
- end
14
- end
15
- end
16
- end
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Descendants
5
+ module ClassMethods
6
+ def inherited(descendant)
7
+ descendants << descendant
8
+ super
9
+ end
10
+
11
+ def descendants
12
+ @descendants ||= []
13
+ end
14
+ end
15
+ end
16
+ end
17
17
  end
@@ -1,120 +1,120 @@
1
- # encoding: UTF-8
2
- module MongoMapper
3
- module Plugins
4
- module Dirty
5
- module InstanceMethods
6
- def method_missing(method, *args, &block)
7
- if method.to_s =~ /(_changed\?|_change|_will_change!|_was)$/
8
- method_suffix = $1
9
- key = method.to_s.gsub(method_suffix, '')
10
-
11
- if key_names.include?(key)
12
- case method_suffix
13
- when '_changed?'
14
- key_changed?(key)
15
- when '_change'
16
- key_change(key)
17
- when '_will_change!'
18
- key_will_change!(key)
19
- when '_was'
20
- key_was(key)
21
- end
22
- else
23
- super
24
- end
25
- else
26
- super
27
- end
28
- end
29
-
30
- def changed?
31
- !changed_keys.empty?
32
- end
33
-
34
- def changed
35
- changed_keys.keys
36
- end
37
-
38
- def changes
39
- changed.inject({}) { |h, key| h[key] = key_change(key); h }
40
- end
41
-
42
- def initialize(*args)
43
- super
44
- changed_keys.clear if args.first.blank? || !new?
45
- end
46
-
47
- def save(*args)
48
- if status = super
49
- changed_keys.clear
50
- end
51
- status
52
- end
53
-
54
- def save!(*args)
55
- status = super
56
- changed_keys.clear
57
- status
58
- end
59
-
60
- def reload(*args)
61
- document = super
62
- changed_keys.clear
63
- document
64
- end
65
-
66
- private
67
- def clone_key_value(key)
68
- value = read_key(key)
69
- value.duplicable? ? value.clone : value
70
- rescue TypeError, NoMethodError
71
- value
72
- end
73
-
74
- def changed_keys
75
- @changed_keys ||= {}
76
- end
77
-
78
- def key_changed?(key)
79
- changed_keys.include?(key)
80
- end
81
-
82
- def key_change(key)
83
- [changed_keys[key], __send__(key)] if key_changed?(key)
84
- end
85
-
86
- def key_was(key)
87
- key_changed?(key) ? changed_keys[key] : __send__(key)
88
- end
89
-
90
- def key_will_change!(key)
91
- changed_keys[key] = clone_key_value(key)
92
- end
93
-
94
- def write_key(key, value)
95
- key = key.to_s
96
-
97
- if changed_keys.include?(key)
98
- old = changed_keys[key]
99
- changed_keys.delete(key) unless value_changed?(key, old, value)
100
- else
101
- old = clone_key_value(key)
102
- changed_keys[key] = old if value_changed?(key, old, value)
103
- end
104
-
105
- super(key, value)
106
- end
107
-
108
- def value_changed?(key_name, old, value)
109
- key = keys[key_name]
110
-
111
- if key.number? && value.blank?
112
- value = nil
113
- end
114
-
115
- old != value
116
- end
117
- end
118
- end
119
- end
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Dirty
5
+ module InstanceMethods
6
+ def method_missing(method, *args, &block)
7
+ if method.to_s =~ /(_changed\?|_change|_will_change!|_was)$/
8
+ method_suffix = $1
9
+ key = method.to_s.gsub(method_suffix, '')
10
+
11
+ if key_names.include?(key)
12
+ case method_suffix
13
+ when '_changed?'
14
+ key_changed?(key)
15
+ when '_change'
16
+ key_change(key)
17
+ when '_will_change!'
18
+ key_will_change!(key)
19
+ when '_was'
20
+ key_was(key)
21
+ end
22
+ else
23
+ super
24
+ end
25
+ else
26
+ super
27
+ end
28
+ end
29
+
30
+ def changed?
31
+ !changed_keys.empty?
32
+ end
33
+
34
+ def changed
35
+ changed_keys.keys
36
+ end
37
+
38
+ def changes
39
+ changed.inject({}) { |h, key| h[key] = key_change(key); h }
40
+ end
41
+
42
+ def initialize(*args)
43
+ super
44
+ changed_keys.clear if args.first.blank? || !new?
45
+ end
46
+
47
+ def save(*args)
48
+ if status = super
49
+ changed_keys.clear
50
+ end
51
+ status
52
+ end
53
+
54
+ def save!(*args)
55
+ status = super
56
+ changed_keys.clear
57
+ status
58
+ end
59
+
60
+ def reload(*args)
61
+ document = super
62
+ changed_keys.clear
63
+ document
64
+ end
65
+
66
+ private
67
+ def clone_key_value(key)
68
+ value = read_key(key)
69
+ value.duplicable? ? value.clone : value
70
+ rescue TypeError, NoMethodError
71
+ value
72
+ end
73
+
74
+ def changed_keys
75
+ @changed_keys ||= {}
76
+ end
77
+
78
+ def key_changed?(key)
79
+ changed_keys.include?(key)
80
+ end
81
+
82
+ def key_change(key)
83
+ [changed_keys[key], __send__(key)] if key_changed?(key)
84
+ end
85
+
86
+ def key_was(key)
87
+ key_changed?(key) ? changed_keys[key] : __send__(key)
88
+ end
89
+
90
+ def key_will_change!(key)
91
+ changed_keys[key] = clone_key_value(key)
92
+ end
93
+
94
+ def write_key(key, value)
95
+ key = key.to_s
96
+
97
+ if changed_keys.include?(key)
98
+ old = changed_keys[key]
99
+ changed_keys.delete(key) unless value_changed?(key, old, value)
100
+ else
101
+ old = clone_key_value(key)
102
+ changed_keys[key] = old if value_changed?(key, old, value)
103
+ end
104
+
105
+ super(key, value)
106
+ end
107
+
108
+ def value_changed?(key_name, old, value)
109
+ key = keys[key_name]
110
+
111
+ if key.number? && value.blank?
112
+ value = nil
113
+ end
114
+
115
+ old != value
116
+ end
117
+ end
118
+ end
119
+ end
120
120
  end
@@ -1,24 +1,24 @@
1
- # encoding: UTF-8
2
- module MongoMapper
3
- module Plugins
4
- module Equality
5
- module InstanceMethods
6
- def ==(other)
7
- other.is_a?(self.class) && _id == other._id
8
- end
9
-
10
- def eql?(other)
11
- self == other
12
- end
13
-
14
- def equal?(other)
15
- object_id === other.object_id
16
- end
17
-
18
- def hash
19
- _id.hash
20
- end
21
- end
22
- end
23
- end
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Equality
5
+ module InstanceMethods
6
+ def ==(other)
7
+ other.is_a?(self.class) && _id == other._id
8
+ end
9
+
10
+ def eql?(other)
11
+ self == other
12
+ end
13
+
14
+ def equal?(other)
15
+ object_id === other.object_id
16
+ end
17
+
18
+ def hash
19
+ _id.hash
20
+ end
21
+ end
22
+ end
23
+ end
24
24
  end
@@ -1,124 +1,124 @@
1
- # encoding: UTF-8
2
- module MongoMapper
3
- module Plugins
4
- module IdentityMap
5
- def self.models
6
- @models ||= Set.new
7
- end
8
-
9
- def self.clear
10
- models.each { |m| m.identity_map.clear }
11
- end
12
-
13
- def self.configure(model)
14
- IdentityMap.models << model
15
- end
16
-
17
- module ClassMethods
18
- def inherited(descendant)
19
- descendant.identity_map = identity_map
20
- super
21
- end
22
-
23
- def identity_map
24
- @identity_map ||= {}
25
- end
26
-
27
- def identity_map=(v)
28
- @identity_map = v
29
- end
30
-
31
- def find_one(options={})
32
- query = query(options)
33
- criteria = query.criteria.to_hash
34
-
35
- if simple_find?(criteria) && identity_map.key?(criteria[:_id])
36
- identity_map[criteria[:_id]]
37
- else
38
- super.tap do |document|
39
- remove_documents_from_map(document) if selecting_fields?(query.options)
40
- end
41
- end
42
- end
43
-
44
- def find_many(options)
45
- super.tap do |documents|
46
- remove_documents_from_map(documents) if selecting_fields?(query(options).options)
47
- end
48
- end
49
-
50
- def load(attrs)
51
- return nil if attrs.nil?
52
- document = identity_map[attrs['_id']]
53
-
54
- if document.nil? || identity_map_off?
55
- document = super
56
- identity_map[document._id] = document if identity_map_on?
57
- end
58
-
59
- document
60
- end
61
-
62
- def identity_map_status
63
- defined?(@identity_map_status) ? @identity_map_status : true
64
- end
65
-
66
- def identity_map_on
67
- @identity_map_status = true
68
- end
69
-
70
- def identity_map_off
71
- @identity_map_status = false
72
- end
73
-
74
- def identity_map_on?
75
- identity_map_status
76
- end
77
-
78
- def identity_map_off?
79
- !identity_map_on?
80
- end
81
-
82
- def without_identity_map(&block)
83
- identity_map_off
84
- yield
85
- ensure
86
- identity_map_on
87
- end
88
-
89
- private
90
- def remove_documents_from_map(*documents)
91
- documents.flatten.compact.each do |document|
92
- identity_map.delete(document._id)
93
- end
94
- end
95
-
96
- def simple_find?(criteria)
97
- criteria.keys == [:_id] || criteria.keys.to_set == [:_id, :_type].to_set
98
- end
99
-
100
- def selecting_fields?(options)
101
- !options[:fields].nil?
102
- end
103
- end
104
-
105
- module InstanceMethods
106
- def identity_map
107
- self.class.identity_map
108
- end
109
-
110
- def save(*args)
111
- if result = super
112
- identity_map[_id] = self if self.class.identity_map_on?
113
- end
114
- result
115
- end
116
-
117
- def delete
118
- identity_map.delete(_id) if self.class.identity_map_on?
119
- super
120
- end
121
- end
122
- end
123
- end
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module IdentityMap
5
+ def self.models
6
+ @models ||= Set.new
7
+ end
8
+
9
+ def self.clear
10
+ models.each { |m| m.identity_map.clear }
11
+ end
12
+
13
+ def self.configure(model)
14
+ IdentityMap.models << model
15
+ end
16
+
17
+ module ClassMethods
18
+ def inherited(descendant)
19
+ descendant.identity_map = identity_map
20
+ super
21
+ end
22
+
23
+ def identity_map
24
+ @identity_map ||= {}
25
+ end
26
+
27
+ def identity_map=(v)
28
+ @identity_map = v
29
+ end
30
+
31
+ def find_one(options={})
32
+ query = query(options)
33
+ criteria = query.criteria.to_hash
34
+
35
+ if simple_find?(criteria) && identity_map.key?(criteria[:_id])
36
+ identity_map[criteria[:_id]]
37
+ else
38
+ super.tap do |document|
39
+ remove_documents_from_map(document) if selecting_fields?(query.options)
40
+ end
41
+ end
42
+ end
43
+
44
+ def find_many(options)
45
+ super.tap do |documents|
46
+ remove_documents_from_map(documents) if selecting_fields?(query(options).options)
47
+ end
48
+ end
49
+
50
+ def load(attrs)
51
+ return nil if attrs.nil?
52
+ document = identity_map[attrs['_id']]
53
+
54
+ if document.nil? || identity_map_off?
55
+ document = super
56
+ identity_map[document._id] = document if identity_map_on?
57
+ end
58
+
59
+ document
60
+ end
61
+
62
+ def identity_map_status
63
+ defined?(@identity_map_status) ? @identity_map_status : true
64
+ end
65
+
66
+ def identity_map_on
67
+ @identity_map_status = true
68
+ end
69
+
70
+ def identity_map_off
71
+ @identity_map_status = false
72
+ end
73
+
74
+ def identity_map_on?
75
+ identity_map_status
76
+ end
77
+
78
+ def identity_map_off?
79
+ !identity_map_on?
80
+ end
81
+
82
+ def without_identity_map(&block)
83
+ identity_map_off
84
+ yield
85
+ ensure
86
+ identity_map_on
87
+ end
88
+
89
+ private
90
+ def remove_documents_from_map(*documents)
91
+ documents.flatten.compact.each do |document|
92
+ identity_map.delete(document._id)
93
+ end
94
+ end
95
+
96
+ def simple_find?(criteria)
97
+ criteria.keys == [:_id] || criteria.keys.to_set == [:_id, :_type].to_set
98
+ end
99
+
100
+ def selecting_fields?(options)
101
+ !options[:fields].nil?
102
+ end
103
+ end
104
+
105
+ module InstanceMethods
106
+ def identity_map
107
+ self.class.identity_map
108
+ end
109
+
110
+ def save(*args)
111
+ if result = super
112
+ identity_map[_id] = self if self.class.identity_map_on?
113
+ end
114
+ result
115
+ end
116
+
117
+ def delete
118
+ identity_map.delete(_id) if self.class.identity_map_on?
119
+ super
120
+ end
121
+ end
122
+ end
123
+ end
124
124
  end