mongo_mapper 0.17.0 → 0.18.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74ec9f278a24ea6d1a430b798204b50f2e69e96f5d01d3cd564a4a582b3683c8
4
- data.tar.gz: d803b9283fcef845d9d841d2d93dba96084bdeb55c316b0f9350e304e2fbe809
3
+ metadata.gz: 070e83a811199050ec61377dc5a69a44aa19a737486632dde0f59dc5e86fbeb1
4
+ data.tar.gz: 94ed7b07b627b1b3ff92025e02c89d68558e6c7eeccf9337b27fd269dfb80948
5
5
  SHA512:
6
- metadata.gz: e58932d6d99c84dd711aaac5162fa4ac7e6fd518e187f9c544e2498fb215aa3ab21f6f70e27abfddb312a594c0ec115a217eede15772202a8e6c24b6b51dc051
7
- data.tar.gz: 23f13767289d092fb1aad74fb4dc18d92a573b42fd4bf938b426ea15fcfdc317de252c6100a3687a11e7edce855bfa13a10e3e956e2e2eb182314208182b0351
6
+ metadata.gz: 32066190e451388cf35bd65167f0a8f9112eb097f4dbf0584e86c4ce0a3fb4f01ee4891ee794064d30056817080a61ffb0b2db0989d6cc6f3dfaa2b180f3b117
7
+ data.tar.gz: da85cfaf7ea47b586b5cecd6bd323b98d266cc58e2cc53c6c2975f96f00d13255aa6b7e328e26daa781c90026a4f2a18a6f18699231bb91fa27736ff12125b56
data/README.md CHANGED
@@ -30,7 +30,7 @@ work some more on the project, send an email to Scott Taylor
30
30
 
31
31
  MongoMapper is tested against:
32
32
 
33
- - MRI 2.4 - 3.2
33
+ - MRI 2.4 - 4.0
34
34
  - JRuby (Versions with 3.1 compatibility)
35
35
 
36
36
  Additionally, MongoMapper is tested against:
@@ -38,9 +38,13 @@ Additionally, MongoMapper is tested against:
38
38
  - Rails 5.0 - 5.2
39
39
  - Rails 6.0 - 6.1
40
40
  - Rails 7.0 - 7.2
41
- - Rails 8.0
41
+ - Rails 8.0 - 8.1
42
42
 
43
- Note, if you are using Ruby 3.0+, you'll need Rails 6.0+.
43
+ ### Notes
44
+
45
+ - if you are using Ruby >= 3.0, you'll need Rails >= 6.0.
46
+ - if you are using Ruby >= 3.4 and Rails <= 7.0, you'll need to add the drb gem to your Gemfile
47
+ - if you are using Ruby >= 3.4 and Rails <= 7.1, you'll need to add the mutex_m gem to your Gemfile
44
48
 
45
49
  ## Contributing & Development
46
50
 
@@ -50,6 +50,11 @@ module MongoMapper
50
50
  config[environment.to_s]
51
51
  end
52
52
 
53
+ MONGOMAPPER_WRAPPING_LIBRARY = {
54
+ name: 'MongoMapper',
55
+ version: Version,
56
+ }.freeze
57
+
53
58
  def connect(environment, options={})
54
59
  raise 'Set config before connecting. MongoMapper.config = {...}' if config.blank?
55
60
  env = config_for_environment(environment).dup
@@ -68,6 +73,8 @@ module MongoMapper
68
73
  end
69
74
 
70
75
  options[:read] = options[:read].to_sym if options[:read].is_a? String
76
+ options[:wrapping_libraries] = [MONGOMAPPER_WRAPPING_LIBRARY]
77
+
71
78
  MongoMapper.connection = Mongo::Client.new(addresses_or_uri, options)
72
79
  end
73
80
 
@@ -77,6 +84,19 @@ module MongoMapper
77
84
  connect(environment, options)
78
85
  end
79
86
 
