mongo_mapper-unstable 2010.1.6 → 2010.1.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. data/VERSION +1 -1
  2. data/lib/mongo_mapper/descendant_appends.rb +44 -0
  3. data/lib/mongo_mapper/document.rb +54 -98
  4. data/lib/mongo_mapper/embedded_document.rb +28 -348
  5. data/lib/mongo_mapper/finder_options.rb +15 -33
  6. data/lib/mongo_mapper/plugins/associations/base.rb +121 -0
  7. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +28 -0
  8. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +23 -0
  9. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  10. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +49 -0
  11. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +139 -0
  12. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  13. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +117 -0
  14. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  15. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  16. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  17. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +66 -0
  18. data/lib/mongo_mapper/plugins/associations/proxy.rb +118 -0
  19. data/lib/mongo_mapper/plugins/associations.rb +104 -0
  20. data/lib/mongo_mapper/plugins/callbacks.rb +65 -0
  21. data/lib/mongo_mapper/plugins/clone.rb +13 -0
  22. data/lib/mongo_mapper/plugins/descendants.rb +16 -0
  23. data/lib/mongo_mapper/plugins/dirty.rb +119 -0
  24. data/lib/mongo_mapper/plugins/equality.rb +11 -0
  25. data/lib/mongo_mapper/plugins/identity_map.rb +66 -0
  26. data/lib/mongo_mapper/plugins/inspect.rb +14 -0
  27. data/lib/mongo_mapper/plugins/keys.rb +295 -0
  28. data/lib/mongo_mapper/plugins/logger.rb +17 -0
  29. data/lib/mongo_mapper/plugins/pagination.rb +85 -0
  30. data/lib/mongo_mapper/plugins/rails.rb +45 -0
  31. data/lib/mongo_mapper/plugins/serialization.rb +109 -0
  32. data/lib/mongo_mapper/plugins/validations.rb +48 -0
  33. data/lib/mongo_mapper/plugins.rb +19 -0
  34. data/lib/mongo_mapper/support.rb +36 -15
  35. data/lib/mongo_mapper.rb +23 -22
  36. data/performance/read_write.rb +52 -0
  37. data/specs.watchr +23 -2
  38. data/test/functional/associations/test_belongs_to_proxy.rb +1 -1
  39. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +58 -39
  40. data/test/functional/associations/test_many_embedded_proxy.rb +103 -69
  41. data/test/functional/test_dirty.rb +1 -1
  42. data/test/functional/test_document.rb +25 -25
  43. data/test/functional/test_embedded_document.rb +66 -63
  44. data/test/functional/test_identity_map.rb +233 -0
  45. data/test/functional/test_modifiers.rb +14 -0
  46. data/test/functional/test_string_id_compatibility.rb +4 -4
  47. data/test/functional/test_validations.rb +13 -0
  48. data/test/models.rb +0 -39
  49. data/test/test_helper.rb +8 -2
  50. data/test/unit/associations/test_base.rb +1 -1
  51. data/test/unit/associations/test_proxy.rb +3 -3
  52. data/test/unit/test_descendant_appends.rb +71 -0
  53. data/test/unit/test_document.rb +35 -46
  54. data/test/unit/test_embedded_document.rb +218 -271
  55. data/test/unit/{test_key.rb → test_keys.rb} +0 -0
  56. data/test/unit/test_pagination.rb +10 -2
  57. data/test/unit/test_plugins.rb +42 -0
  58. data/test/unit/test_rails.rb +123 -0
  59. data/test/unit/{test_serializations.rb → test_serialization.rb} +0 -0
  60. data/test/unit/test_support.rb +10 -6
  61. data/test/unit/test_time_zones.rb +2 -2
  62. metadata +44 -31
  63. data/lib/mongo_mapper/associations/base.rb +0 -119
  64. data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +0 -26
  65. data/lib/mongo_mapper/associations/belongs_to_proxy.rb +0 -21
  66. data/lib/mongo_mapper/associations/collection.rb +0 -19
  67. data/lib/mongo_mapper/associations/in_array_proxy.rb +0 -137
  68. data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +0 -26
  69. data/lib/mongo_mapper/associations/many_documents_proxy.rb +0 -115
  70. data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +0 -31
  71. data/lib/mongo_mapper/associations/many_embedded_proxy.rb +0 -54
  72. data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +0 -11
  73. data/lib/mongo_mapper/associations/one_proxy.rb +0 -64
  74. data/lib/mongo_mapper/associations/proxy.rb +0 -116
  75. data/lib/mongo_mapper/associations.rb +0 -78
  76. data/lib/mongo_mapper/callbacks.rb +0 -61
  77. data/lib/mongo_mapper/dirty.rb +0 -117
  78. data/lib/mongo_mapper/key.rb +0 -36
  79. data/lib/mongo_mapper/mongo_mapper.rb +0 -125
  80. data/lib/mongo_mapper/pagination.rb +0 -66
  81. data/lib/mongo_mapper/rails_compatibility/document.rb +0 -15
  82. data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +0 -28
  83. data/lib/mongo_mapper/serialization.rb +0 -54
  84. data/lib/mongo_mapper/serializers/json_serializer.rb +0 -48
  85. data/lib/mongo_mapper/validations.rb +0 -39
  86. data/test/functional/test_rails_compatibility.rb +0 -25
