ssherman-mongo_mapper 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +33 -0
  3. data/UPGRADES +7 -0
  4. data/bin/mmconsole +60 -0
  5. data/examples/attr_accessible.rb +22 -0
  6. data/examples/attr_protected.rb +22 -0
  7. data/examples/cache_key.rb +24 -0
  8. data/examples/custom_types.rb +24 -0
  9. data/examples/identity_map.rb +33 -0
  10. data/examples/identity_map/automatic.rb +8 -0
  11. data/examples/identity_map/middleware.rb +14 -0
  12. data/examples/keys.rb +40 -0
  13. data/examples/modifiers/set.rb +25 -0
  14. data/examples/plugins.rb +41 -0
  15. data/examples/querying.rb +35 -0
  16. data/examples/safe.rb +43 -0
  17. data/examples/scopes.rb +52 -0
  18. data/examples/validating/embedded_docs.rb +29 -0
  19. data/lib/mongo_mapper.rb +79 -0
  20. data/lib/mongo_mapper/connection.rb +83 -0
  21. data/lib/mongo_mapper/document.rb +41 -0
  22. data/lib/mongo_mapper/embedded_document.rb +31 -0
  23. data/lib/mongo_mapper/exceptions.rb +27 -0
  24. data/lib/mongo_mapper/extensions/array.rb +19 -0
  25. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  26. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  27. data/lib/mongo_mapper/extensions/date.rb +25 -0
  28. data/lib/mongo_mapper/extensions/float.rb +14 -0
  29. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  30. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  31. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  32. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  33. data/lib/mongo_mapper/extensions/object.rb +27 -0
  34. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  35. data/lib/mongo_mapper/extensions/set.rb +20 -0
  36. data/lib/mongo_mapper/extensions/string.rb +18 -0
  37. data/lib/mongo_mapper/extensions/time.rb +29 -0
  38. data/lib/mongo_mapper/plugins.rb +15 -0
  39. data/lib/mongo_mapper/plugins.rb~ +15 -0
  40. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  41. data/lib/mongo_mapper/plugins/associations.rb +97 -0
  42. data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
  43. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
  44. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
  45. data/lib/mongo_mapper/plugins/associations/collection.rb +27 -0
  46. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
  47. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +127 -0
  48. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  49. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +109 -0
  50. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  51. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  52. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  53. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  54. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
  55. data/lib/mongo_mapper/plugins/associations/proxy.rb +139 -0
  56. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  57. data/lib/mongo_mapper/plugins/callbacks.rb +241 -0
  58. data/lib/mongo_mapper/plugins/clone.rb +22 -0
  59. data/lib/mongo_mapper/plugins/descendants.rb +17 -0
  60. data/lib/mongo_mapper/plugins/dirty.rb +124 -0
  61. data/lib/mongo_mapper/plugins/document.rb +41 -0
  62. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  63. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  64. data/lib/mongo_mapper/plugins/embedded_document.rb +48 -0
  65. data/lib/mongo_mapper/plugins/equality.rb +17 -0
  66. data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
  67. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  68. data/lib/mongo_mapper/plugins/inspect.rb +15 -0
  69. data/lib/mongo_mapper/plugins/keys.rb +313 -0
  70. data/lib/mongo_mapper/plugins/keys/key.rb +59 -0
  71. data/lib/mongo_mapper/plugins/logger.rb +18 -0
  72. data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
  73. data/lib/mongo_mapper/plugins/pagination.rb +14 -0
  74. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  75. data/lib/mongo_mapper/plugins/protected.rb +53 -0
  76. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  77. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  78. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  79. data/lib/mongo_mapper/plugins/rails.rb +58 -0
  80. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  81. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  82. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  83. data/lib/mongo_mapper/plugins/serialization.rb +76 -0
  84. data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
  85. data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
  86. data/lib/mongo_mapper/plugins/validations.rb +50 -0
  87. data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
  88. data/lib/mongo_mapper/version.rb +4 -0
  89. data/rails/init.rb +19 -0
  90. data/test/_NOTE_ON_TESTING +1 -0
  91. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  92. data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
  93. data/test/functional/associations/test_in_array_proxy.rb +319 -0
  94. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  95. data/test/functional/associations/test_many_documents_proxy.rb +615 -0
  96. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  97. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  98. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  99. data/test/functional/associations/test_one_embedded_proxy.rb +81 -0
  100. data/test/functional/associations/test_one_proxy.rb +181 -0
  101. data/test/functional/test_accessible.rb +168 -0
  102. data/test/functional/test_associations.rb +44 -0
  103. data/test/functional/test_binary.rb +27 -0
  104. data/test/functional/test_caching.rb +76 -0
  105. data/test/functional/test_callbacks.rb +151 -0
  106. data/test/functional/test_dirty.rb +163 -0
  107. data/test/functional/test_document.rb +253 -0
  108. data/test/functional/test_dynamic_querying.rb +75 -0
  109. data/test/functional/test_embedded_document.rb +210 -0
  110. data/test/functional/test_identity_map.rb +514 -0
  111. data/test/functional/test_indexes.rb +42 -0
  112. data/test/functional/test_logger.rb +20 -0
  113. data/test/functional/test_modifiers.rb +416 -0
  114. data/test/functional/test_pagination.rb +91 -0
  115. data/test/functional/test_protected.rb +175 -0
  116. data/test/functional/test_querying.rb +873 -0
  117. data/test/functional/test_safe.rb +76 -0
  118. data/test/functional/test_sci.rb +230 -0
  119. data/test/functional/test_scopes.rb +171 -0
  120. data/test/functional/test_string_id_compatibility.rb +67 -0
  121. data/test/functional/test_timestamps.rb +62 -0
  122. data/test/functional/test_userstamps.rb +27 -0
  123. data/test/functional/test_validations.rb +342 -0
  124. data/test/models.rb +233 -0
  125. data/test/test_active_model_lint.rb +13 -0
  126. data/test/test_helper.rb +100 -0
  127. data/test/unit/associations/test_base.rb +212 -0
  128. data/test/unit/associations/test_proxy.rb +105 -0
  129. data/test/unit/serializers/test_json_serializer.rb +217 -0
  130. data/test/unit/test_clone.rb +69 -0
  131. data/test/unit/test_descendant_appends.rb +71 -0
  132. data/test/unit/test_document.rb +213 -0
  133. data/test/unit/test_dynamic_finder.rb +125 -0
  134. data/test/unit/test_embedded_document.rb +644 -0
  135. data/test/unit/test_extensions.rb +376 -0
  136. data/test/unit/test_key.rb +185 -0
  137. data/test/unit/test_keys.rb +89 -0
  138. data/test/unit/test_mongo_mapper.rb +110 -0
  139. data/test/unit/test_pagination.rb +11 -0
  140. data/test/unit/test_plugins.rb +50 -0
  141. data/test/unit/test_rails.rb +181 -0
  142. data/test/unit/test_rails_compatibility.rb +52 -0
  143. data/test/unit/test_serialization.rb +51 -0
  144. data/test/unit/test_time_zones.rb +39 -0
  145. data/test/unit/test_validations.rb +564 -0
  146. metadata +340 -0
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Float
5
+ def to_mongo(value)
6
+ value.nil? ? nil : value.to_f
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ class Float
13
+ extend MongoMapper::Extensions::Float
14
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Hash
5
+ def from_mongo(value)
6
+ HashWithIndifferentAccess.new(value || {})
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ class Hash
13
+ extend MongoMapper::Extensions::Hash
14
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Integer
5
+ def to_mongo(value)
6
+ value_to_i = value.to_i
7
+ if value_to_i == 0 && value != value_to_i
8
+ value.to_s =~ /^(0x|0b)?0+/ ? 0 : nil
9
+ else
10
+ value_to_i
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ class Integer
18
+ extend MongoMapper::Extensions::Integer
19
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: UTF-8
2
+ module Kernel
3
+ # Returns the object's singleton class.
4
+ def singleton_class
5
+ class << self
6
+ self
7
+ end
8
+ end unless respond_to?(:singleton_class) # exists in 1.9.2
9
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module NilClass
5
+ def to_mongo(value)
6
+ value
7
+ end
8
+
9
+ def from_mongo(value)
10
+ value
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ class NilClass
17
+ include MongoMapper::Extensions::NilClass
18
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Object
5
+ module ClassMethods
6
+ def to_mongo(value)
7
+ value
8
+ end
9
+
10
+ def from_mongo(value)
11
+ value
12
+ end
13
+ end
14
+
15
+ module InstanceMethods
16
+ def to_mongo
17
+ self.class.to_mongo(self)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ class Object
25
+ extend MongoMapper::Extensions::Object::ClassMethods
26
+ include MongoMapper::Extensions::Object::InstanceMethods
27
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module ObjectId
5
+ def to_mongo(value)
6
+ Plucky.to_object_id(value)
7
+ end
8
+
9
+ def from_mongo(value)
10
+ value
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ class ObjectId
17
+ extend MongoMapper::Extensions::ObjectId
18
+ end
19
+
20
+ class BSON::ObjectId
21
+ alias_method :original_to_json, :to_json
22
+
23
+ def as_json(options=nil)
24
+ to_s
25
+ end
26
+
27
+ def to_json(options = nil)
28
+ as_json.to_json
29
+ end
30
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: UTF-8
2
+ require 'set'
3
+
4
+ module MongoMapper
5
+ module Extensions
6
+ module Set
7
+ def to_mongo(value)
8
+ value.to_a
9
+ end
10
+
11
+ def from_mongo(value)
12
+ (value || []).to_set
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ class Set
19
+ extend MongoMapper::Extensions::Set
20
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module String
5
+ def to_mongo(value)
6
+ value.nil? ? nil : value.to_s
7
+ end
8
+
9
+ def from_mongo(value)
10
+ value.nil? ? nil : value.to_s
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ class String
17
+ extend MongoMapper::Extensions::String
18
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Extensions
4
+ module Time
5
+ def to_mongo(value)
6
+ if value.nil? || value == ''
7
+ nil
8
+ else
9
+ time_class = ::Time.try(:zone).present? ? ::Time.zone : ::Time
10
+ time = value.is_a?(::Time) ? value : time_class.parse(value.to_s)
11
+ # strip milliseconds as Ruby does micro and bson does milli and rounding rounded wrong
12
+ at(time.to_i).utc if time
13
+ end
14
+ end
15
+
16
+ def from_mongo(value)
17
+ if ::Time.try(:zone).present? && value.present?
18
+ value.in_time_zone(::Time.zone)
19
+ else
20
+ value
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ class Time
28
+ extend MongoMapper::Extensions::Time
29
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ def plugins
5
+ @plugins ||= []
6
+ end
7
+
8
+ def plugin(mod)
9
+ extend mod::ClassMethods if mod.constants.include?(:ClassMethods)
10
+ include mod::InstanceMethods if mod.constants.include?(:InstanceMethods)
11
+ mod.configure(self) if mod.respond_to?(:configure)
12
+ plugins << mod
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ def plugins
5
+ @plugins ||= []
6
+ end
7
+
8
+ def plugin(mod)
9
+ extend mod::ClassMethods if mod.const_defined?(:ClassMethods)
10
+ include mod::InstanceMethods if mod.const_defined?(:InstanceMethods)
11
+ mod.configure(self) if mod.respond_to?(:configure)
12
+ plugins << mod
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,44 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module Accessible
4
+ module ClassMethods
5
+ def attr_accessible(*attrs)
6
+ raise AccessibleOrProtected.new(name) if try(:protected_attributes?)
7
+ self.write_inheritable_attribute(:attr_accessible, Set.new(attrs) + (accessible_attributes || []))
8
+ end
9
+
10
+ def accessible_attributes?
11
+ !accessible_attributes.nil?
12
+ end
13
+
14
+ def accessible_attributes
15
+ self.read_inheritable_attribute(:attr_accessible)
16
+ end
17
+ end
18
+
19
+ module InstanceMethods
20
+ def assign(attrs={})
21
+ super(filter_inaccessible_attrs(attrs))
22
+ end
23
+
24
+ def update_attributes(attrs={})
25
+ super(filter_inaccessible_attrs(attrs))
26
+ end
27
+
28
+ def update_attributes!(attrs={})
29
+ super(filter_inaccessible_attrs(attrs))
30
+ end
31
+
32
+ def accessible_attributes
33
+ self.class.accessible_attributes
34
+ end
35
+
36
+ protected
37
+ def filter_inaccessible_attrs(attrs)
38
+ return attrs if accessible_attributes.blank? || attrs.blank?
39
+ attrs.dup.delete_if { |key, val| !accessible_attributes.include?(key.to_sym) }
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,97 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Associations
5
+ module ClassMethods
6
+ def inherited(subclass)
7
+ subclass.associations = associations.dup
8
+ super
9
+ end
10
+
11
+ def belongs_to(association_id, options={}, &extension)
12
+ create_association(:belongs_to, association_id, options, &extension)
13
+ end
14
+
15
+ def many(association_id, options={}, &extension)
16
+ create_association(:many, association_id, options, &extension)
17
+ end
18
+
19
+ def one(association_id, options={}, &extension)
20
+ create_association(:one, association_id, options, &extension)
21
+ end
22
+
23
+ def associations
24
+ @associations ||= HashWithIndifferentAccess.new
25
+ end
26
+
27
+ def associations=(hash)
28
+ @associations = hash
29
+ end
30
+
31
+ private
32
+ def create_association(type, name, options, &extension)
33
+ association = Associations::Base.new(type, name, options, &extension)
34
+ associations[association.name] = association
35
+
36
+ define_method(association.name) do
37
+ get_proxy(association)
38
+ end
39
+
40
+ define_method("#{association.name}=") do |value|
41
+ get_proxy(association).replace(value)
42
+ value
43
+ end
44
+
45
+ if association.one? || association.belongs_to?
46
+ define_method("#{association.name}?") do
47
+ get_proxy(association).present?
48
+ end
49
+ end
50
+
51
+ if association.options[:dependent] && association.many? && !association.embeddable?
52
+ after_destroy do |doc|
53
+ case association.options[:dependent]
54
+ when :destroy
55
+ doc.get_proxy(association).destroy_all
56
+ when :delete_all
57
+ doc.get_proxy(association).delete_all
58
+ when :nullify
59
+ doc.get_proxy(association).nullify
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ module InstanceMethods
67
+ def associations
68
+ self.class.associations
69
+ end
70
+
71
+ def embedded_associations
72
+ associations.select do |name, association|
73
+ association.embeddable?
74
+ end.map do |name, association|
75
+ association
76
+ end
77
+ end
78
+
79
+ def get_proxy(association)
80
+ unless proxy = self.instance_variable_get(association.ivar)
81
+ proxy = association.proxy_class.new(self, association)
82
+ self.instance_variable_set(association.ivar, proxy)
83
+ end
84
+ proxy
85
+ end
86
+
87
+ def save_to_collection(options={})
88
+ super
89
+ associations.each do |association_name, association|
90
+ proxy = get_proxy(association)
91
+ proxy.save_to_collection(options) if proxy.proxy_respond_to?(:save_to_collection)
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,124 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Associations
5
+ class Base
6
+ attr_reader :type, :name, :options, :query_options
7
+
8
+ # Options that should not be considered MongoDB query options/criteria
9
+ AssociationOptions = [:as, :class, :class_name, :dependent, :extend, :foreign_key, :in, :polymorphic]
10
+
11
+ def initialize(type, name, options={}, &extension)
12
+ @type, @name, @options, @query_options, @original_options = type, name, {}, {}, options
13
+ options.symbolize_keys!
14
+ options[:extend] = modularized_extensions(extension, options[:extend])
15
+ separate_options_and_conditions
16
+ end
17
+
18
+ def class_name
19
+ return @class_name if defined?(@class_name)
20
+
21
+ @class_name =
22
+ if cn = options[:class_name]
23
+ cn
24
+ elsif many?
25
+ name.to_s.singularize.camelize
26
+ else
27
+ name.to_s.camelize
28
+ end
29
+ end
30
+
31
+ def klass
32
+ @klass ||= options[:class] || class_name.constantize
33
+ end
34
+
35
+ def many?
36
+ @type == :many
37
+ end
38
+
39
+ def belongs_to?
40
+ @type == :belongs_to
41
+ end
42
+
43
+ def one?
44
+ @type == :one
45
+ end
46
+
47
+ def polymorphic?
48
+ !!@options[:polymorphic]
49
+ end
50
+
51
+ def as?
52
+ !!@options[:as]
53
+ end
54
+
55
+ def in_array?
56
+ !!@options[:in]
57
+ end
58
+
59
+ def embeddable?
60
+ (one? || many?) && klass.embeddable?
61
+ end
62
+
63
+ def type_key_name
64
+ many? ? '_type' : "#{as}_type"
65
+ end
66
+
67
+ def as
68
+ @options[:as] || self.name
69
+ end
70
+
71
+ def foreign_key
72
+ @options[:foreign_key] || "#{name}_id"
73
+ end
74
+
75
+ def ivar
76
+ @ivar ||= "@_#{name}"
77
+ end
78
+
79
+ # hate this, need to revisit
80
+ def proxy_class
81
+ return @proxy_class if defined?(@proxy_class)
82
+
83
+ @proxy_class =
84
+ if many?
85
+ if klass.embeddable?
86
+ polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy
87
+ else
88
+ if polymorphic?
89
+ ManyPolymorphicProxy
90
+ elsif as?
91
+ ManyDocumentsAsProxy
92
+ elsif in_array?
93
+ InArrayProxy
94
+ else
95
+ ManyDocumentsProxy
96
+ end
97
+ end
98
+ elsif one?
99
+ klass.embeddable? ? OneEmbeddedProxy : OneProxy
100
+ else
101
+ polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy
102
+ end
103
+ end
104
+
105
+ private
106
+ def separate_options_and_conditions
107
+ @original_options.each_pair do |key, value|
108
+ if AssociationOptions.include?(key)
109
+ @options[key] = value
110
+ else
111
+ @query_options[key] = value
112
+ end
113
+ end
114
+ end
115
+
116
+ def modularized_extensions(*extensions)
117
+ extensions.flatten.compact.map do |extension|
118
+ Proc === extension ? Module.new(&extension) : extension
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end