87
+ def setup_with_config_file(path, environment, options={})
88
+ source = ERB.new(File.read(path)).result
89
+
90
+ yaml_major, yaml_minor, _ = Gem::Version.new(YAML::VERSION).segments
91
+ config = if yaml_major >= 4 || (yaml_major == 3 && yaml_minor >= 1)
92
+ YAML.safe_load(source, aliases: true)
93
+ else
94
+ YAML.safe_load(source, [], [], true)
95
+ end
96
+
97
+ MongoMapper.setup(config, environment, options)
98
+ end
99
+
80
100
  def handle_passenger_forking
81
101
  # :nocov:
82
102
  if defined?(PhusionPassenger)
@@ -40,6 +40,7 @@ module MongoMapper
40
40
  include Plugins::IdentityMap
41
41
  include Plugins::CounterCache
42
42
  include Plugins::Shardable
43
+ include Plugins::GlobalID if defined?(::GlobalID)
43
44
 
44
45
  included do
45
46
  extend Plugins
@@ -30,4 +30,10 @@ module MongoMapper
30
30
  super("Declare either attr_protected or attr_accessible for #{name}, but not both.")
31
31
  end
32
32
  end
33
- end
33
+
34
+ class NoRootDocument < Error
35
+ def initialize(name)
36
+ super("Can not save embedded document #{name} without a root document.")
37
+ end
38
+ end
39
+ end
@@ -15,4 +15,7 @@ end
15
15
 
16
16
  class Symbol
17
17
  extend MongoMapper::Extensions::Symbol
18
+
19
+ # Symbol#name was added in Ruby 3.0. Remove when Ruby 2.x support is dropped.
20
+ alias_method :name, :to_s unless method_defined?(:name)
18
21
  end
@@ -3,7 +3,7 @@ module MongoMapper
3
3
  module Plugins
4
4
  module Associations
5
5
  class Base
6
- attr_reader :name, :options, :query_options
6
+ attr_reader :name, :options, :query_options, :foreign_key, :ivar
7
7
 
8
8
  # Options that should not be considered MongoDB query options/criteria
9
9
  AssociationOptions = [:as, :class, :class_name, :dependent, :extend, :foreign_key, :in, :from, :polymorphic, :autosave, :touch, :counter_cache, :ordered]
@@ -13,6 +13,8 @@ module MongoMapper
13
13
  options.symbolize_keys!
14
14
  options[:extend] = modularized_extensions(extension, options[:extend])
15
15
  separate_options_and_conditions
16
+ @foreign_key = (options[:foreign_key] || "#{name}_id").freeze
17
+ @ivar = "@_#{name}".freeze
16
18
  end
17
19
 
18
20
  def class_name
@@ -63,14 +65,6 @@ module MongoMapper
63
65
  @options[:as] || self.name
64
66
  end
65
67
 
66
- def foreign_key
67
- @options[:foreign_key] || "#{name}_id"
68
- end
69
-
70
- def ivar
71
- @ivar ||= "@_#{name}"
72
- end
73
-
74
68
  # :nocov:
75
69
  def proxy_class
76
70
  raise NotImplementedError
@@ -27,12 +27,16 @@ module MongoMapper
27
27
  end
28
28
 
29
29
  def save(options={})
30
+ ensure_root_document
31
+
30
32
  _root_document.try(:save, options).tap do |result|
31
33
  persist(options) if result
32
34
  end
33
35
  end
34
36
 
35
37
  def save!(options={})
38
+ ensure_root_document
39
+
36
40
  valid? || raise(DocumentNotValid.new(self))
37
41
  _root_document.try(:save!, options).tap do |result|
38
42
  persist(options) if result
@@ -48,6 +52,12 @@ module MongoMapper
48
52
  def _root_document
49
53
  _parent_document.try(:_root_document)
50
54
  end
55
+
56
+ private
57
+
58
+ def ensure_root_document
59
+ raise NoRootDocument.new(self.class.name) unless _root_document
60
+ end
51
61
  end