@@ -1,8 +1,16 @@
1
1
  require 'test_helper'
2
2
 
3
- class PaginationTest < Test::Unit::TestCase
3
+ class PaginationTest < Test::Unit::TestCase
4
+ should "default per_page to 25" do
5
+ Doc().per_page.should == 25
6
+ end
7
+
8
+ should "allow overriding per_page" do
9
+ Doc() { def self.per_page; 1 end }.per_page.should == 1
10
+ end
11
+
4
12
  context "Pagination proxy" do
5
- include MongoMapper::Pagination
13
+ include MongoMapper::Plugins::Pagination
6
14
 
7
15
  should "should have accessors for subject" do
8
16
  subject = [1,2,3,4,5]
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ module MyPlugin
4
+ module ClassMethods
5
+ def class_foo
6
+ 'class_foo'
7
+ end
8
+ end
9
+
10
+ module InstanceMethods
11
+ def instance_foo
12
+ 'instance_foo'
13
+ end
14
+ end
15
+ end
16
+
17
+ class PluginsTest < Test::Unit::TestCase
18
+ context "plugin" do
19
+ setup do
20
+ @document = Class.new do
21
+ extend MongoMapper::Plugins
22
+ plugin MyPlugin
23
+ end
24
+ end
25
+
26
+ should "include instance methods" do
27
+ @document.new.instance_foo.should == 'instance_foo'
28
+ end
29
+
30
+ should "extend class methods" do
31
+ @document.class_foo.should == 'class_foo'
32
+ end
33
+
34
+ should "default plugins to empty array" do
35
+ Class.new { extend MongoMapper::Plugins }.plugins.should == []
36
+ end
37
+
38
+ should "add plugin to plugins" do
39
+ @document.plugins.should include(MyPlugin)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,123 @@
1
+ require 'test_helper'
2
+
3
+ class TestRails < Test::Unit::TestCase
4
+ context "Document" do
5
+ setup do
6
+ @klass = Doc('Post') do
7
+ key :foo, String
8
+ end
9
+ end
10
+
11
+ context "Class methods" do
12
+ should "alias has_many to many" do
13
+ @klass.should respond_to(:has_many)
14
+ end
15
+
16
+ should "alias has_one to one" do
17
+ @klass.should respond_to(:has_one)
18
+ end
19
+
20
+ should "have column names" do
21
+ @klass.column_names.sort.should == ['_id', 'foo']
22
+ end
23
+
24
+ should "implement human_name" do
25
+ @klass.human_name.should == 'Post'
26
+ end
27
+ end
28
+
29
+ context "Instance methods" do
30
+ setup do
31
+ @klass.class_eval do
32
+ def bar=(value)
33
+ write_attribute(:foo, value)
34
+ end
35
+
36
+ def bar_before_typecast
37
+ read_attribute_before_typecast(:foo)
38
+ end
39
+
40
+ def bar
41
+ read_attribute(:foo)
42
+ end
43
+ end
44
+ end
45
+
46
+ should "alias new_record? to new?" do
47
+ @klass.new.should be_new_record
48
+ end
49
+
50
+ should "be able to read key with read_attribute" do
51
+ @klass.new(:foo => 'Bar').bar.should == 'Bar'
52
+ end
53
+
54
+ should "be able to read key before typecast with read_attribute_before_typecast" do
55
+ @klass.new(:foo => 21).bar_before_typecast.should == 21
56
+ @klass.new(:foo => 21).bar.should == '21'
57
+ end
58
+
59
+ should "be able to write key with write_attribute" do
60
+ @klass.new(:bar => 'Setting Foo').foo.should == 'Setting Foo'
61
+ end
62
+ end
63
+ end
64
+
65
+ context "EmbeddedDocument" do
66
+ setup do
67
+ @klass = EDoc('Post') { key :foo, String }
68
+ end
69
+
70
+ context "Class methods" do
71
+ should "alias has_many to many" do
72
+ @klass.should respond_to(:has_many)
73
+ end
74
+
75
+ should "alias has_one to one" do
76
+ @klass.should respond_to(:has_one)
77
+ end
78
+
79
+ should "have column names" do
80
+ @klass.column_names.sort.should == ['_id', 'foo']
81
+ end
82
+
83
+ should "implement human_name" do
84
+ @klass.human_name.should == 'Post'
85
+ end
86
+ end
87
+
88
+ context "Instance methods" do
89
+ setup do
90
+ @klass.class_eval do
91
+ def bar=(value)
92
+ write_attribute(:foo, value)
93
+ end
94
+
95
+ def bar_before_typecast
96
+ read_attribute_before_typecast(:foo)
97
+ end
98
+
99
+ def bar
100
+ read_attribute(:foo)
101
+ end
102
+ end
103
+ end
104
+
105
+ should "alias new_record? to new?" do
106
+ @klass.new.should be_new_record
107
+ end
108
+
109
+ should "be able to read key with read_attribute" do
110
+ @klass.new(:foo => 'Bar').bar.should == 'Bar'
111
+ end
112
+
113
+ should "be able to read key before typecast with read_attribute_before_typecast" do
114
+ @klass.new(:foo => 21).bar_before_typecast.should == 21
115
+ @klass.new(:foo => 21).bar.should == '21'
116
+ end
117
+
118
+ should "be able to write key with write_attribute" do
119
+ @klass.new(:bar => 'Setting Foo').foo.should == 'Setting Foo'
120
+ end
121
+ end
122
+ end
123
+ end
@@ -90,6 +90,10 @@ class SupportTest < Test::Unit::TestCase
90
90
  Date.to_mongo(Date.new(2009, 10, 1)).should == Time.utc(2009, 10, 1)
