mongo_mapper 0.15.3 → 0.15.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +17 -6
  3. data/lib/mongo_mapper.rb +17 -18
  4. data/lib/mongo_mapper/extensions/array.rb +1 -1
  5. data/lib/mongo_mapper/extensions/binary.rb +1 -1
  6. data/lib/mongo_mapper/extensions/date.rb +1 -1
  7. data/lib/mongo_mapper/extensions/float.rb +1 -1
  8. data/lib/mongo_mapper/extensions/hash.rb +1 -1
  9. data/lib/mongo_mapper/extensions/nil_class.rb +2 -2
  10. data/lib/mongo_mapper/extensions/object.rb +1 -1
  11. data/lib/mongo_mapper/extensions/object_id.rb +1 -1
  12. data/lib/mongo_mapper/extensions/set.rb +1 -1
  13. data/lib/mongo_mapper/extensions/string.rb +1 -1
  14. data/lib/mongo_mapper/plugins/associations/many_association.rb +2 -3
  15. data/lib/mongo_mapper/plugins/associations/{belongs_to_polymorphic_proxy.rb → proxy/belongs_to_polymorphic_proxy.rb} +0 -0
  16. data/lib/mongo_mapper/plugins/associations/{belongs_to_proxy.rb → proxy/belongs_to_proxy.rb} +0 -0
  17. data/lib/mongo_mapper/plugins/associations/proxy/collection.rb +55 -0
  18. data/lib/mongo_mapper/plugins/associations/{embedded_collection.rb → proxy/embedded_collection.rb} +0 -0
  19. data/lib/mongo_mapper/plugins/associations/{in_array_proxy.rb → proxy/in_array_proxy.rb} +0 -0
  20. data/lib/mongo_mapper/plugins/associations/{in_foreign_array_proxy.rb → proxy/in_foreign_array_proxy.rb} +0 -0
  21. data/lib/mongo_mapper/plugins/associations/{many_documents_as_proxy.rb → proxy/many_documents_as_proxy.rb} +0 -0
  22. data/lib/mongo_mapper/plugins/associations/{many_documents_proxy.rb → proxy/many_documents_proxy.rb} +0 -4
  23. data/lib/mongo_mapper/plugins/associations/{many_embedded_polymorphic_proxy.rb → proxy/many_embedded_polymorphic_proxy.rb} +0 -0
  24. data/lib/mongo_mapper/plugins/associations/{many_embedded_proxy.rb → proxy/many_embedded_proxy.rb} +0 -0
  25. data/lib/mongo_mapper/plugins/associations/{many_polymorphic_proxy.rb → proxy/many_polymorphic_proxy.rb} +0 -0
  26. data/lib/mongo_mapper/plugins/associations/{one_as_proxy.rb → proxy/one_as_proxy.rb} +0 -0
  27. data/lib/mongo_mapper/plugins/associations/{one_embedded_polymorphic_proxy.rb → proxy/one_embedded_polymorphic_proxy.rb} +0 -0
  28. data/lib/mongo_mapper/plugins/associations/{one_embedded_proxy.rb → proxy/one_embedded_proxy.rb} +0 -0
  29. data/lib/mongo_mapper/plugins/associations/{one_proxy.rb → proxy/one_proxy.rb} +0 -0
  30. data/lib/mongo_mapper/plugins/associations/{proxy.rb → proxy/proxy.rb} +63 -48
  31. data/lib/mongo_mapper/plugins/associations/single_association.rb +2 -11
  32. data/lib/mongo_mapper/plugins/dirty.rb +1 -1
  33. data/lib/mongo_mapper/plugins/keys/key.rb +9 -6
  34. data/lib/mongo_mapper/plugins/validations.rb +1 -1
  35. data/lib/mongo_mapper/version.rb +1 -1
  36. data/spec/examples.txt +1706 -1704
  37. data/spec/functional/keys_spec.rb +13 -0
  38. data/spec/unit/associations/proxy_spec.rb +5 -5
  39. data/spec/unit/extensions_spec.rb +9 -3
  40. data/spec/unit/inspect_spec.rb +1 -1
  41. data/spec/unit/key_spec.rb +7 -1
  42. metadata +33 -19
  43. data/lib/mongo_mapper/plugins/associations/collection.rb +0 -29
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d5192b00f4bce2f16b7ac329a9bdb059c888ebea3c62ba91e9017a42a43371c
4
- data.tar.gz: 9e3f0dca756cb0e260266fa147800c07d02133dfad74376f3aaa169f56d67f36
3
+ metadata.gz: 290286ec91fda33bce766c8cacff110abd4027b200fa5279c2ca8afdf68d3ad3
4
+ data.tar.gz: b38548fb44300f730c70e0e43abe48a7a9785f884d8b28d3d7447afc78a77ec5
5
5
  SHA512:
