Class: Openphar::Registry::TypeRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/openphar/registry/type_registry.rb

Overview

Central registry for mapping JSON-LD @type to Ruby model classes.

This registry provides a single source of truth for type mappings, decoupling type resolution from specific exporters (like Neo4j).

Built-in Types

The registry comes pre-populated with all core and publisher-specific model types. Publishers can register additional types at runtime.

Examples:

Looking up a model class

klass = TypeRegistry.for('CrudeDrugMonograph')
# => Openphar::Models::CrudeDrugMonograph

Registering a custom type

TypeRegistry.register('CustomType', MyCustomModel)

Checking if a type is registered

TypeRegistry.registered?('CrudeDrugMonograph') # => true

Class Method Summary collapse

Class Method Details

.all_classesArray<Class>

Returns all registered model classes.

Returns:

  • (Array<Class>)

    List of model classes



73
74
75
# File 'lib/openphar/registry/type_registry.rb', line 73

def all_classes
  registry.values.map { |v| v.is_a?(Proc) ? v.call : v }
end

.all_typesArray<String>

Returns all registered type names.

Returns:

  • (Array<String>)

    List of registered types



66
67
68
# File 'lib/openphar/registry/type_registry.rb', line 66

def all_types
  registry.keys
end

.clearvoid

This method returns an undefined value.

Clears all registered types (useful for testing).



80
81
82
# File 'lib/openphar/registry/type_registry.rb', line 80

def clear
  @registry = nil
end

.for(type) ⇒ Class?

Returns the model class for a given JSON-LD @type.

Parameters:

  • type (String)

    The JSON-LD @type value

Returns:

  • (Class, nil)

    The model class or nil if not found



31
32
33
34
35
36
# File 'lib/openphar/registry/type_registry.rb', line 31

def for(type)
  return nil if type.nil? || type.empty?

  entry = registry[type]
  entry.is_a?(Proc) ? entry.call : entry
end

.register(type, model_class) ⇒ void

This method returns an undefined value.

Registers a new type-to-class mapping.

Parameters:

  • type (String)

    The JSON-LD @type value

  • model_class (Class, Proc)

    The model class or a lambda



43
44
45
# File 'lib/openphar/registry/type_registry.rb', line 43

def register(type, model_class)
  registry[type] = model_class
end

.registered?(type) ⇒ Boolean

Checks if a type is registered.

Parameters:

  • type (String)

    The JSON-LD @type value

Returns:

  • (Boolean)

    True if registered



59
60
61
# File 'lib/openphar/registry/type_registry.rb', line 59

def registered?(type)
  registry.key?(type)
end

.resetvoid

This method returns an undefined value.

Resets to default registrations.



87
88
89
# File 'lib/openphar/registry/type_registry.rb', line 87

def reset
  clear
end

.unregister(type) ⇒ void

This method returns an undefined value.

Unregisters a type mapping.

Parameters:

  • type (String)

    The JSON-LD @type value



51
52
53
# File 'lib/openphar/registry/type_registry.rb', line 51

def unregister(type)
  registry.delete(type)
end