91
91
  end
92
92
 
93
+ should "be date if time" do
94
+ Date.to_mongo(Time.parse("2009-10-1T12:30:00")).should == Time.utc(2009, 10, 1)
95
+ end
96
+
93
97
  should "be nil if bogus string" do
94
98
  Date.to_mongo('jdsafop874').should be_nil
95
99
  end
@@ -264,8 +268,8 @@ class SupportTest < Test::Unit::TestCase
264
268
  end
265
269
 
266
270
  context "Time#to_mongo without Time.zone" do
267
- should "be time if string" do
268
- Time.to_mongo('2000-01-01 01:01:01.123456').should == Time.local(2000, 1, 1, 1, 1, 1, 123456).utc
271
+ should "be time to milliseconds if string" do
272
+ Time.to_mongo('2000-01-01 01:01:01.123456').should == Time.local(2000, 1, 1, 1, 1, 1, 123000).utc
269
273
  end
270
274
 
271
275
  should "be time in utc if time" do
@@ -282,15 +286,15 @@ class SupportTest < Test::Unit::TestCase
282
286
  end
283
287
 
284
288
  context "Time#to_mongo with Time.zone" do
285
- should "be time if time" do
289
+ should "be time to milliseconds if time" do
286
290
  Time.zone = 'Hawaii'
287
- Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0)).should == Time.utc(2009, 8, 16, 0, 0, 0)
291
+ Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0, 123456)).should == Time.utc(2009, 8, 16, 0, 0, 0, 123000)
288
292
  Time.zone = nil
289
293
  end
290
294
 
291
- should "be time if string" do
295
+ should "be time to milliseconds if string" do
292
296
  Time.zone = 'Hawaii'
293
- Time.to_mongo('2009-08-15 14:00:00').should == Time.utc(2009, 8, 16, 0, 0, 0)
297
+ Time.to_mongo('2009-08-15 14:00:00.123456').should == Time.utc(2009, 8, 16, 0, 0, 0, 123000)
294
298
  Time.zone = nil
295
299
  end
296
300
 
@@ -26,10 +26,10 @@ class TimeZonesTest < Test::Unit::TestCase
26
26
  Time.zone = nil
27
27
  end
28
28
 
