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,54 +1,54 @@
1
- # encoding: UTF-8
2
- module MongoMapper
3
- module Plugins
4
- module Keys
5
- class Key
6
- attr_accessor :name, :type, :options, :default_value
7
-
8
- def initialize(*args)
9
- options = args.extract_options!
10
- @name, @type = args.shift.to_s, args.shift
11
- self.options = (options || {}).symbolize_keys
12
- self.default_value = self.options[:default]
13
- end
14
-
15
- def ==(other)
16
- @name == other.name && @type == other.type
17
- end
18
-
19
- def embeddable?
20
- type.respond_to?(:embeddable?) && type.embeddable? ? true : false
21
- end
22
-
23
- def number?
24
- [Integer, Float].include?(type)
25
- end
26
-
27
- def get(value)
28
- if value.nil? && !default_value.nil?
29
- if default_value.respond_to?(:call)
30
- return default_value.call
31
- else
32
- return default_value
33
- end
34
- end
35
-
36
- type.from_mongo(value)
37
- end
38
-
39
- def set(value)
40
- type.to_mongo(value).tap do |values|
41
- if options[:typecast].present?
42
- values.map! { |v| typecast_class.to_mongo(v) }
43
- end
44
- end
45
- end
46
-
47
- private
48
- def typecast_class
49
- options[:typecast].constantize
50
- end
51
- end
52
- end
53
- end
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Keys
5
+ class Key
6
+ attr_accessor :name, :type, :options, :default_value
7
+
8
+ def initialize(*args)
9
+ options = args.extract_options!
10
+ @name, @type = args.shift.to_s, args.shift
11
+ self.options = (options || {}).symbolize_keys
12
+ self.default_value = self.options[:default]
13
+ end
14
+
15
+ def ==(other)
16
+ @name == other.name && @type == other.type
17
+ end
18
+
19
+ def embeddable?
20
+ type.respond_to?(:embeddable?) && type.embeddable? ? true : false
21
+ end
22
+
23
+ def number?
24
+ [Integer, Float].include?(type)
25
+ end
26
+
27
+ def get(value)
28
+ if value.nil? && !default_value.nil?
29
+ if default_value.respond_to?(:call)
30
+ return default_value.call
31
+ else
32
+ return default_value
33
+ end
34
+ end
35
+
36
+ type.from_mongo(value)
37
+ end
38
+
39
+ def set(value)
40
+ type.to_mongo(value).tap do |values|
41
+ if options[:typecast].present?
42
+ values.map! { |v| typecast_class.to_mongo(v) }
43
+ end
44
+ end
45
+ end
46
+
47
+ private
48
+ def typecast_class
49
+ options[:typecast].constantize
50
+ end
51
+ end
52
+ end
53
+ end
54
54
  end
@@ -1,18 +1,18 @@
1
- # encoding: UTF-8
2
- module MongoMapper
3
- module Plugins
4
- module Logger
5
- module ClassMethods
6
- def logger
7
- MongoMapper.logger
8
- end
9
- end
10
-
11
- module InstanceMethods
12
- def logger
13
- self.class.logger
14
- end
15
- end
16
- end
17
- end
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Logger
5
+ module ClassMethods
6
+ def logger
7
+ MongoMapper.logger
8
+ end
9
+ end
10
+
11
+ module InstanceMethods
12
+ def logger
13
+ self.class.logger
14
+ end
15
+ end
16
+ end
17
+ end
18
18
  end