52
62
  end
53
63
  end
@@ -0,0 +1,8 @@
1
+ # encoding: UTF-8
2
+ module MongoMapper
3
+ module Plugins
4
+ module GlobalID
5
+ include ::GlobalID::Identification
6
+ end
7
+ end
8
+ end
@@ -106,6 +106,20 @@ module MongoMapper
106
106
  RESERVED_KEYS.include?(@name)
107
107
  end
108
108
 
109
+ # Names of the accessor methods this key will define. Empty when
110
+ # accessors are :skip or :none. The reader also defines
111
+ # name_before_type_cast.
112
+ def accessor_method_names
113
+ names = []
114
+ if read_accessor?
115
+ names << @name
116
+ names << "#{@name}_before_type_cast"
117
+ end
118
+ names << "#{@name}=" if write_accessor?
119
+ names << "#{@name}?" if predicate_accessor?
120
+ names
121
+ end
122
+
109
123
  def read_accessor?
110
124
  any_accessor? ["read"]
111
125
  end
@@ -35,11 +35,12 @@ module MongoMapper
35
35
  def load_from_database(attrs, with_cast = false)
36
36
  return super if !self.class.static_keys || !attrs.respond_to?(:each)
37
37
 
38
- attrs = attrs.select { |key, _| self.class.key?(key) }
38
+ embedded_keys = self.class.embedded_associations.map(&:name).map(&:to_s)
39
+ attrs = attrs.select { |key, _| self.class.key?(key) || key.in?(embedded_keys) }
39
40
 
40
41
  super(attrs, with_cast)
41
42
  end
42
43
  end
43
44
  end
44
45
  end
45
- end
46
+ end
@@ -58,7 +58,10 @@ module MongoMapper
58
58
  Key.new(*args).tap do |key|
59
59
  keys[key.name] = key
60
60
  keys[key.abbr] = key if key.abbr
61
- create_accessors_for(key) if key.valid_ruby_name? && !key.reserved_name?
61
+ if key.valid_ruby_name? && !key.reserved_name?
62
+ warn_on_internal_method_clash(key)
63
+ create_accessors_for(key)
64
+ end
62
65
  create_key_in_descendants(*args)
63
66
  create_indexes_for(key)
64
67
  create_validations_for(key)
@@ -139,7 +142,54 @@ module MongoMapper
139
142
  if key_accessors_module_defined?
140
143
  const_get 'MongoMapperKeys'
141
144
  else
142
- const_set 'MongoMapperKeys', Module.new
145
+ mod = Module.new
146
+ # Mark accessor modules so a later key (an _id override, or _type
147
+ # copied onto a subclass) is not treated as a clash with itself.
148
+ mod.define_singleton_method(:key_accessor_module?) { true }
149
+ const_set 'MongoMapperKeys', mod
150
+ end
151
+ end
152
+
153
+ # Warn when an accessor this key will define is already a method on
154
+ # this model. Reserved names still raise from Key. __dynamic keys and
155
+ # keys with accessors :skip / :none define nothing, so they do not
156
+ # warn. Methods created by an earlier key (accessor module, ActiveModel
157
+ # attribute methods, or name_before_type_cast on the model class) are
158
+ # not clashes. This will raise in a future release.
159
+ def warn_on_internal_method_clash(key)
160
+ return if key.dynamic?
161
+
162
+ clashes = key.accessor_method_names.select do |method_name|
163
+ clashes_with_existing_method?(method_name)
164
+ end
165
+ return if clashes.empty?
166
+
167
+ Kernel.warn "[DEPRECATION] Key name `#{key.name}` clashes with an internal MongoMapper method (#{clashes.join(', ')}) and will raise in a future release."
168
+ end
169
+
170
+ def clashes_with_existing_method?(method_name)
171
+ name = method_name.to_sym
172
+ if method_defined?(name) || private_method_defined?(name)
173
+ owner = instance_method(name).owner
174
+ return false if owner.respond_to?(:key_accessor_module?) && owner&.key_accessor_module?
175
+ return false if attribute_method_module?(owner)
176
+ return false if name.to_s.end_with?("_before_type_cast") &&
177
+ owner.is_a?(Class) && ancestors.include?(owner)
178
+
179
+ return true
180
+ end
181
+
182
+ false
183
+ end
184
+
185
+ # ActiveModel#define_attribute_methods stores its reader module in this
186
+ # ivar. Calling generated_attribute_methods would create one, so look
187
+ # it up without touching it. The ivar lives on the class that defined
188
+ # the key, which may be an ancestor.
189
+ def attribute_method_module?(owner)
190
+ ancestors.any? do |ancestor|
191
+ ancestor.instance_variable_defined?(:@generated_attribute_methods) &&
192
+ owner.equal?(ancestor.instance_variable_get(:@generated_attribute_methods))
143
193
  end
