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 models that are registered and nested within other models.
-
#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 models that are registered and nested within other models
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/lutaml/store/model_registry.rb', line 85 def find_composite_models(model) composite_models = {} model.class.attributes.each_key do |attr_name| attr_value = model.public_send(attr_name) next if attr_value.nil? add_composite_entry(composite_models, attr_name, attr_value) if attr_value.is_a?(Object) && registered?(attr_value.class) next unless attr_value.is_a?(Array) attr_value.each_with_index do |item, index| next unless item.is_a?(Object) && registered?(item.class) add_composite_entry(composite_models, "#{attr_name}.#{index}", item) end end composite_models rescue NoMethodError {} 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 |