29
- should_eventually "work with timezones that are not UTC" do
29
+ should "work with timezones that are not UTC" do
30
30
  Time.zone = 'Hawaii'
31
31
 
32
- doc = @document.new(:created_at => @original_time)
32
+ doc = @document.new(:created_at => "2009-08-15 14:00:00")
33
33
  doc.created_at.is_a?(ActiveSupport::TimeWithZone).should be_true
34
34
  doc.created_at.should == Time.utc(2009, 8, 16)
35
35
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper-unstable
3
3
  version: !ruby/object:Gem::Version
4
- version: 2010.1.6
4
+ version: 2010.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-06 00:00:00 -05:00
12
+ date: 2010-01-12 00:00:00 -05:00
13
13
  default_executable: mmconsole
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -99,35 +99,42 @@ files:
99
99
  - VERSION
100
100
  - bin/mmconsole
101
101
  - lib/mongo_mapper.rb
102
- - lib/mongo_mapper/associations.rb
103
- - lib/mongo_mapper/associations/base.rb
104
- - lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb
105
- - lib/mongo_mapper/associations/belongs_to_proxy.rb
106
- - lib/mongo_mapper/associations/collection.rb
107
- - lib/mongo_mapper/associations/in_array_proxy.rb
108
- - lib/mongo_mapper/associations/many_documents_as_proxy.rb
109
- - lib/mongo_mapper/associations/many_documents_proxy.rb
110
- - lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb
111
- - lib/mongo_mapper/associations/many_embedded_proxy.rb
112
- - lib/mongo_mapper/associations/many_polymorphic_proxy.rb
113
- - lib/mongo_mapper/associations/one_proxy.rb
114
- - lib/mongo_mapper/associations/proxy.rb
115
- - lib/mongo_mapper/callbacks.rb
116
- - lib/mongo_mapper/dirty.rb
102
+ - lib/mongo_mapper/descendant_appends.rb
117
103
  - lib/mongo_mapper/document.rb
118
104
  - lib/mongo_mapper/dynamic_finder.rb
119
105
  - lib/mongo_mapper/embedded_document.rb
120
106
  - lib/mongo_mapper/finder_options.rb
121
- - lib/mongo_mapper/key.rb
122
- - lib/mongo_mapper/mongo_mapper.rb
123
- - lib/mongo_mapper/pagination.rb
124
- - lib/mongo_mapper/rails_compatibility/document.rb
125
- - lib/mongo_mapper/rails_compatibility/embedded_document.rb
126
- - lib/mongo_mapper/serialization.rb
127
- - lib/mongo_mapper/serializers/json_serializer.rb
107
+ - lib/mongo_mapper/plugins.rb
108
+ - lib/mongo_mapper/plugins/associations.rb
109
+ - lib/mongo_mapper/plugins/associations/base.rb
110
+ - lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb
111
+ - lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb
112
+ - lib/mongo_mapper/plugins/associations/collection.rb
113
+ - lib/mongo_mapper/plugins/associations/embedded_collection.rb
114
+ - lib/mongo_mapper/plugins/associations/in_array_proxy.rb
115
+ - lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb
116
+ - lib/mongo_mapper/plugins/associations/many_documents_proxy.rb
117
+ - lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb
118
+ - lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb
119
+ - lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb
120
+ - lib/mongo_mapper/plugins/associations/one_proxy.rb
121
+ - lib/mongo_mapper/plugins/associations/proxy.rb
122
+ - lib/mongo_mapper/plugins/callbacks.rb
123
+ - lib/mongo_mapper/plugins/clone.rb
124
+ - lib/mongo_mapper/plugins/descendants.rb
125
+ - lib/mongo_mapper/plugins/dirty.rb
126
+ - lib/mongo_mapper/plugins/equality.rb
127
+ - lib/mongo_mapper/plugins/identity_map.rb
128
+ - lib/mongo_mapper/plugins/inspect.rb
129
+ - lib/mongo_mapper/plugins/keys.rb
130
+ - lib/mongo_mapper/plugins/logger.rb
131
+ - lib/mongo_mapper/plugins/pagination.rb
132
+ - lib/mongo_mapper/plugins/rails.rb
133
+ - lib/mongo_mapper/plugins/serialization.rb
134
+ - lib/mongo_mapper/plugins/validations.rb
128
135
  - lib/mongo_mapper/support.rb
129
- - lib/mongo_mapper/validations.rb
130
136
  - mongo_mapper.gemspec