144
194
  end
145
195
 
@@ -378,12 +428,13 @@ module MongoMapper
378
428
  key_name_sym = key_name.to_sym
379
429
  if @_dynamic_attributes && @_dynamic_attributes.key?(key_name_sym)
380
430
  @_dynamic_attributes[key_name_sym]
381
- elsif key = keys[key_name.to_s]
382
- if key.ivar && instance_variable_defined?(key.ivar)
383
- value = instance_variable_get(key.ivar)
431
+ elsif key = keys[key_name_sym.name]
432
+ ivar = key.ivar
433
+ if ivar && instance_variable_defined?(ivar)
434
+ value = instance_variable_get(ivar)
384
435
  else
385
- if key.ivar
386
- instance_variable_set key.ivar, key.get(nil)
436
+ if ivar
437
+ instance_variable_set ivar, key.get(nil)
387
438
  else
388
439
  @_dynamic_attributes[key_name_sym] = key.get(nil)
389
440
  end
@@ -30,10 +30,6 @@ module MongoMapper
30
30
  self._scopes ||= {}
31
31
  end
32
32
 
33
- def active_scopes
34
- Thread.current["mongo_mapper_#{name}_active_scopes"] ||= []
35
- end
36
-
37
33
  def default_scopes
38
34
  @default_scopes ||= begin
39
35
  superclass.respond_to?(:default_scopes) ?
@@ -42,16 +38,30 @@ module MongoMapper
42
38
  end
43
39
  end
44
40
 
41
+ # @param Hash options
42
+ # @options options [Boolean] :unscoped Specify to return plain query object
45
43
  def query(options = {})
46
- res = super(options)
47
-
48
- all_anonymous_scopes.each do |scope|
49
- unscoped do
50
- res = process_scope(res, scope)
51
- end
44
+ # Dup so :unscoped can be removed without mutating a hash the caller
45
+ # still holds, and so a frozen hash does not raise.
46
+ options = options.dup
47
+
48
+ if options.delete(:unscoped)
49
+ super(options)
50
+ elsif (context = current_context)
51
+ # with_scope / unscoped already captured default and active criteria
52
+ # on this query. amend applies both criteria and query options
53
+ # (limit, sort, ...) the same way Querying#query does. where()
54
+ # would fold those options into the criteria hash.
55
+ context.clone.amend(options)
56
+ elsif applying_default_scopes?
57
+ # A default scope is running and called back into query. Applying
58
+ # default scopes again recurses. Skip them for this call only.
59
+ # The previous implementation cleared the shared @default_scopes
60
+ # array to get the same effect, which other threads could observe.
61
+ super(options)
62
+ else
63
+ apply_default_scopes(super(options))
52
64
  end
53
-
54
- res
55
65
  end
56
66
 
57
67
  def default_scope(*args, &block)
@@ -66,28 +76,52 @@ module MongoMapper
66
76
  default_scopes
67
77
  end
68
78
 