6
- metadata.gz: 052506c11e7921c9412fdbbdaeedbc05a199700867502e87c73bb00d4bb649007ed4f5c8f3f57020f96084d26128aaf5ba9973504018a76a1ecf4cd93b014186
7
- data.tar.gz: fb775b4103f3eaa22ae76c5e4875d6c3117e44db8c2e6cd39189800d0fcafb63a8d8368a91a195ea8380cf9ce0744431e41e303bec0c478d61c5606d4ca8bd14
6
+ metadata.gz: eef0f965e3d2b23cafcbfe6d3c39581ef4556e2b606a1668e3c26a2817bd4a75a94635c5102795171f4deeef6a0b52c74ff3bcac6f81c83a69e69d746f369a0b
7
+ data.tar.gz: '058f0007e68c8e8d3bbe5ea9236615ba252a44b12c5e91042390a8f920a4127cbe2950e8748f088daab02e8a7b8c6eb0a10b226c378cf4330951b1f8e94db898'
data/README.md CHANGED
@@ -6,7 +6,7 @@ A Ruby Object Mapper for Mongo.
6
6
 
7
7
  [<img src="https://github.com/mongomapper/mongomapper/workflows/Ruby/badge.svg?branch=master" alt="Build Status" />](https://github.com/mongomapper/mongomapper/actions?query=workflow%3ARuby+branch%3Amaster)
8
8
 
9
- [<img src="https://coveralls.io/repos/mongomapper/mongomapper/badge.svg" alt="Coverage Status" />](https://coveralls.io/r/mongomapper/mongomapper)
9
+ <!-- [<img src="https://coveralls.io/repos/mongomapper/mongomapper/badge.svg" alt="Coverage Status" />](https://coveralls.io/r/mongomapper/mongomapper) -->
10
10
 
11
11
  ## Install
12
12
 
@@ -18,16 +18,27 @@ http://mongomapper.com/documentation/
18
18
 
19
19
  http://rdoc.info/github/mongomapper/mongomapper
20
20
 
21
+ ## Open Commit Policy
22
+
23
+ Like Rubinius, we're trying out an "open commit policy".
24
+
25
+ If you've committed one (code) patch that has been accepted and would like to
26
+ work some more on the project, send an email to Scott Taylor
27
+ <scott@railsnewbie.com> along with your commit sha1.
28
+
21
29
  ## Compatibility
22
30
 
23
31
  MongoMapper is tested against:
24
32
 
25
- * MRI 2.4 - 2.7
33
+ * MRI 2.4 - 3.0.1
26
34
  * JRuby (Versions with 1.9 compatibility)
27
35
 
28
36
  Additionally, MongoMapper is tested against:
29
37
 
30
- * Rails 5.0+ - 6.0
38
+ * Rails 5.0 - 5.2
39
+ * Rails 6.0 - 6.1
40
+
41
+ Note, if you are using Ruby 3.0+, you'll need Rails 6.
31
42
 
32
43
  ## Contributing & Development
33
44
 
@@ -37,8 +48,8 @@ Additionally, MongoMapper is tested against:
37
48
 
38
49
  * Fork the project.
39
50
  * Make your feature addition or bug fix. All specs should pass.
40
- * Add specs for your changes. This is important so I don't break it in a future version unintentionally.
41
- * Commit, do not mess with Rakefile, version, or history. If you want to have your own version, that is fine but bump version in a commit by itself in another branch so a maintainer ignore it when your pull request is merged.
51
+ * Add specs for your changes. This is important so that it doesn't break in a future version.
52
+ * Commit, do not mess with Rakefile, version, or history. If you want to have your own version, that is fine but bump version in a commit by itself in another branch so a maintainer can ignore it when your pull request is merged.
42
53
  * Send a pull request. Bonus points for topic branches.
43
54
 
44
55
  ## Problems or Questions?
@@ -47,7 +58,7 @@ Hit up the Google group: http://groups.google.com/group/mongomapper
47
58
 
48
59
  ## Copyright
49
60
 
50
- Copyright (c) 2009-2020 MongoMapper. See LICENSE for details.
61
+ Copyright (c) 2009-2021 MongoMapper. See LICENSE for details.
51
62
 
52
63
  ## Contributors
53
64
 
data/lib/mongo_mapper.rb CHANGED
@@ -70,25 +70,27 @@ module MongoMapper
70
70
 
71
71
  module Associations
72
72
  autoload :Base, 'mongo_mapper/plugins/associations/base'
73
- autoload :Collection, 'mongo_mapper/plugins/associations/collection'
74
- autoload :EmbeddedCollection, 'mongo_mapper/plugins/associations/embedded_collection'
75
73
  autoload :ManyAssociation, 'mongo_mapper/plugins/associations/many_association'
76
74
  autoload :SingleAssociation, 'mongo_mapper/plugins/associations/single_association'
77
75
  autoload :BelongsToAssociation, 'mongo_mapper/plugins/associations/belongs_to_association'
78
76
  autoload :OneAssociation, 'mongo_mapper/plugins/associations/one_association'
79
- autoload :ManyDocumentsProxy, 'mongo_mapper/plugins/associations/many_documents_proxy'
80
- autoload :BelongsToProxy, 'mongo_mapper/plugins/associations/belongs_to_proxy'
81
- autoload :BelongsToPolymorphicProxy, 'mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy'
82
- autoload :ManyPolymorphicProxy, 'mongo_mapper/plugins/associations/many_polymorphic_proxy'
83
- autoload :ManyEmbeddedProxy, 'mongo_mapper/plugins/associations/many_embedded_proxy'
84
- autoload :ManyEmbeddedPolymorphicProxy, 'mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy'
85
- autoload :ManyDocumentsAsProxy, 'mongo_mapper/plugins/associations/many_documents_as_proxy'
86
- autoload :OneProxy, 'mongo_mapper/plugins/associations/one_proxy'
87
- autoload :OneAsProxy, 'mongo_mapper/plugins/associations/one_as_proxy'
88
- autoload :OneEmbeddedProxy, 'mongo_mapper/plugins/associations/one_embedded_proxy'
89
- autoload :OneEmbeddedPolymorphicProxy, 'mongo_mapper/plugins/associations/one_embedded_polymorphic_proxy'
90
- autoload :InArrayProxy, 'mongo_mapper/plugins/associations/in_array_proxy'
91
- autoload :InForeignArrayProxy, 'mongo_mapper/plugins/associations/in_foreign_array_proxy'
77
+
78
+ autoload :Proxy, 'mongo_mapper/plugins/associations/proxy/proxy'
79
+ autoload :Collection, 'mongo_mapper/plugins/associations/proxy/collection'
80
+ autoload :EmbeddedCollection, 'mongo_mapper/plugins/associations/proxy/embedded_collection'
81
+ autoload :ManyDocumentsProxy, 'mongo_mapper/plugins/associations/proxy/many_documents_proxy'
82
+ autoload :BelongsToProxy, 'mongo_mapper/plugins/associations/proxy/belongs_to_proxy'
83
+ autoload :BelongsToPolymorphicProxy, 'mongo_mapper/plugins/associations/proxy/belongs_to_polymorphic_proxy'
84
+ autoload :ManyPolymorphicProxy, 'mongo_mapper/plugins/associations/proxy/many_polymorphic_proxy'
85
+ autoload :ManyEmbeddedProxy, 'mongo_mapper/plugins/associations/proxy/many_embedded_proxy'
86
+ autoload :ManyEmbeddedPolymorphicProxy, 'mongo_mapper/plugins/associations/proxy/many_embedded_polymorphic_proxy'
87
+ autoload :ManyDocumentsAsProxy, 'mongo_mapper/plugins/associations/proxy/many_documents_as_proxy'
88
+ autoload :OneProxy, 'mongo_mapper/plugins/associations/proxy/one_proxy'
89
+ autoload :OneAsProxy, 'mongo_mapper/plugins/associations/proxy/one_as_proxy'
90
+ autoload :OneEmbeddedProxy, 'mongo_mapper/plugins/associations/proxy/one_embedded_proxy'
91
+ autoload :OneEmbeddedPolymorphicProxy, 'mongo_mapper/plugins/associations/proxy/one_embedded_polymorphic_proxy'
92
+ autoload :InArrayProxy, 'mongo_mapper/plugins/associations/proxy/in_array_proxy'
93
+ autoload :InForeignArrayProxy, 'mongo_mapper/plugins/associations/proxy/in_foreign_array_proxy'
92
94
  end
93
95
  end
94
96
 
@@ -99,7 +101,4 @@ Dir[File.join(File.dirname(__FILE__), 'mongo_mapper', 'extensions', '*.rb')].eac
99
101
  require extension
100
102
  end
101
103
 
102
- # FIXME: autoload with proxy is failing, need to investigate
103
- require 'mongo_mapper/plugins/associations/proxy'
104
-
105
104
  ActiveSupport.run_load_hooks(:mongo_mapper, MongoMapper)
@@ -24,4 +24,4 @@ end
24
24
 
25
25
  class Array
26
26
  include MongoMapper::Extensions::Array
27
- end
27
+ end
@@ -19,4 +19,4 @@ end
19
19
 
20
20
  class Binary
21
21
  extend MongoMapper::Extensions::Binary
22
- end
22
+ end
@@ -22,4 +22,4 @@ end
22
22
 
23
23
  class Date
24
24
  extend MongoMapper::Extensions::Date
25
- end
25
+ end
@@ -11,4 +11,4 @@ end
11
11
 
12
12
  class Float
13
13
  extend MongoMapper::Extensions::Float
14
- end
14
+ end
@@ -23,4 +23,4 @@ end
23
23
 
24
24
  class Hash
25
25
  include MongoMapper::Extensions::Hash
26
- end
26
+ end
@@ -14,5 +14,5 @@ module MongoMapper
14
14
  end
15
15
 
16
16
  class NilClass
17
- include MongoMapper::Extensions::NilClass
18
- end
17
+ extend MongoMapper::Extensions::NilClass
18
+ end
@@ -27,4 +27,4 @@ end
27
27
 
28
28
  class Object
29
29
  include MongoMapper::Extensions::Object
30
- end
30
+ end
@@ -33,4 +33,4 @@ class BSON::ObjectId
33
33
  def original_to_json(*args)
34
34
  original_as_json.to_json(*args)
35
35
  end
36
- end
36
+ end
@@ -17,4 +17,4 @@ end
17
17
 
18
18
  class Set
19
19
  extend MongoMapper::Extensions::Set
20
- end
20
+ end
@@ -23,4 +23,4 @@ end
23
23
 
24
24
  class String
25
25
  include MongoMapper::Extensions::String
26
- end
26
+ end
@@ -30,12 +30,11 @@ module MongoMapper
30
30
  def setup(model)
31
31
  model.associations_module.module_eval(<<-end_eval, __FILE__, __LINE__ + 1)
32
32
  def #{name}
33
- get_proxy(associations[#{name.inspect}])
33
+ get_proxy(associations[#{name.inspect}]).read
34
34
  end
35
35
 
36
36
  def #{name}=(value)
37
- get_proxy(associations[#{name.inspect}]).replace(value)
38
- value
37
+ get_proxy(associations[#{name.inspect}]).write(value)
39
38
  end
40
39
  end_eval
41
40
 
@@ -0,0 +1,55 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module Associations
5
+ class Collection < Proxy
6
+ include Enumerable
7
+
8
+ def to_a
9
+ load_target
10
+
11
+ target.is_a?(Array) ?
12
+ target :
13
+ Array(target)
14
+ end
15
+
16
+ alias_method :to_ary, :to_a
17
+
18
+ def each(&block)
19
+ to_a.each(&block)
20
+ end
21
+
22
+ def [](val)
23
+ objs = to_a
24
+ objs ? objs[val] : nil
25
+ end
26
+
27
+ def empty?
28
+ to_a.empty?
29
+ end
30
+
31
+ def size
32
+ to_a.size
33
+ end
34
+
35
+ def length
36
+ to_a.length
37
+ end
38
+
39
+ def reset
40
+ super
41
+ target = []
42
+ end
43
+
44
+ def read
45
+ self
46
+ end
47
+
48
+ def write(value)
49
+ replace(value)
50
+ read
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -76,10 +76,6 @@ module MongoMapper
76
76
  @target.each { |doc| doc.save(options) } if @target
77
77
  end
78
78
 
79
- def each(&block)
80
- load_target.each(&block)
81
- end
82
-
83
79
  protected
84
80
 
85
81
  def query(options={})
@@ -1,19 +1,25 @@
1
1
  # encoding: UTF-8
2
2
  require 'forwardable'
3
+
3
4
  module MongoMapper
4
5
  module Plugins
5
6
  module Associations
6
7
  class Proxy
7
8
  extend Forwardable
8
9
 
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_|^respond_to_missing\?$|^object_id$)/ }
10
+ class << self
11
+ def define_proxy_method(method)
12
+ define_method(method) do |*args, &block|
13
+ proxy_method(method, *args, &block)
14
+ end
15
+ end
16
+ end
13
17
 
14
18
  attr_reader :proxy_owner, :association, :target
15
19
 
16
- alias :proxy_association :association
20
+ alias_method :proxy_respond_to?, :respond_to?
21
+ alias_method :proxy_extend, :extend
22
+ alias_method :proxy_association, :association
17
23
 
18
24
  def_delegators :proxy_association, :klass, :options
19
25
  def_delegator :klass, :collection
@@ -24,26 +30,30 @@ module MongoMapper
24
30
  reset
25
31
  end
26
32
 
27
- # Active support in rails 3 beta 4 can override to_json after this is loaded,
28
- # at least when run in mongomapper tests. The implementation was changed in master
29
- # some time after this, so not sure whether this is still a problem.
30
- #
31
- # In rails 2, this isn't a problem however it also solves an issue where
32
- # to_json isn't forwarded because it supports to_json itself
33
- def to_json(*options)
34
- load_target
35
- target.to_json(*options)
36
- end
37
-
38
- # see comments to to_json
39
- def as_json(*options)
40
- load_target
41
- target.as_json(*options)
33
+ [
34
+ :is_a?,
35
+ :to_mongo,
36
+ :==,
37
+ :!=,
38
+ :nil?,
39
+ :blank?,
40
+ :present?,
41
+ # Active support in rails 3 beta 4 can override to_json after this is loaded,
42
+ # at least when run in mongomapper tests. The implementation was changed in master
43
+ # some time after this, so not sure whether this is still a problem.
44
+ #
45
+ # In rails 2, this isn't a problem however it also solves an issue where
46
+ # to_json isn't forwarded because it supports to_json itself
47
+ :to_json,
48
+ # see comments to to_json
49
+ :as_json,
50
+ ].each do |m|
51
+ define_proxy_method(m)
42
52
  end
43
53
 
44
54
  def inspect
45
55
  load_target
46
- target.inspect
56
+ "#<#{self.class.inspect}:#{object_id} #{@target.inspect}>"
47
57
  end
48
58
 
49
59
  def loaded?
@@ -54,21 +64,6 @@ module MongoMapper
54
64
  @loaded = true
55
65
  end
56
66
 
57
- def nil?
58
- load_target
59
- target.nil?
60
- end
61
-
62
- def blank?
63
- load_target
64
- target.blank?
65
- end
66
-
67
- def present?
68
- load_target
69
- target.present?
70
- end
71
-
72
67
  def reload
73
68
  reset
74
69
  load_target
@@ -87,16 +82,7 @@ module MongoMapper
87
82
  end
88
83
 
89
84
  def respond_to?(*args)
90
- proxy_respond_to?(*args) || (load_target && target.respond_to?(*args))
91
- end
92
-
93
- def send(method, *args, &block)
94
- if proxy_respond_to?(method, true)
95
- super
96
- else
97
- load_target
98
- target.send(method, *args, &block)
99
- end
85
+ super || (load_target && target.respond_to?(*args))
100
86
  end
101
87
 
102
88
  def read
@@ -104,6 +90,16 @@ module MongoMapper
104
90
  @target
105
91
  end
106
92
 
93
+ def write(value)
94
+ replace(value)
95
+ read
96
+ end
97
+
98
+ def proxy_method(method, *args, &block)
99
+ load_target
100
+ target.public_send(method, *args, &block)
101
+ end
102
+
107
103
  protected
108
104
 
109
105
  def load_target
@@ -138,9 +134,28 @@ module MongoMapper
138
134
  false
139
135
  end
140
136
 
137
+ def define_proxy_method(method)
138
+ metaclass = class << self; self; end
139
+ metaclass.class_eval do
140
+ define_proxy_method(method)
141
+ end
142
+ end
143
+
144
+ def define_and_call_proxy_method(method, *args, &block)
145
+ define_proxy_method(method)
146
+ public_send(method, *args, &block)
147
+ end
148
+
141
149
  def method_missing(method, *args, &block)
142
- if load_target
143
- target.public_send(method, *args, &block)
150
+ # load the target just in case it isn't loaded
151
+ load_target
152
+
153
+ # only define the method if the target has the method
154
+ # NOTE: include private methods!
155
+ if target.respond_to?(method, true)
156
+ define_and_call_proxy_method(method, *args, &block)
157
+ else
158
+ super
144
159
  end
145
160
  end
146
161
  end