mongo_mapper-rails3 0.7.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. data/.gitignore +10 -0
  2. data/Gemfile +15 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +60 -0
  5. data/Rakefile +58 -0
  6. data/VERSION +1 -0
  7. data/bin/mmconsole +60 -0
  8. data/lib/mongo_mapper.rb +131 -0
  9. data/lib/mongo_mapper/document.rb +439 -0
  10. data/lib/mongo_mapper/embedded_document.rb +68 -0
  11. data/lib/mongo_mapper/plugins.rb +30 -0
  12. data/lib/mongo_mapper/plugins/associations.rb +106 -0
  13. data/lib/mongo_mapper/plugins/associations/base.rb +123 -0
  14. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
  15. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
  16. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  17. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +50 -0
  18. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +141 -0
  19. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  20. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +120 -0
  21. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  22. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  23. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  24. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
  25. data/lib/mongo_mapper/plugins/associations/proxy.rb +119 -0
  26. data/lib/mongo_mapper/plugins/callbacks.rb +87 -0
  27. data/lib/mongo_mapper/plugins/clone.rb +14 -0
  28. data/lib/mongo_mapper/plugins/descendants.rb +17 -0
  29. data/lib/mongo_mapper/plugins/dirty.rb +120 -0
  30. data/lib/mongo_mapper/plugins/equality.rb +24 -0
  31. data/lib/mongo_mapper/plugins/identity_map.rb +124 -0
  32. data/lib/mongo_mapper/plugins/inspect.rb +15 -0
  33. data/lib/mongo_mapper/plugins/keys.rb +310 -0
  34. data/lib/mongo_mapper/plugins/logger.rb +19 -0
  35. data/lib/mongo_mapper/plugins/pagination.rb +26 -0
  36. data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
  37. data/lib/mongo_mapper/plugins/protected.rb +46 -0
  38. data/lib/mongo_mapper/plugins/rails.rb +46 -0
  39. data/lib/mongo_mapper/plugins/serialization.rb +50 -0
  40. data/lib/mongo_mapper/plugins/validations.rb +88 -0
  41. data/lib/mongo_mapper/query.rb +130 -0
  42. data/lib/mongo_mapper/support.rb +217 -0
  43. data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
  44. data/lib/mongo_mapper/support/find.rb +77 -0
  45. data/mongo_mapper-rails3.gemspec +208 -0
  46. data/performance/read_write.rb +52 -0
  47. data/specs.watchr +51 -0
  48. data/test/NOTE_ON_TESTING +1 -0
  49. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  50. data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
  51. data/test/functional/associations/test_in_array_proxy.rb +321 -0
  52. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  53. data/test/functional/associations/test_many_documents_proxy.rb +453 -0
  54. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  55. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  56. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  57. data/test/functional/associations/test_one_proxy.rb +161 -0
  58. data/test/functional/test_associations.rb +44 -0
  59. data/test/functional/test_binary.rb +27 -0
  60. data/test/functional/test_callbacks.rb +81 -0
  61. data/test/functional/test_dirty.rb +163 -0
  62. data/test/functional/test_document.rb +1244 -0
  63. data/test/functional/test_embedded_document.rb +125 -0
  64. data/test/functional/test_identity_map.rb +508 -0
  65. data/test/functional/test_logger.rb +20 -0
  66. data/test/functional/test_modifiers.rb +252 -0
  67. data/test/functional/test_pagination.rb +93 -0
  68. data/test/functional/test_protected.rb +161 -0
  69. data/test/functional/test_string_id_compatibility.rb +67 -0
  70. data/test/functional/test_validations.rb +329 -0
  71. data/test/models.rb +232 -0
  72. data/test/support/custom_matchers.rb +55 -0
  73. data/test/support/timing.rb +16 -0
  74. data/test/test_helper.rb +59 -0
  75. data/test/unit/associations/test_base.rb +207 -0
  76. data/test/unit/associations/test_proxy.rb +105 -0
  77. data/test/unit/serializers/test_json_serializer.rb +189 -0
  78. data/test/unit/test_descendant_appends.rb +71 -0
  79. data/test/unit/test_document.rb +231 -0
  80. data/test/unit/test_dynamic_finder.rb +123 -0
  81. data/test/unit/test_embedded_document.rb +663 -0
  82. data/test/unit/test_keys.rb +169 -0
  83. data/test/unit/test_lint.rb +8 -0
  84. data/test/unit/test_mongo_mapper.rb +125 -0
  85. data/test/unit/test_pagination.rb +160 -0
  86. data/test/unit/test_plugins.rb +51 -0
  87. data/test/unit/test_query.rb +334 -0
  88. data/test/unit/test_rails.rb +123 -0
  89. data/test/unit/test_rails_compatibility.rb +57 -0
  90. data/test/unit/test_serialization.rb +51 -0
  91. data/test/unit/test_support.rb +362 -0
  92. data/test/unit/test_time_zones.rb +39 -0
  93. data/test/unit/test_validations.rb +557 -0
  94. metadata +344 -0