69
- def with_scope(query = {})
70
- active_scopes.push(query)
79
+ def with_scope(scope = {})
80
+ prev, self.current_context = current_context, process_scope(query, scope)
71
81
  yield
72
82
  ensure
73
- active_scopes.pop
83
+ self.current_context = prev
74
84
  end
75
85
 
76
86
  def unscoped
77
- old_default_scopes = default_scopes.dup
78
- old_active_scopes = active_scopes.dup
79
-
80
- @default_scopes = []
81
- active_scopes.clear
82
-
87
+ prev, self.current_context = current_context, query(unscoped: true)
83
88
  yield
84
89
  ensure
85
- @default_scopes = old_default_scopes
86
- active_scopes.concat(old_active_scopes)
90
+ self.current_context = prev
87
91
  end
88
92
 
89
93
  private
90
94
 
95
+ def current_context
96
+ Thread.current["mongo_mapper_#{name}_current_context"]
97
+ end
98
+
99
+ def current_context=(context)
100
+ Thread.current["mongo_mapper_#{name}_current_context"] = context
101
+ end
102
+
103
+ def applying_default_scopes_key
104
+ "mongo_mapper_#{name}_applying_default_scopes"
105
+ end
106
+
107
+ def applying_default_scopes?
108
+ Thread.current[applying_default_scopes_key]
109
+ end
110
+
111
+ def apply_default_scopes(context)
112
+ key = applying_default_scopes_key
113
+ previous = Thread.current[key]
114
+ Thread.current[key] = true
115
+
116
+ default_scopes.flatten.each do |scope|
117
+ context = process_scope(context, scope)
118
+ end
119
+
120
+ context
121
+ ensure
122
+ Thread.current[key] = previous
123
+ end
124
+
91
125
  def process_scope(context, scope, *args)
92
126
  if scope.is_a?(Proc)
93
127
  scope = context.instance_exec(*args, &scope)
@@ -98,10 +132,6 @@ module MongoMapper
98
132
  scope
99
133
  end
100
134
 
101
- def all_anonymous_scopes
102
- [default_scopes + active_scopes].flatten
103
- end
104
-
105
135
  RESTRICTED_CLASS_METHODS = %w(private public protected allocate new name parent superclass)
106
136
  private_constant :RESTRICTED_CLASS_METHODS
107
137
 
@@ -1,14 +1,36 @@
1
1
  # encoding: UTF-8
2
2
  module MongoMapper
3
3
  module Plugins
4
- if defined?(ActiveSupport::DescendantsTracker::DescendantsArray)
5
- DescendantsArray = ::ActiveSupport::DescendantsTracker::DescendantsArray
6
- else
7
- DescendantsArray = ::Array
4
+ # Holds weak references to the classes that include a plugin host
5
+ # (MongoMapper::Document / EmbeddedDocument), so that classes discarded by
6
+ # code reloading (e.g. Rails development mode) can be garbage collected.
7
+ #
8
+ # ActiveSupport::DescendantsTracker::DescendantsArray used to provide this,
9
+ # but it was removed in Rails 7.1 and falling back to a plain Array kept
10
+ # every reloaded model class (and everything it references) alive forever.
11
+ class WeakDescendants
12
+ include Enumerable
13
+
14
+ def initialize
15
+ @map = ObjectSpace::WeakMap.new
16
+ end
17
+
18
+ def <<(klass)
19
+ # Use the class as the value too: older Rubies cannot hold immediates
20
+ # (e.g. true) in a WeakMap.
21
+ @map[klass] = klass
22
+ self
23
+ end
24
+
25
+ def each(&block)
26
+ return to_enum(:each) unless block_given?
27
+ @map.each_value(&block)
28
+ self
29
+ end
8
30
  end
9
31
 
10
32
  def self.direct_descendants_hash
11
- @direct_descendants_hash ||= Hash.new { |h,k| h[k] = DescendantsArray.new }
33
+ @direct_descendants_hash ||= Hash.new { |h,k| h[k] = WeakDescendants.new }
12
34
  end