137
+ - performance/read_write.rb
131
138
  - specs.watchr
132
139
  - test/NOTE_ON_TESTING
133
140
  - test/functional/associations/test_belongs_to_polymorphic_proxy.rb
@@ -145,10 +152,10 @@ files:
145
152
  - test/functional/test_dirty.rb
146
153
  - test/functional/test_document.rb
147
154
  - test/functional/test_embedded_document.rb
155
+ - test/functional/test_identity_map.rb
148
156
  - test/functional/test_logger.rb
149
157
  - test/functional/test_modifiers.rb
150
158
  - test/functional/test_pagination.rb
151
- - test/functional/test_rails_compatibility.rb
152
159
  - test/functional/test_string_id_compatibility.rb
153
160
  - test/functional/test_validations.rb
154
161
  - test/models.rb
@@ -158,15 +165,18 @@ files:
158
165
  - test/unit/associations/test_base.rb
159
166
  - test/unit/associations/test_proxy.rb
160
167
  - test/unit/serializers/test_json_serializer.rb
168
+ - test/unit/test_descendant_appends.rb
161
169
  - test/unit/test_document.rb
162
170
  - test/unit/test_dynamic_finder.rb
163
171
  - test/unit/test_embedded_document.rb
164
172
  - test/unit/test_finder_options.rb
165
- - test/unit/test_key.rb
173
+ - test/unit/test_keys.rb
166
174
  - test/unit/test_mongo_mapper.rb
167
175
  - test/unit/test_pagination.rb
176
+ - test/unit/test_plugins.rb
177
+ - test/unit/test_rails.rb
168
178
  - test/unit/test_rails_compatibility.rb
169
- - test/unit/test_serializations.rb
179
+ - test/unit/test_serialization.rb
170
180
  - test/unit/test_support.rb
171
181
  - test/unit/test_time_zones.rb
172
182
  - test/unit/test_validations.rb
@@ -200,18 +210,21 @@ specification_version: 3
200
210
  summary: Awesome gem for modeling your domain and storing it in mongo
201
211
  test_files:
202
212
  - test/unit/test_embedded_document.rb
203
- - test/unit/test_serializations.rb
204
- - test/unit/test_key.rb
205
213
  - test/unit/test_dynamic_finder.rb
206
214
  - test/unit/serializers/test_json_serializer.rb
207
215
  - test/unit/test_support.rb
208
216
  - test/unit/test_finder_options.rb
217
+ - test/unit/test_keys.rb
209
218
  - test/unit/associations/test_proxy.rb
210
219
  - test/unit/associations/test_base.rb
211
220
  - test/unit/test_time_zones.rb
221
+ - test/unit/test_serialization.rb
222
+ - test/unit/test_rails.rb
212
223
  - test/unit/test_rails_compatibility.rb
213
224
  - test/unit/test_validations.rb
214
225
  - test/unit/test_mongo_mapper.rb
226
+ - test/unit/test_descendant_appends.rb
227
+ - test/unit/test_plugins.rb
215
228
  - test/unit/test_document.rb
216
229
  - test/unit/test_pagination.rb
217
230
  - test/test_helper.rb
@@ -230,10 +243,10 @@ test_files:
230
243
  - test/functional/associations/test_many_polymorphic_proxy.rb
231
244
  - test/functional/associations/test_belongs_to_polymorphic_proxy.rb
232
245
  - test/functional/associations/test_one_proxy.rb
246
+ - test/functional/test_identity_map.rb
233
247
  - test/functional/test_binary.rb
234
248
  - test/functional/test_dirty.rb
235
249
  - test/functional/test_callbacks.rb
236
- - test/functional/test_rails_compatibility.rb
237
250
  - test/functional/test_validations.rb
238
251
  - test/functional/test_logger.rb
239
252
  - test/functional/test_document.rb