@@ -0,0 +1,68 @@
1
+ module MongoMapper
2
+ module EmbeddedDocument
3
+ extend Support::DescendantAppends
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ extend Plugins
7
+
8
+ plugin Plugins::Associations
9
+ plugin Plugins::Clone
10
+ plugin Plugins::Descendants
11
+ plugin Plugins::Equality
12
+ plugin Plugins::Inspect
13
+ plugin Plugins::Keys
14
+ plugin Plugins::Logger
15
+ plugin Plugins::Protected
16
+ plugin Plugins::Rails
17
+ plugin Plugins::Serialization
18
+ plugin Plugins::Validations
19
+
20
+ attr_accessor :_root_document, :_parent_document
21
+ end
22
+
23
+ module ClassMethods
24
+ def embeddable?
25
+ true
26
+ end
27
+
28
+ def embedded_in(owner_name)
29
+ define_method(owner_name) { _parent_document }
30
+ end
31
+ end
32
+
33
+ module InstanceMethods
34
+ def initialize(attrs={}, from_database=false)
35
+ unless attrs.nil?
36
+ provided_keys = attrs.keys.map { |k| k.to_s }
37
+ unless provided_keys.include?('_id') || provided_keys.include?('id')
38
+ write_key :_id, Mongo::ObjectID.new
39
+ end
40
+ end
41
+
42
+ assign_type_if_present
43
+
44
+ if from_database
45
+ @new = false
46
+ self.attributes = attrs
47
+ else
48
+ @new = true
49
+ assign(attrs)
50
+ end
51
+ end
52
+
53
+ def save(options={})
54
+ if result = _root_document.try(:save, options)
55
+ @new = false
56
+ end
57
+ result
58
+ end
59
+
60
+ def save!(options={})
61
+ if result = _root_document.try(:save!, options)
62
+ @new = false
63
+ end
64
+ result
65
+ end
66
+ end # InstanceMethods
67
+ end # EmbeddedDocument
68
+ end # MongoMapper
@@ -0,0 +1,30 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ def plugins
4
+ @plugins ||= []
5
+ end
6
+
7
+ def plugin(mod)
8
+ include mod
9
+ mod.configure(self) if mod.respond_to?(:configure)
10
+ plugins << mod
11
+ end
12
+
13
+ autoload :Callbacks, 'mongo_mapper/plugins/callbacks'
14
+ autoload :Clone, 'mongo_mapper/plugins/clone'
15
+ autoload :Descendants, 'mongo_mapper/plugins/descendants'
16
+ autoload :Dirty, 'mongo_mapper/plugins/dirty'
17
+ autoload :Equality, 'mongo_mapper/plugins/equality'
18
+ autoload :IdentityMap, 'mongo_mapper/plugins/identity_map'
19
+ autoload :Inspect, 'mongo_mapper/plugins/inspect'
20
+ autoload :Keys, 'mongo_mapper/plugins/keys'
21
+ autoload :Logger, 'mongo_mapper/plugins/logger'
22
+ autoload :Protected, 'mongo_mapper/plugins/protected'
23
+ autoload :Rails, 'mongo_mapper/plugins/rails'
24
+ autoload :Serialization, 'mongo_mapper/plugins/serialization'
25
+ autoload :Validations, 'mongo_mapper/plugins/validations'
26
+ end
27
+ end
28
+
29
+ require 'mongo_mapper/plugins/associations'
30
+ require 'mongo_mapper/plugins/pagination'
@@ -0,0 +1,106 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module Associations
4
+ extend ActiveSupport::Concern
5
+ module ClassMethods
6
+ def belongs_to(association_id, options={}, &extension)
7
+ create_association(:belongs_to, association_id, options, &extension)
8
+ end
9
+
10
+ def many(association_id, options={}, &extension)
11
+ create_association(:many, association_id, options, &extension)
12
+ end
13
+
14
+ def one(association_id, options={}, &extension)
15
+ create_association(:one, association_id, options, &extension)
16
+ end
17
+
18
+ def associations
19
+ @associations ||= HashWithIndifferentAccess.new
20
+ end
21
+
22
+ def associations=(hash)
23
+ @associations = hash
24
+ end
25
+
26
+ def inherited(subclass)
27
+ subclass.associations = associations.dup
28
+ super
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
+ # @api private?
72
+ def embedded_associations
73
+ associations.select do |name, association|
74
+ association.embeddable?
75
+ end.map do |name, association|
76
+ association
77
+ end
78
+ end
79
+
80
+ def get_proxy(association)
81
+ unless proxy = self.instance_variable_get(association.ivar)
82
+ proxy = association.proxy_class.new(self, association)
83
+ self.instance_variable_set(association.ivar, proxy)
84
+ end
85
+
86
+ proxy
87
+ end
88
+ end
89
+
90
+ autoload :Base, 'mongo_mapper/plugins/associations/base'
91
+ autoload :Collection, 'mongo_mapper/plugins/associations/collection'
92
+ autoload :EmbeddedCollection, 'mongo_mapper/plugins/associations/embedded_collection'
93
+ autoload :ManyDocumentsProxy, 'mongo_mapper/plugins/associations/many_documents_proxy'
94
+ autoload :BelongsToProxy, 'mongo_mapper/plugins/associations/belongs_to_proxy'
95
+ autoload :BelongsToPolymorphicProxy, 'mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy'
96
+ autoload :ManyPolymorphicProxy, 'mongo_mapper/plugins/associations/many_polymorphic_proxy'
97
+ autoload :ManyEmbeddedProxy, 'mongo_mapper/plugins/associations/many_embedded_proxy'
98
+ autoload :ManyEmbeddedPolymorphicProxy, 'mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy'
99
+ autoload :ManyDocumentsAsProxy, 'mongo_mapper/plugins/associations/many_documents_as_proxy'
100
+ autoload :OneProxy, 'mongo_mapper/plugins/associations/one_proxy'
101
+ autoload :InArrayProxy, 'mongo_mapper/plugins/associations/in_array_proxy'
102
+ end
103
+ end
104
+ end
105
+
106
+ require 'mongo_mapper/plugins/associations/proxy'
@@ -0,0 +1,123 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module Associations
4
+ class Base
5
+ attr_reader :type, :name, :options, :query_options
6
+
7
+ # Options that should not be considered MongoDB query options/criteria
8
+ AssociationOptions = [:as, :class, :class_name, :dependent, :extend, :foreign_key, :in, :polymorphic]
9
+
10
+ def initialize(type, name, options={}, &extension)
11
+ @type, @name, @options, @query_options, @original_options = type, name, {}, {}, options
12
+ options.symbolize_keys!
13
+ options[:extend] = modularized_extensions(extension, options[:extend])
14
+ separate_options_and_conditions
15
+ end
16
+
17
+ def class_name
18
+ return @class_name if defined?(@class_name)
19
+
20
+ @class_name =
21
+ if cn = options[:class_name]
22
+ cn
23
+ elsif many?
24
+ name.to_s.singularize.camelize
25
+ else
26
+ name.to_s.camelize
27
+ end
28
+ end
29
+
30
+ def klass
31
+ @klass ||= options[:class] || class_name.constantize
32
+ end
33
+
34
+ def many?
35
+ @type == :many
36
+ end
37
+
38
+ def belongs_to?
39
+ @type == :belongs_to
40
+ end
41
+
42
+ def one?
43
+ @type == :one
44
+ end
45
+
46
+ def polymorphic?
47
+ !!@options[:polymorphic]
48
+ end
49
+
50
+ def as?
51
+ !!@options[:as]
52
+ end
53
+
54
+ def in_array?
55
+ !!@options[:in]
56
+ end
57
+
58
+ def embeddable?
59
+ many? && klass.embeddable?
60
+ end
61
+
62
+ def type_key_name
63
+ many? ? '_type' : "#{as}_type"
64
+ end
65
+
66
+ def as
67
+ @options[:as] || self.name
68
+ end
69
+
70
+ def foreign_key
71
+ @options[:foreign_key] || "#{name}_id"
72
+ end
73
+
74
+ def ivar
75
+ @ivar ||= "@_#{name}"
76
+ end
77
+
78
+ # hate this, need to revisit
79
+ def proxy_class
80
+ return @proxy_class if defined?(@proxy_class)
81
+
82
+ @proxy_class =
83
+ if many?
84
+ if klass.embeddable?
85
+ polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy
86
+ else
87
+ if polymorphic?
88
+ ManyPolymorphicProxy
89
+ elsif as?
90
+ ManyDocumentsAsProxy
91
+ elsif in_array?
92
+ InArrayProxy
93
+ else
94
+ ManyDocumentsProxy
95
+ end
96
+ end
97
+ elsif one?
98
+ OneProxy
99
+ else
100
+ polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy
101
+ end
102
+ end
103
+
104
+ private
105
+ def separate_options_and_conditions
106
+ @original_options.each_pair do |key, value|
107
+ if AssociationOptions.include?(key)
108
+ @options[key] = value
109
+ else
110
+ @query_options[key] = value
111
+ end
112
+ end
113
+ end
114
+
115
+ def modularized_extensions(*extensions)
116
+ extensions.flatten.compact.map do |extension|
117
+ Proc === extension ? Module.new(&extension) : extension
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,30 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module Associations
4
+ class BelongsToPolymorphicProxy < Proxy
5
+ undef_method :object_id
6
+
7
+ def replace(doc)
8
+ if doc
9
+ doc.save if doc.new?
10
+ id, type = doc.id, doc.class.name
11
+ end
12
+
13
+ owner[association.foreign_key] = id
14
+ owner[association.type_key_name] = type
15
+ reset
16
+ end
17
+
18
+ protected
19
+ def find_target
20
+ return nil if association_class.nil? || owner[association.foreign_key].nil?
21
+ association_class.find_by_id(owner[association.foreign_key])
22
+ end
23
+
24
+ def association_class
25
+ proxy_owner[association.type_key_name] ? proxy_owner[association.type_key_name].constantize : nil
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module Associations
4
+ class BelongsToProxy < Proxy
5
+ undef_method :object_id
6
+
7
+ def replace(doc)
8
+ if doc
9
+ doc.save if doc.new?
10
+ id = doc.id
11
+ end
12
+
13
+ owner[association.foreign_key] = id
14
+ reset
15
+ end
16
+
17
+ protected
18
+ def find_target
19
+ return nil if owner[association.foreign_key].nil?
20
+ klass.find_by_id(owner[association.foreign_key])
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module Associations
4
+ class Collection < Proxy
5
+ def to_ary
6
+ load_target
7
+ if target.is_a?(Array)
8
+ target.to_ary
9
+ else
10
+ Array(target)
11
+ end
12
+ end
13
+
14
+ def reset
15
+ super
16
+ target = []
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,50 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module Associations
4
+ class EmbeddedCollection < Collection
5
+ def build(attributes={})
6
+ doc = klass.new(attributes)
7
+ assign_references(doc)
8
+ self << doc
9
+ doc
10
+ end
11
+
12
+ def find(id)
13
+ load_target
14
+ target.detect { |item| item.id.to_s == id || item.id == id }
15
+ end
16
+
17
+ def count
18
+ load_target
19
+ target.size
20
+ end
21
+
22
+ def <<(*docs)
23
+ load_target
24
+ docs.each do |doc|
25
+ assign_references(doc)
26
+ target << doc
27
+ end
28
+ end
29
+ alias_method :push, :<<
30
+ alias_method :concat, :<<
31
+
32
+ private
33
+ def _root_document
34
+ if owner.respond_to?(:_root_document)
35
+ owner._root_document
36
+ else
37
+ owner
38
+ end
39
+ end
40
+
41
+ def assign_references(*docs)
42
+ docs.each do |doc|
43
+ doc._root_document = _root_document
44
+ doc._parent_document = owner
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end