123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::ModelResolver

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Inherits: Object
Defined in: lib/mongoid/model_resolver.rb

Overview

The default class for resolving model classes based on discriminant keys. Given a key, it will return the corresponding model class, if any. By default, it looks for classes with names that match the given keys, but additional mappings may be provided.

It is also possible to instantiate multiple resolvers—and even implement your own—so that different sets of classes can use independent resolution mechanics.

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

.newModelResolver

Instantiates a new ModelResolver instance.

[ GitHub ]

  
# File 'lib/mongoid/model_resolver.rb', line 75

def initialize
  @key_to_model = {}
  @model_to_keys = {}
end

Class Method Details

.instanceModelResolver

Returns the default instance of the ModelResolver.

Returns:

  • (ModelResolver)

    the default ModelResolver instance.

[ GitHub ]

  
# File 'lib/mongoid/model_resolver.rb', line 27

def instance
  @instance ||= INSTANCE_MUTEX.synchronize { @instance ||= new }
end

.register_resolver(resolver, name)

Register the given resolver under the given name.

Parameters:

  • resolver (Mongoid::ModelResolver::Interface)

    the resolver to register.

  • name (String | Symbol)

    the identifier to use to register the resolver.

[ GitHub ]

  
# File 'lib/mongoid/model_resolver.rb', line 68

def register_resolver(resolver, name)
  resolvers[name.to_sym] = resolver
  self
end

.resolver(identifier_or_object = :default) ⇒ Mongoid::ModelResolver::Interface

Returns the resolver instance that corresponds to the argument.

Parameters:

  • identifier_or_object (nil | true | false Symbol | String | Mongoid::ModelResolver::Interface) (defaults to: :default)

    When nil or false, returns nil. When true or :default, corresponds to the default resolver. When any other symbol or string, corresponds to the registered resolver with that identifier. Otherwise, it must be a resolver instance itself.

Returns:

  • (Mongoid::ModelResolver::Interface)

    the resolver instance corresponding to the given argument.

Raises:

  • Mongoid::Errors::UnrecognizedResolver if the given identifier is a symbol or string and it does not match any registered resolver.

[ GitHub ]

  
# File 'lib/mongoid/model_resolver.rb', line 52

def resolver(identifier_or_object = :default)
  case identifier_or_object
  when nil, false then nil
  when true, :default then instance
  when String, Symbol
    resolvers.fetch(identifier_or_object.to_sym) do |key|
      raise Mongoid::Errors::UnrecognizedResolver, key
    end
  else identifier_or_object
  end
end

.resolversHash<Symbol => Mongoid::ModelResolver::Interface>

Returns the map of registered resolvers. The default resolver is not included here.

Returns:

  • (Hash<Symbol => Mongoid::ModelResolver::Interface>)

    the hash of resolver instances, mapped by symbol identifier.

[ GitHub ]

  
# File 'lib/mongoid/model_resolver.rb', line 36

def resolvers
  @resolvers ||= {}
end

Instance Method Details

#register(klass, *keys)

Registers the given model class with the given keys. In addition to the given keys, the class name itself will be included as a key to identify the class. Keys are given in priority order, with highest priority keys first and lowest last. The class name, if not given explicitly, is always given lowest priority.

If called more than once, newer keys have higher priority than older keys. All duplicate keys will be removed.

Parameters:

[ GitHub ]

  
# File 'lib/mongoid/model_resolver.rb', line 90

def register(klass, *keys)
  default_key = klass.name

  @model_to_keys[klass] = [ *keys, *@model_to_keys[klass], default_key ].uniq
  @key_to_model[default_key] = klass

  keys.each do |key|
    @key_to_model[key] = klass
  end

  self
end