@@ -1,119 +0,0 @@
1
- module MongoMapper
2
- module Associations
3
- class Base
4
- attr_reader :type, :name, :options, :finder_options
5
-
6
- # Options that should not be considered MongoDB query options/criteria
7
- AssociationOptions = [:as, :class, :class_name, :dependent, :extend, :foreign_key, :in, :polymorphic]
8
-
9
- def initialize(type, name, options={}, &extension)
10
- @type, @name, @options, @finder_options = type, name, {}, {}
11
- options.symbolize_keys!
12
-
13
- options[:extend] = modulized_extensions(extension, options[:extend])
14
-
15
- options.each_pair do |key, value|
16
- if AssociationOptions.include?(key)
17
- @options[key] = value
18
- else
19
- @finder_options[key] = value
20
- end
21
- end
22
- end
23
-
24
- def class_name
25
- @class_name ||= begin
26
- if cn = options[:class_name]
27
- cn
28
- elsif many?
29
- name.to_s.singularize.camelize
30
- else
31
- name.to_s.camelize
32
- end
33
- end
34
- end
35
-
36
- def klass
37
- @klass ||= options[:class] || class_name.constantize
38
- end
39
-
40
- def many?
41
- @many_type ||= @type == :many
42
- end
43
-
44
- def belongs_to?
45
- @belongs_to_type ||= @type == :belongs_to
46
- end
47
-
48
- def one?
49
- @one_type ||= @type == :one
50
- end
51
-
52
- def polymorphic?
53
- !!@options[:polymorphic]
54
- end
55
-
56
- def as?
57
- !!@options[:as]
58
- end
59
-
60
- def in_array?
61
- !!@options[:in]
62
- end
63
-
64
- def embeddable?
65
- many? && klass.embeddable?
66
- end
67
-
68
- def type_key_name
69
- @type_key_name ||= many? ? '_type' : "#{as}_type"
70
- end
71
-
72
- def as
73
- @options[:as] || self.name
74
- end
75
-
76
- def foreign_key
77
- @options[:foreign_key] || "#{name}_id"
78
- end
79
-
80
- def ivar
81
- @ivar ||= "@_#{name}"
82
- end
83
-
84
- def proxy_class
85
- @proxy_class ||= begin
86
- if many?
87
- if self.klass.embeddable?
88
- polymorphic? ? ManyEmbeddedPolymorphicProxy : ManyEmbeddedProxy
89
- else
90
- if polymorphic?
91
- ManyPolymorphicProxy
92
- elsif as?
93
- ManyDocumentsAsProxy
94
- elsif in_array?
95
- InArrayProxy
96
- else
97
- ManyDocumentsProxy
98
- end
99
- end
100
- elsif one?
101
- OneProxy
102
- else
103
- polymorphic? ? BelongsToPolymorphicProxy : BelongsToProxy
104
- end
105
- end
106
- end
107
-
108
- private
109
-
110
- # @param [Array<Module, Proc>] extensions a collection of Modules or
111
- # Procs that extend the behaviour of this association.
112
- def modulized_extensions(*extensions)
113
- extensions.flatten.compact.map do |extension|
114
- Proc === extension ? Module.new(&extension) : extension
115
- end
116
- end
117
- end
118
- end
119
- end
@@ -1,26 +0,0 @@
1
- module MongoMapper
2
- module Associations
3
- class BelongsToPolymorphicProxy < Proxy
4
- def replace(doc)
5
- if doc
6
- doc.save if doc.new?
7
- id, type = doc.id, doc.class.name
8
- end
9
-
10
- owner[reflection.foreign_key] = id
11
- owner[reflection.type_key_name] = type
12
- reset
13
- end
14
-
15
- protected
16
- def find_target
17
- return nil if association_class.nil? || owner[reflection.foreign_key].nil?
18
- association_class.first(:id => owner[reflection.foreign_key])
19
- end
20
-
21
- def association_class
22
- proxy_owner[reflection.type_key_name] ? proxy_owner[reflection.type_key_name].constantize : nil
23
- end
24
- end
25
- end
26
- end
@@ -1,21 +0,0 @@
1
- module MongoMapper
2
- module Associations
3
- class BelongsToProxy < Proxy
4
- def replace(doc)
5
- if doc
6
- doc.save if doc.new?
7
- id = doc.id
8
- end
9
-
10
- owner[reflection.foreign_key] = id
11
- reset
12
- end
13
-
14
- protected
15
- def find_target
16
- return nil if owner[reflection.foreign_key].nil?
17
- klass.first(:id => owner[reflection.foreign_key])
18
- end
19
- end
20
- end
21
- end
@@ -1,19 +0,0 @@
1
- module MongoMapper
2
- module Associations
3
- class Collection < Proxy
4
- def to_ary
5
- load_target
6
- if target.is_a?(Array)
7
- target.to_ary
8
- else
9
- Array(target)
10
- end
11
- end
12
-
13
- def reset
14
- super
15
- target = []
16
- end
17
- end
18
- end
19
- end