Module: Mongoid::Fields::Mappings

Extended by:
Mappings
Included in:
Mappings
Defined in:
lib/mongoid/fields/mappings.rb

Overview

This module maps classes used in field type definitions to the custom definable field in Mongoid.

Constant Summary collapse

MODULE =
"Mongoid::Fields::Internal"

Instance Method Summary collapse

Instance Method Details

#for(klass, foreign_key = false) ⇒ Class

Get the custom field type for the provided class used in the field definition.

Examples:

Get the mapping for the class.

Mappings.for(BSON::ObjectId)

Parameters:

  • klass (Class)

    The class to get the field type for.

Returns:

  • (Class)

    The class of the custom field.

Since:

  • 2.1.0



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mongoid/fields/mappings.rb', line 23

def for(klass, foreign_key = false)
  return Internal::Object unless klass
  if foreign_key
    return "#{MODULE}::ForeignKeys::#{klass.to_s.demodulize}".constantize
  end
  begin
    modules = "#{ MODULE }::|BSON::|ActiveSupport::"
    if match = klass.to_s.match(Regexp.new("^(#{ modules })?(\\w+)$"))
      "#{MODULE}::#{ match[2] }".constantize
    else
      klass.to_s.constantize
    end
  rescue NameError
    klass
  end
end