Class: Lutaml::Store::ModelRegistration
- Inherits:
-
Object
- Object
- Lutaml::Store::ModelRegistration
- Defined in:
- lib/lutaml/store/model_registration.rb
Overview
Represents a single model registration with its metadata
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#key_field ⇒ Object
readonly
Returns the value of attribute key_field.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#polymorphic_class_key ⇒ Object
readonly
Returns the value of attribute polymorphic_class_key.
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
Instance Method Summary collapse
-
#extract_key(model) ⇒ Object
Extract key value from model instance.
-
#extract_polymorphic_class(model) ⇒ Object
Extract polymorphic class from model instance.
-
#generate_storage_key(model) ⇒ Object
Generate storage key for model.
-
#generate_storage_key_from_value(key_value, polymorphic_class = nil) ⇒ Object
Generate storage key from key value and optional polymorphic class.
-
#initialize(model_class, key_field, polymorphic_class_key: nil, serializer: nil, dir: nil) ⇒ ModelRegistration
constructor
A new instance of ModelRegistration.
-
#matches_model?(model_class) ⇒ Boolean
Check if model class matches this registration (including inheritance).
-
#polymorphic? ⇒ Boolean
Check if registration supports polymorphism.
Constructor Details
#initialize(model_class, key_field, polymorphic_class_key: nil, serializer: nil, dir: nil) ⇒ ModelRegistration
Returns a new instance of ModelRegistration.
9 10 11 12 13 14 15 16 |
# File 'lib/lutaml/store/model_registration.rb', line 9 def initialize(model_class, key_field, polymorphic_class_key: nil, serializer: nil, dir: nil) @model_class = model_class @key_field = key_field.to_sym @polymorphic_class_key = polymorphic_class_key&.to_sym @serializer = serializer @dir = dir validate! end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
7 8 9 |
# File 'lib/lutaml/store/model_registration.rb', line 7 def dir @dir end |
#key_field ⇒ Object (readonly)
Returns the value of attribute key_field.
7 8 9 |
# File 'lib/lutaml/store/model_registration.rb', line 7 def key_field @key_field end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
7 8 9 |
# File 'lib/lutaml/store/model_registration.rb', line 7 def model_class @model_class end |
#polymorphic_class_key ⇒ Object (readonly)
Returns the value of attribute polymorphic_class_key.
7 8 9 |
# File 'lib/lutaml/store/model_registration.rb', line 7 def polymorphic_class_key @polymorphic_class_key end |
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
7 8 9 |
# File 'lib/lutaml/store/model_registration.rb', line 7 def serializer @serializer end |
Instance Method Details
#extract_key(model) ⇒ Object
Extract key value from model instance
19 20 21 22 23 24 |
# File 'lib/lutaml/store/model_registration.rb', line 19 def extract_key(model) key_value = model.public_send(@key_field) raise InvalidKeyError, "Key field '#{@key_field}' is nil for #{@model_class}" if key_value.nil? key_value.to_s end |
#extract_polymorphic_class(model) ⇒ Object
Extract polymorphic class from model instance
32 33 34 35 36 37 |
# File 'lib/lutaml/store/model_registration.rb', line 32 def extract_polymorphic_class(model) return @model_class.name unless polymorphic? polymorphic_value = model.public_send(@polymorphic_class_key) polymorphic_value || @model_class.name end |
#generate_storage_key(model) ⇒ Object
Generate storage key for model
40 41 42 |
# File 'lib/lutaml/store/model_registration.rb', line 40 def generate_storage_key(model) StorageKey.new(model.class.name, extract_key(model)) end |
#generate_storage_key_from_value(key_value, polymorphic_class = nil) ⇒ Object
Generate storage key from key value and optional polymorphic class
45 46 47 48 |
# File 'lib/lutaml/store/model_registration.rb', line 45 def generate_storage_key_from_value(key_value, polymorphic_class = nil) class_name = polymorphic_class || @model_class.name StorageKey.new(class_name, key_value) end |
#matches_model?(model_class) ⇒ Boolean
Check if model class matches this registration (including inheritance)
51 52 53 |
# File 'lib/lutaml/store/model_registration.rb', line 51 def matches_model?(model_class) model_class <= @model_class end |
#polymorphic? ⇒ Boolean
Check if registration supports polymorphism
27 28 29 |
# File 'lib/lutaml/store/model_registration.rb', line 27 def polymorphic? !@polymorphic_class_key.nil? end |