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.

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



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

def for(klass, foreign_key = false)
  if klass.nil?
    Internal::Object
  elsif foreign_key
    Internal::ForeignKeys.const_get(klass.to_s)
  else
    modules = "BSON::|ActiveSupport::"
    match = klass.to_s.match(Regexp.new("^(#{ modules })?(\\w+)$"))
    if match and Internal.const_defined?(match[2])
      Internal.const_get(match[2])
    else
      klass
    end
  end
end