thorsson-mongo_mapper 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +27 -0
  3. data/bin/mmconsole +60 -0
  4. data/examples/keys.rb +40 -0
  5. data/examples/modifiers/set.rb +25 -0
  6. data/examples/plugins.rb +41 -0
  7. data/examples/querying.rb +35 -0
  8. data/examples/scopes.rb +52 -0
  9. data/lib/mongo_mapper.rb +79 -0
  10. data/lib/mongo_mapper/connection.rb +83 -0
  11. data/lib/mongo_mapper/document.rb +41 -0
  12. data/lib/mongo_mapper/embedded_document.rb +31 -0
  13. data/lib/mongo_mapper/exceptions.rb +27 -0
  14. data/lib/mongo_mapper/extensions/array.rb +19 -0
  15. data/lib/mongo_mapper/extensions/binary.rb +22 -0
  16. data/lib/mongo_mapper/extensions/boolean.rb +44 -0
  17. data/lib/mongo_mapper/extensions/date.rb +25 -0
  18. data/lib/mongo_mapper/extensions/float.rb +14 -0
  19. data/lib/mongo_mapper/extensions/hash.rb +14 -0
  20. data/lib/mongo_mapper/extensions/integer.rb +19 -0
  21. data/lib/mongo_mapper/extensions/kernel.rb +9 -0
  22. data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
  23. data/lib/mongo_mapper/extensions/object.rb +27 -0
  24. data/lib/mongo_mapper/extensions/object_id.rb +30 -0
  25. data/lib/mongo_mapper/extensions/set.rb +20 -0
  26. data/lib/mongo_mapper/extensions/string.rb +18 -0
  27. data/lib/mongo_mapper/extensions/time.rb +29 -0
  28. data/lib/mongo_mapper/plugins.rb +16 -0
  29. data/lib/mongo_mapper/plugins/accessible.rb +44 -0
  30. data/lib/mongo_mapper/plugins/associations.rb +97 -0
  31. data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
  32. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
  33. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
  34. data/lib/mongo_mapper/plugins/associations/collection.rb +22 -0
  35. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
  36. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +127 -0
  37. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  38. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +110 -0
  39. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
  40. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
  41. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
  42. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
  43. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
  44. data/lib/mongo_mapper/plugins/associations/proxy.rb +126 -0
  45. data/lib/mongo_mapper/plugins/caching.rb +21 -0
  46. data/lib/mongo_mapper/plugins/callbacks.rb +241 -0
  47. data/lib/mongo_mapper/plugins/clone.rb +19 -0
  48. data/lib/mongo_mapper/plugins/descendants.rb +17 -0
  49. data/lib/mongo_mapper/plugins/dirty.rb +120 -0
  50. data/lib/mongo_mapper/plugins/document.rb +41 -0
  51. data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
  52. data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
  53. data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
  54. data/lib/mongo_mapper/plugins/equality.rb +17 -0
  55. data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
  56. data/lib/mongo_mapper/plugins/indexes.rb +12 -0
  57. data/lib/mongo_mapper/plugins/inspect.rb +15 -0
  58. data/lib/mongo_mapper/plugins/keys.rb +309 -0
  59. data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
  60. data/lib/mongo_mapper/plugins/logger.rb +18 -0
  61. data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
  62. data/lib/mongo_mapper/plugins/pagination.rb +14 -0
  63. data/lib/mongo_mapper/plugins/persistence.rb +69 -0
  64. data/lib/mongo_mapper/plugins/protected.rb +53 -0
  65. data/lib/mongo_mapper/plugins/querying.rb +176 -0
  66. data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
  67. data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
  68. data/lib/mongo_mapper/plugins/rails.rb +58 -0
  69. data/lib/mongo_mapper/plugins/safe.rb +28 -0
  70. data/lib/mongo_mapper/plugins/sci.rb +32 -0
  71. data/lib/mongo_mapper/plugins/scopes.rb +21 -0
  72. data/lib/mongo_mapper/plugins/serialization.rb +76 -0
  73. data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
  74. data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
  75. data/lib/mongo_mapper/plugins/validations.rb +50 -0
  76. data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
  77. data/lib/mongo_mapper/version.rb +4 -0
  78. data/test/_NOTE_ON_TESTING +1 -0
  79. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  80. data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
  81. data/test/functional/associations/test_in_array_proxy.rb +319 -0
  82. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  83. data/test/functional/associations/test_many_documents_proxy.rb +575 -0
  84. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  85. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  86. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  87. data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
  88. data/test/functional/associations/test_one_proxy.rb +182 -0
  89. data/test/functional/test_accessible.rb +168 -0
  90. data/test/functional/test_associations.rb +44 -0
  91. data/test/functional/test_binary.rb +27 -0
  92. data/test/functional/test_caching.rb +76 -0
  93. data/test/functional/test_callbacks.rb +151 -0
  94. data/test/functional/test_dirty.rb +163 -0
  95. data/test/functional/test_document.rb +253 -0
  96. data/test/functional/test_dynamic_querying.rb +75 -0
  97. data/test/functional/test_embedded_document.rb +210 -0
  98. data/test/functional/test_identity_map.rb +506 -0
  99. data/test/functional/test_indexes.rb +42 -0
  100. data/test/functional/test_logger.rb +20 -0
  101. data/test/functional/test_modifiers.rb +416 -0
  102. data/test/functional/test_pagination.rb +91 -0
  103. data/test/functional/test_protected.rb +175 -0
  104. data/test/functional/test_querying.rb +873 -0
  105. data/test/functional/test_safe.rb +76 -0
  106. data/test/functional/test_sci.rb +230 -0
  107. data/test/functional/test_scopes.rb +171 -0
  108. data/test/functional/test_string_id_compatibility.rb +67 -0
  109. data/test/functional/test_timestamps.rb +62 -0
  110. data/test/functional/test_userstamps.rb +27 -0
  111. data/test/functional/test_validations.rb +342 -0
  112. data/test/models.rb +233 -0
  113. data/test/test_active_model_lint.rb +13 -0
  114. data/test/test_helper.rb +104 -0
  115. data/test/unit/associations/test_base.rb +212 -0
  116. data/test/unit/associations/test_proxy.rb +105 -0
  117. data/test/unit/serializers/test_json_serializer.rb +202 -0
  118. data/test/unit/test_clone.rb +69 -0
  119. data/test/unit/test_descendant_appends.rb +71 -0
  120. data/test/unit/test_document.rb +213 -0
  121. data/test/unit/test_dynamic_finder.rb +125 -0
  122. data/test/unit/test_embedded_document.rb +644 -0
  123. data/test/unit/test_extensions.rb +380 -0
  124. data/test/unit/test_key.rb +185 -0
  125. data/test/unit/test_keys.rb +55 -0
  126. data/test/unit/test_mongo_mapper.rb +110 -0
  127. data/test/unit/test_pagination.rb +11 -0
  128. data/test/unit/test_plugins.rb +50 -0
  129. data/test/unit/test_rails.rb +181 -0
  130. data/test/unit/test_rails_compatibility.rb +52 -0
  131. data/test/unit/test_serialization.rb +51 -0
  132. data/test/unit/test_time_zones.rb +39 -0
  133. data/test/unit/test_validations.rb +544 -0
  134. metadata +316 -0
