Class: Lutaml::Store::ModelRegistry
- Inherits:
-
Object
- Object
- Lutaml::Store::ModelRegistry
- Defined in:
- lib/lutaml/store/model_registry.rb
Overview
Manages registered models with their key fields and polymorphic configurations
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all registrations.
-
#count ⇒ Object
Get count of registered models.
-
#empty? ⇒ Boolean
Check if any models are registered.
-
#find_composite_models(model) ⇒ Object
Find declared composite models nested within the given model.
-
#find_registration(model_class) ⇒ Object
Find registration for model class or its superclass.
-
#initialize(model_configs = []) ⇒ ModelRegistry
constructor
A new instance of ModelRegistry.
-
#register(model_class, key_field, **options) ⇒ Object
Register a single model.
-
#register_models(model_configs) ⇒ Object
Register multiple models from configuration array.
-
#registered?(model_class) ⇒ Boolean
Check if model is registered.
-
#registered_models ⇒ Object
Get all registered model classes.
-
#registration_for(model_class) ⇒ Object
Get registration for model class (raises error if not found).
-
#registrations ⇒ Object
Get all registrations.
Constructor Details
#initialize(model_configs = []) ⇒ ModelRegistry
Returns a new instance of ModelRegistry.
7 8 9 10 |
# File 'lib/lutaml/store/model_registry.rb', line 7 def initialize(model_configs = []) @registrations = {} register_models(model_configs) end |
Instance Method Details
#clear ⇒ Object
Clear all registrations
80 81 82 |
# File 'lib/lutaml/store/model_registry.rb', line 80 def clear @registrations.clear end |
#count ⇒ Object
Get count of registered models
75 76 77 |
# File 'lib/lutaml/store/model_registry.rb', line 75 def count @registrations.size end |
#empty? ⇒ Boolean
Check if any models are registered
70 71 72 |
# File 'lib/lutaml/store/model_registry.rb', line 70 def empty? @registrations.empty? end |
#find_composite_models(model) ⇒ Object
Find declared composite models nested within the given model
85 86 87 88 89 90 91 92 |
# File 'lib/lutaml/store/model_registry.rb', line 85 def find_composite_models(model) registration = find_registration(model.class) return {} unless registration&.composites&.any? registration.composites.each_with_object({}) do |attr_name, result| collect_composite(model, attr_name, result) end end |
#find_registration(model_class) ⇒ Object
Find registration for model class or its superclass
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lutaml/store/model_registry.rb', line 31 def find_registration(model_class) # First try exact match return @registrations[model_class] if @registrations[model_class] # Then try inheritance chain @registrations.each_value do |registration| return registration if registration.matches_model?(model_class) end nil end |
#register(model_class, key_field, **options) ⇒ Object
Register a single model
13 14 15 16 17 |
# File 'lib/lutaml/store/model_registry.rb', line 13 def register(model_class, key_field, **) registration = ModelRegistration.new(model_class, key_field, **) @registrations[model_class] = registration registration end |
#register_models(model_configs) ⇒ Object
Register multiple models from configuration array
20 21 22 23 24 25 26 27 28 |
# File 'lib/lutaml/store/model_registry.rb', line 20 def register_models(model_configs) model_configs.each do |config| raise ConfigurationError, "Invalid model configuration: #{config}" unless config.is_a?(Hash) model_class = config[:model] key_field = config[:key] register(model_class, key_field, **config.except(:model, :key)) end end |
#registered?(model_class) ⇒ Boolean
Check if model is registered
44 45 46 |
# File 'lib/lutaml/store/model_registry.rb', line 44 def registered?(model_class) !find_registration(model_class).nil? end |
#registered_models ⇒ Object
Get all registered model classes
60 61 62 |
# File 'lib/lutaml/store/model_registry.rb', line 60 def registered_models @registrations.keys end |
#registration_for(model_class) ⇒ Object
Get registration for model class (raises error if not found)
49 50 51 52 53 54 55 56 57 |
# File 'lib/lutaml/store/model_registry.rb', line 49 def registration_for(model_class) registration = find_registration(model_class) unless registration raise ModelNotRegisteredError, "Model #{model_class} is not registered. " \ "Register it with: models: [{ model: #{model_class}, key: :key_field }]" end registration end |
#registrations ⇒ Object
Get all registrations
65 66 67 |
# File 'lib/lutaml/store/model_registry.rb', line 65 def registrations @registrations.values end |