Class: Mongoid::ModelResolver

Herda:
Objeto
  • Objeto
mostrar tudo
Estendido por:
Encaminhável
Definido em:
lib/mongoid/model_resolver.rb

Visão geral

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.

Colapsode resumo constante

INSTANCE_MUTEX =

Essa constante faz parte de uma API privada. Você deve evitar usar essa constante, se possível, pois ela pode ser removida ou alterada no futuro.

The mutex instance used to make the ‘.instance` method thread-safe.

Mutex.Novo

Recolhimento do Resumo do método de classe

Recolhimento do Resumo do método de instância

Detalhes do construtor

#initializeModelResolver

Instantiates a new ModelResolver instance.



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

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

Detalhes do método de classe

.instanceMongoid::ModelResolver

Returns the default instance of the ModelResolver.

Retorna:



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

def instância
  @instance ||= INSTANCE_MUTEX.sincronizar { @instance ||= Novo }
end

.register_resolver(resolver, name) ⇒ Object

Register the given resolver under the given name.

Parâmetros:

  • resolvedor (Mongoid::ModelResolver::Interface)

    the resolver to register.

  • name (string | Símbolo)

    the identifier to use to register the resolver.



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

def registra_resolver(resolvedor, name)
  resolvedores[name.to_sym] = resolvedor
  auto
end

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

Returns the resolver instance that corresponds to the argument.

Parâmetros:

  • 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.

Retorna:

  • (Mongoid::ModelResolver::Interface)

    the resolver instance corresponding to the given argument.

Aumenta:

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



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mongoid/model_resolver.rb', line 52

def resolvedor(identifier_or_object = :default)
  caso identifier_or_object
  quando nada, false então nada
  quando true, :default então instância
  quando String, Símbolo
    resolvedores.buscar(identifier_or_object.to_sym) fazer |chave|
      aumentar Mongoid::Errors::UnrecognizedResolver, chave
    end
  mais identifier_or_object
  end
end

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

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

Retorna:

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

    the hash of resolver instances, mapped by symbol identifier.



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

def resolvedores
  @resolvers ||= {}
end

Detalhes do método de instância

#register(klass, *keys) ⇒ Object

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.

Parâmetros:

  • classe (Mongoid::Document)

    the document class to register

  • *keys (Array<String>)

    the list of keys to use as an alias (optional)



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mongoid/model_resolver.rb', line 90

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

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

  keys.cada fazer |chave|
    @key_to_model[chave] = classe
  end

  auto
end