@@ -0,0 +1,28 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Associations
5
+ class ManyDocumentsAsProxy < ManyDocumentsProxy
6
+ protected
7
+ def criteria
8
+ {type_key_name => proxy_owner.class.name, id_key_name => proxy_owner.id}
9
+ end
10
+
11
+ def apply_scope(doc)
12
+ ensure_owner_saved
13
+ criteria.each { |key, value| doc[key] = value }
14
+ doc
15
+ end
16
+
17
+ private
18
+ def type_key_name
19
+ "#{options[:as]}_type"
20
+ end
21
+
22
+ def id_key_name
23
+ "#{options[:as]}_id"
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,110 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Associations
5
+ class ManyDocumentsProxy < Collection
6
+ include DynamicQuerying::ClassMethods
7
+ include Querying::PluckyMethods
8
+
9
+ def replace(docs)
10
+ load_target
11
+ target.map(&:destroy)
12
+ docs.each { |doc| prepare(doc).save }
13
+ reset
14
+ end
15
+
16
+ def <<(*docs)
17
+ ensure_owner_saved
18
+ flatten_deeper(docs).each { |doc| prepare(doc).save }
19
+ reset
20
+ end
21
+ alias_method :push, :<<
22
+ alias_method :concat, :<<
23
+
24
+ def build(attrs={})
25
+ doc = klass.new(attrs)
26
+ apply_scope(doc)
27
+ @target ||= [] unless loaded?
28
+ @target << doc
29
+ doc
30
+ end
31
+
32
+ def create(attrs={})
33
+ doc = klass.new(attrs)
34
+ apply_scope(doc).save
35
+ reset
36
+ doc
37
+ end
38
+
39
+ def create!(attrs={})
40
+ doc = klass.new(attrs)
41
+ apply_scope(doc).save!
42
+ reset
43
+ doc
44
+ end
45
+
46
+ def destroy_all(options={})
47
+ all(options).map(&:destroy)
48
+ reset
49
+ end
50
+
51
+ def delete_all(options={})
52
+ query(options).remove
53
+ reset
54
+ end
55
+
56
+ def nullify
57
+ all.each { |doc| doc.update_attributes(self.foreign_key => nil) }
58
+ reset
59
+ end
60
+
61
+ def save_to_collection(options={})
62
+ @target.each { |doc| doc.save(options) } if @target
63
+ end
64
+
65
+ protected
66
+ def query(options={})
67
+ klass.
68
+ query(association.query_options).
69
+ update(options).update(criteria)
70
+ end
71
+
72
+ def method_missing(method, *args, &block)
73
+ if klass.respond_to?(method)
74
+ result = klass.send(method, *args, &block)
75
+ result.is_a?(Plucky::Query) ?
76
+ query.merge(result) : super
77
+ else
78
+ super
79
+ end
80
+ end
81
+
82
+ def criteria
83
+ {self.foreign_key => proxy_owner.id}
84
+ end
85
+
86
+ def find_target
87
+ all
88
+ end
89
+
90
+ def ensure_owner_saved
91
+ proxy_owner.save if proxy_owner.new?
92
+ end
93
+
94
+ def prepare(doc)
95
+ klass === doc ? apply_scope(doc) : build(doc)
96
+ end
97
+
98
+ def apply_scope(doc)
99
+ ensure_owner_saved
100
+ criteria.each { |key, value| doc[key] = value }
101
+ doc
102
+ end
103
+
104
+ def foreign_key
105
+ options[:foreign_key] || proxy_owner.class.name.to_s.underscore.gsub("/", "_") + "_id"
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Associations
5
+ class ManyEmbeddedPolymorphicProxy < EmbeddedCollection
6
+ def replace(values)
7
+ @_values = values.map do |v|
8
+ v.respond_to?(:attributes) ? v.attributes.merge(association.type_key_name => v.class.name) : v
9
+ end
10
+ reset
11
+ end
12
+
13
+ protected
14
+ def find_target
15
+ (@_values || []).map do |hash|
16
+ child = polymorphic_class(hash).load(hash)
17
+ assign_references(child)
18
+ child
19
+ end
20
+ end
21
+
22
+ def polymorphic_class(doc)
23
+ if class_name = doc[association.type_key_name]
24
+ class_name.constantize
25
+ else
26
+ klass
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Associations
5
+ class ManyEmbeddedProxy < EmbeddedCollection
6
+ def replace(values)
7
+ @_values = values.map do |v|
8
+ v.respond_to?(:attributes) ? v.attributes : v
9
+ end
10
+ reset
11
+ end
12
+
13
+ private
14
+ def find_target
15
+ (@_values || []).map do |attrs|
16
+ klass.load(attrs).tap do |child|
17
+ assign_references(child)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Associations
5
+ class ManyPolymorphicProxy < ManyDocumentsProxy
6
+ private
7
+ def apply_scope(doc)
8
+ doc[association.type_key_name] = doc.class.name
9
+ super
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,40 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Associations
5
+ class OneEmbeddedProxy < Proxy
6
+ def build(attributes={})
7
+ @target = klass.new(attributes)
8
+ assign_references(@target)
9
+ loaded
10
+ @target
11
+ end
12
+
13
+ def replace(doc)
14
+ if doc.respond_to?(:attributes)
15
+ @target = klass.load(doc.attributes)
16
+ else
17
+ @target = klass.load(doc)
18
+ end
19
+ assign_references(@target)
20
+ loaded
21
+ @target
22
+ end
23
+
24
+ protected
25
+
26
+ def find_target
27
+ if @value
28
+ klass.load(@value).tap do |child|
29
+ assign_references(child)
30
+ end
31
+ end
32
+ end
33
+
34
+ def assign_references(doc)
35
+ doc._parent_document = proxy_owner if doc
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Associations
5
+ class OneProxy < Proxy
6
+ def build(attrs={})
7
+ instantiate_target(:new, attrs)
8
+ end
9
+
10
+ def create(attrs={})
11
+ instantiate_target(:create, attrs)
12
+ end
13
+
14
+ def create!(attrs={})
15
+ instantiate_target(:create!, attrs)
16
+ end
17
+
18
+ def replace(doc)
19
+ load_target
20
+
21
+ if !target.nil? && target != doc
22
+ if options[:dependent] && !target.new?
23
+ case options[:dependent]
24
+ when :delete
25
+ target.delete
26
+ when :destroy
27
+ target.destroy
28
+ when :nullify
29
+ target[foreign_key] = nil
30
+ target.save
31
+ end
32
+ end
33
+ end
34
+
35
+ reset
36
+
37
+ unless doc.nil?
38
+ proxy_owner.save if proxy_owner.new?
39
+ doc = klass.new(doc) unless klass === doc
40
+ doc[foreign_key] = proxy_owner.id
41
+ doc.save if doc.new?
42
+ loaded
43
+ @target = doc
44
+ end
45
+ end
46
+
47
+ protected
48
+ def find_target
49
+ target_class.first(association.query_options.merge(foreign_key => proxy_owner.id))
50
+ end
51
+
52
+ def instantiate_target(instantiator, attrs={})
53
+ @target = target_class.send(instantiator, attrs.update(foreign_key => proxy_owner.id))
54
+ loaded
55
+ @target
56
+ end
57
+
58
+ def target_class
59
+ @target_class ||= options[:class] || (options[:class_name] || association.name.to_s.camelize).constantize
60
+ end
61
+
62
+ def foreign_key
63
+ options[:foreign_key] || proxy_owner.class.name.foreign_key
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,126 @@
1
+ # encoding: UTF-8
2
+ require 'forwardable'
3
+ module MongoMapper
4
+ module Plugins
5
+ module Associations
6
+ class Proxy
7
+ extend Forwardable
8
+
9
+ alias :proxy_respond_to? :respond_to?
10
+ alias :proxy_extend :extend
11
+
12
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_|^object_id$)/ }
13
+
14
+ attr_reader :proxy_owner, :association, :target
15
+
16
+ alias :proxy_target :target
17
+ alias :proxy_association :association
18
+
19
+ def_delegators :proxy_association, :klass, :options
20
+ def_delegator :klass, :collection
21
+
22
+ def initialize(owner, association)
23
+ @proxy_owner, @association, @loaded = owner, association, false
24
+ Array(association.options[:extend]).each { |ext| proxy_extend(ext) }
25
+ reset
26
+ end
27
+
28
+ def inspect
29
+ load_target
30
+ target.inspect
31
+ end
32
+
33
+ def loaded?
34
+ @loaded
35
+ end
36
+
37
+ def loaded
38
+ @loaded = true
39
+ end
40
+
41
+ def nil?
42
+ load_target
43
+ target.nil?
44
+ end
45
+
46
+ def blank?
47
+ load_target
48
+ target.blank?
49
+ end
50
+
51
+ def present?
52
+ load_target
53
+ target.present?
54
+ end
55
+
56
+ def reload
57
+ reset
58
+ load_target
59
+ self unless target.nil?
60
+ end
61
+
62
+ def replace(v)
63
+ raise NotImplementedError
64
+ end
65
+
66
+ def reset
67
+ @loaded = false
68
+ @target = nil
69
+ end
70
+
71
+ def respond_to?(*args)
72
+ proxy_respond_to?(*args) || (load_target && target.respond_to?(*args))
73
+ end
74
+
75
+ def send(method, *args)
76
+ if proxy_respond_to?(method)
77
+ super
78
+ else
79
+ load_target
80
+ target.send(method, *args)
81
+ end
82
+ end
83
+
84
+ def ===(other)
85
+ load_target
86
+ other === target
87
+ end
88
+
89
+ protected
90
+ def method_missing(method, *args, &block)
91
+ if load_target
92
+ if block_given?
93
+ target.send(method, *args) { |*block_args| block.call(*block_args) }
94
+ else
95
+ target.send(method, *args)
96
+ end
97
+ end
98
+ end
99
+
100
+ def load_target
101
+ unless loaded?
102
+ if @target.is_a?(Array) && @target.any?
103
+ @target = find_target + @target.find_all { |record| record.new? }
104
+ else
105
+ @target = find_target
106
+ end
107
+ loaded
108
+ end
109
+ @target
110
+ rescue MongoMapper::DocumentNotFound
111
+ reset
112
+ end
113
+
114
+ def find_target
115
+ raise NotImplementedError
116
+ end
117
+
118
+ def flatten_deeper(array)
119
+ array.collect do |element|
120
+ (element.respond_to?(:flatten) && !element.is_a?(Hash)) ? element.flatten : element
121
+ end.flatten
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end