@@ -1,112 +1,112 @@
1
- # encoding: UTF-8
2
- module MongoMapper
3
- module Plugins
4
- module Modifiers
5
- module ClassMethods
6
- def increment(*args)
7
- modifier_update('$inc', args)
8
- end
9
-
10
- def decrement(*args)
11
- criteria, keys = criteria_and_keys_from_args(args)
12
- values, to_decrement = keys.values, {}
13
- keys.keys.each_with_index { |k, i| to_decrement[k] = -values[i].abs }
14
- collection.update(criteria, {'$inc' => to_decrement}, :multi => true)
15
- end
16
-
17
- def set(*args)
18
- criteria, updates = criteria_and_keys_from_args(args)
19
- updates.each do |key, value|
20
- updates[key] = keys[key].set(value) if key?(key)
21
- end
22
- collection.update(criteria, {'$set' => updates}, :multi => true)
23
- end
24
-
25
- def unset(*args)
26
- if args[0].is_a?(Hash)
27
- criteria, keys = args.shift, args
28
- else
29
- keys, ids = args.partition { |arg| arg.is_a?(Symbol) }
30
- criteria = {:id => ids}
31
- end
32
-
33
- criteria = query(criteria).criteria.to_hash
34
- modifiers = keys.inject({}) { |hash, key| hash[key] = 1; hash }
35
- collection.update(criteria, {'$unset' => modifiers}, :multi => true)
36
- end
37
-
38
- def push(*args)
39
- modifier_update('$push', args)
40
- end
41
-
42
- def push_all(*args)
43
- modifier_update('$pushAll', args)
44
- end
45
-
46
- def add_to_set(*args)
47
- modifier_update('$addToSet', args)
48
- end
49
- alias push_uniq add_to_set
50
-
51
- def pull(*args)
52
- modifier_update('$pull', args)
53
- end
54
-
55
- def pull_all(*args)
56
- modifier_update('$pullAll', args)
57
- end
58
-
59
- def pop(*args)
60
- modifier_update('$pop', args)
61
- end
62
-
63
- private
64
- def modifier_update(modifier, args)
65
- criteria, updates = criteria_and_keys_from_args(args)
66
- collection.update(criteria, {modifier => updates}, :multi => true)
67
- end
68
-
69
- def criteria_and_keys_from_args(args)
70
- keys = args.pop
71
- criteria = args[0].is_a?(Hash) ? args[0] : {:id => args}
72
- [query(criteria).criteria.to_hash, keys]
73
- end
74
- end
75
-
76
- module InstanceMethods
77
- def unset(*keys)
78
- self.class.unset(id, *keys)
79
- end
80
-
81
- def increment(hash)
82
- self.class.increment(id, hash)
83
- end
84
-
85
- def decrement(hash)
86
- self.class.decrement(id, hash)
87
- end
88
-
89
- def set(hash)
90
- self.class.set(id, hash)
91
- end
92
-
93
- def push(hash)
94
- self.class.push(id, hash)
95
- end
96
-
97
- def pull(hash)
98
- self.class.pull(id, hash)
99
- end
100
-
101
- def add_to_set(hash)
102
- self.class.push_uniq(id, hash)
103
- end
104
- alias push_uniq add_to_set
105
-
106
- def pop(hash)
107
- self.class.pop(id, hash)
108
- end
109
- end
110
- end
111
- end
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Modifiers
5
+ module ClassMethods
6
+ def increment(*args)
7
+ modifier_update('$inc', args)
8
+ end
9
+
10
+ def decrement(*args)
11
+ criteria, keys = criteria_and_keys_from_args(args)
12
+ values, to_decrement = keys.values, {}
13
+ keys.keys.each_with_index { |k, i| to_decrement[k] = -values[i].abs }
14
+ collection.update(criteria, {'$inc' => to_decrement}, :multi => true)
15
+ end
16
+
17
+ def set(*args)
18
+ criteria, updates = criteria_and_keys_from_args(args)
19
+ updates.each do |key, value|
20
+ updates[key] = keys[key].set(value) if key?(key)
21
+ end
22
+ collection.update(criteria, {'$set' => updates}, :multi => true)
23
+ end
24
+
25
+ def unset(*args)
26
+ if args[0].is_a?(Hash)
27
+ criteria, keys = args.shift, args
28
+ else
29
+ keys, ids = args.partition { |arg| arg.is_a?(Symbol) }
30
+ criteria = {:id => ids}
31
+ end
32
+
33
+ criteria = query(criteria).criteria.to_hash
34
+ modifiers = keys.inject({}) { |hash, key| hash[key] = 1; hash }
35
+ collection.update(criteria, {'$unset' => modifiers}, :multi => true)
36
+ end
37
+
38
+ def push(*args)
39
+ modifier_update('$push', args)
40
+ end
41
+
42
+ def push_all(*args)
43
+ modifier_update('$pushAll', args)
44
+ end
45
+
46
+ def add_to_set(*args)
47
+ modifier_update('$addToSet', args)
48
+ end
49
+ alias push_uniq add_to_set
50
+
51
+ def pull(*args)
52
+ modifier_update('$pull', args)
53
+ end
54
+
55
+ def pull_all(*args)
56
+ modifier_update('$pullAll', args)
57
+ end
58
+
59
+ def pop(*args)
60
+ modifier_update('$pop', args)
61
+ end
62
+
63
+ private
64
+ def modifier_update(modifier, args)
65
+ criteria, updates = criteria_and_keys_from_args(args)
66
+ collection.update(criteria, {modifier => updates}, :multi => true)
67
+ end
68
+
69
+ def criteria_and_keys_from_args(args)
70
+ keys = args.pop
71
+ criteria = args[0].is_a?(Hash) ? args[0] : {:id => args}
72
+ [query(criteria).criteria.to_hash, keys]
73
+ end
74
+ end
75
+
76
+ module InstanceMethods
77
+ def unset(*keys)
78
+ self.class.unset(id, *keys)
79
+ end
80
+
81
+ def increment(hash)
82
+ self.class.increment(id, hash)
83
+ end
84
+
85
+ def decrement(hash)
86
+ self.class.decrement(id, hash)
87
+ end
88
+
89
+ def set(hash)
90
+ self.class.set(id, hash)
91
+ end
92
+
93
+ def push(hash)
94
+ self.class.push(id, hash)
95
+ end
96
+
97
+ def pull(hash)
98
+ self.class.pull(id, hash)
99
+ end
100
+
101
+ def add_to_set(hash)
102
+ self.class.push_uniq(id, hash)
103
+ end
104
+ alias push_uniq add_to_set
105
+
106
+ def pop(hash)
107
+ self.class.pop(id, hash)
108
+ end
109
+ end
110
+ end
111
+ end
112
112
  end
