Module: Virtus::TypeLookup

Included in:
Attribute
Defined in:
lib/virtus/support/type_lookup.rb

Overview

A module that adds type lookup to a class

Constant Summary collapse

TYPE_FORMAT =
/\A[A-Z]\w*\z/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(model) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set cache ivar on the model

Parameters:

  • model (Class)

Returns:

  • (undefined)


15
16
17
# File 'lib/virtus/support/type_lookup.rb', line 15

def self.extended(model)
  model.instance_variable_set('@type_lookup_cache', {})
end

Instance Method Details

#determine_type(class_or_name) ⇒ Class?

Returns a descendant based on a name or class

Examples:

MyClass.determine_type('String')  # => MyClass::String

Parameters:

  • class_or_name (Class, #to_s)

    name of a class or a class itself

Returns:

  • (Class)

    a descendant

  • (nil)

    nil if the type cannot be determined by the class_or_name



34
35
36
# File 'lib/virtus/support/type_lookup.rb', line 34

def determine_type(class_or_name)
  @type_lookup_cache[class_or_name] ||= determine_type_and_cache(class_or_name)
end

#primitiveClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the default primitive supported

Returns:

  • (Class)

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/virtus/support/type_lookup.rb', line 43

def primitive
  raise NotImplementedError, "#{self}.primitive must be implemented"
end