13
35
 
14
36
  def plugins
@@ -23,16 +45,16 @@ module MongoMapper
23
45
  end
24
46
 
25
47
  def direct_descendants
26
- Plugins.direct_descendants_hash[self]
48
+ Plugins.direct_descendants_hash[self].to_a
27
49
  end
28
50
 
29
51
  def included(base = nil)
30
- direct_descendants << base if base
52
+ Plugins.direct_descendants_hash[self] << base if base
31
53
  super
32
54
  end
33
55
 
34
56
  def inherited(subclass)
35
- direct_descendants << subclass
57
+ Plugins.direct_descendants_hash[self] << subclass
36
58
  super
37
59
  end
38
60
  end
@@ -57,8 +57,7 @@ module MongoMapper
57
57
  initializer "mongo_mapper.initialize_database" do |app|
58
58
  config_file = Rails.root.join('config/mongo.yml')
59
59
  if config_file.file?
60
- config = YAML.load(ERB.new(config_file.read).result)
61
- MongoMapper.setup(config, Rails.env, :logger => Rails.logger)
60
+ MongoMapper.setup_with_config_file(config_file, Rails.env, :logger => Rails.logger)
62
61
  end
63
62
  end
64
63
  end
@@ -1,4 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module MongoMapper
3
- Version = '0.17.0'
3
+ Version = '0.18.1'
4
4
  end
data/lib/mongo_mapper.rb CHANGED
@@ -6,6 +6,10 @@ require 'active_model'
6
6
  require 'activemodel-serializers-xml'
7
7
  require 'mongo_mapper/deprecator'
8
8
  require "mongo_mapper/railtie" if defined?(Rails)
9
+ begin
10
+ require 'globalid'
11
+ rescue LoadError
12
+ end
9
13
 
10
14
  I18n.load_path << File.expand_path('../mongo_mapper/locale/en.yml', __FILE__)
11
15
 
@@ -19,6 +23,7 @@ module MongoMapper
19
23
  autoload :AccessibleOrProtected, 'mongo_mapper/exceptions'
20
24
  autoload :InvalidKey, 'mongo_mapper/exceptions'
21
25
  autoload :NotSupported, 'mongo_mapper/exceptions'
26
+ autoload :NoRootDocument, 'mongo_mapper/exceptions'
22
27
 
23
28
  autoload :Document, 'mongo_mapper/document'
24
29
  autoload :EmbeddedDocument, 'mongo_mapper/embedded_document'
@@ -70,6 +75,7 @@ module MongoMapper
70
75
  autoload :Userstamps, 'mongo_mapper/plugins/userstamps'
71
76
  autoload :Validations, 'mongo_mapper/plugins/validations'
72
77
  autoload :Touch, 'mongo_mapper/plugins/touch'
78
+ autoload :GlobalID, 'mongo_mapper/plugins/global_id'
73
79
 
74
80
  module Associations
75
81
  autoload :Base, 'mongo_mapper/plugins/associations/base'
@@ -0,0 +1,14 @@
1
+ default: &default
2
+ host: 127.0.0.1:21017
3
+
4
+ production:
5
+ <<: *default
6
+ database: production
7
+
8
+ development:
9
+ <<: *default
10
+ database: development
11
+
12
+ test:
13
+ <<: *default
14
+ database: test
@@ -0,0 +1,10 @@
1
+ production:
2
+ uri: <%= ENV["MONGODB_URI"] %>
3
+
4
+ development:
5
+ host: 127.0.0.1:21017
6
+ database: development
7
+
8
+ test:
9
+ host: 127.0.0.1:21017
10
+ database: test
@@ -0,0 +1,11 @@
1
+ production:
2
+ host: 127.0.0.1:21017
3
+ database: production
4
+
5
+ development:
6
+ host: 127.0.0.1:21017
7
+ database: development
8
+
9
+ test:
10
+ host: 127.0.0.1:21017
11
+ database: test