@@ -1,25 +1,25 @@
1
- # encoding: UTF-8
2
- module MongoMapper
3
- module Plugins
4
- module Pagination
5
- module ClassMethods
6
- def per_page
7
- 25
8
- end
9
-
10
- def paginate(options)
11
- per_page = options.delete(:per_page) || self.per_page
12
- page = options.delete(:page)
13
- total_entries = count(options)
14
- pagination = Pagination::Proxy.new(total_entries, page, per_page)
15
-
16
- options.update(:limit => pagination.limit, :skip => pagination.skip)
17
- pagination.subject = find_many(options)
18
- pagination
19
- end
20
- end
21
- end
22
- end
23
- end
24
-
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Pagination
5
+ module ClassMethods
6
+ def per_page
7
+ 25
8
+ end
9
+
10
+ def paginate(options)
11
+ per_page = options.delete(:per_page) || self.per_page
12
+ page = options.delete(:page)
13
+ total_entries = count(options)
14
+ pagination = Pagination::Proxy.new(total_entries, page, per_page)
15
+
16
+ options.update(:limit => pagination.limit, :skip => pagination.skip)
17
+ pagination.subject = find_many(options)
18
+ pagination
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
25
  require 'mongo_mapper/plugins/pagination/proxy'
@@ -1,73 +1,73 @@
1
- # encoding: UTF-8
2
- module MongoMapper
3
- module Plugins
4
- module Pagination
5
- class Proxy
6
- instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|respond_to\?|proxy_|^object_id$)/ }
7
-
8
- attr_accessor :subject
9
- attr_reader :total_entries, :per_page, :current_page
10
- alias limit per_page
11
-
12
- def initialize(total_entries, current_page, per_page=nil)
13
- @total_entries = total_entries.to_i
14
- self.per_page = per_page
15
- self.current_page = current_page
16
- end
17
-
18
- def total_pages
19
- (total_entries / per_page.to_f).ceil
20
- end
21
-
22
- def out_of_bounds?
23
- current_page > total_pages
24
- end
25
-
26
- def previous_page
27
- current_page > 1 ? (current_page - 1) : nil
28
- end
29
-
30
- def next_page
31
- current_page < total_pages ? (current_page + 1) : nil
32
- end
33
-
34
- def skip
35
- (current_page - 1) * per_page
36
- end
37
- alias offset skip # for will paginate support
38
-
39
- def send(method, *args, &block)
40
- if respond_to?(method)
41
- super
42
- else
43
- subject.send(method, *args, &block)
44
- end
45
- end
46
-
47
- def ===(other)
48
- other === subject
49
- end
50
-
51
- def method_missing(name, *args, &block)
52
- @subject.send(name, *args, &block)
53
- end
54
-
55
- def respond_to?(name, *args, &block)
56
- super || @subject.respond_to?(name, *args, &block)
57
- end
58
-
59
- private
60
- def per_page=(value)
61
- value = 25 if value.blank?
62
- @per_page = value.to_i
63
- end
64
-
65
- def current_page=(value)
66
- value = value.to_i
67
- value = 1 if value < 1
68
- @current_page = value
69
- end
70
- end
71
- end
72
- end
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Pagination
5
+ class Proxy
6
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|respond_to\?|proxy_|^object_id$)/ }
7
+
8
+ attr_accessor :subject
9
+ attr_reader :total_entries, :per_page, :current_page
10
+ alias limit per_page
11
+
12
+ def initialize(total_entries, current_page, per_page=nil)
13
+ @total_entries = total_entries.to_i
14
+ self.per_page = per_page
15
+ self.current_page = current_page
16
+ end
17
+
18
+ def total_pages
19
+ (total_entries / per_page.to_f).ceil
20
+ end
21
+
22
+ def out_of_bounds?
23
+ current_page > total_pages
24
+ end
25
+
26
+ def previous_page
27
+ current_page > 1 ? (current_page - 1) : nil
28
+ end
29
+
30
+ def next_page
31
+ current_page < total_pages ? (current_page + 1) : nil
32
+ end
33
+
34
+ def skip
35
+ (current_page - 1) * per_page
36
+ end
37
+ alias offset skip # for will paginate support
38
+
39
+ def send(method, *args, &block)
40
+ if respond_to?(method)
41
+ super
42
+ else
43
+ subject.send(method, *args, &block)
44
+ end
45
+ end
46
+
47
+ def ===(other)
48
+ other === subject
49
+ end
50
+
51
+ def method_missing(name, *args, &block)
52
+ @subject.send(name, *args, &block)
53
+ end
54
+
55
+ def respond_to?(name, *args, &block)
56
+ super || @subject.respond_to?(name, *args, &block)
57
+ end
58
+
59
+ private
60
+ def per_page=(value)
61
+ value = 25 if value.blank?
62
+ @per_page = value.to_i
63
+ end
64
+
65
+ def current_page=(value)
66
+ value = value.to_i
67
+ value = 1 if value < 1
68
+ @current_page = value
69
+ end
70
+ end
71
+ end
72
+ end
73
73
  end