Class: Lutaml::Model::Register

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/register.rb,
lib/lutaml/model/error/register/not_registrable_class_error.rb

Defined Under Namespace

Classes: NotRegistrableClassError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Register

Returns a new instance of Register.



8
9
10
11
12
13
# File 'lib/lutaml/model/register.rb', line 8

def initialize(id)
  @id = id
  @models = {}
  @global_substitutions = {}
  @type_class_cache = {}
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/lutaml/model/register.rb', line 6

def id
  @id
end

#modelsObject (readonly)

Returns the value of attribute models.



6
7
8
# File 'lib/lutaml/model/register.rb', line 6

def models
  @models
end

Instance Method Details

#get_class(klass_name) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/lutaml/model/register.rb', line 28

def get_class(klass_name)
  expected_class = get_class_without_register(klass_name)
  if !(expected_class < Lutaml::Model::Type::Value)
    expected_class.class_variable_set(:@@register, id)
  end
  expected_class
end

#get_class_without_register(klass_name) ⇒ Object



64
65
66
67
68
69
# File 'lib/lutaml/model/register.rb', line 64

def get_class_without_register(klass_name)
  klass = extract_class_from(klass_name)
  raise Lutaml::Model::UnknownTypeError.new(klass_name) unless klass

  substitute(klass) || substitute(klass_name) || klass
end

#register_attributes(attributes) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/lutaml/model/register.rb', line 47

def register_attributes(attributes)
  attributes.each_value do |attribute|
    next unless attribute.unresolved_type.is_a?(Class)
    next if built_in_type?(attribute.unresolved_type) || attribute.unresolved_type.nil?

    register_model_tree(attribute.unresolved_type)
  end
end

#register_global_type_substitution(from_type:, to_type:) ⇒ Object



43
44
45
# File 'lib/lutaml/model/register.rb', line 43

def register_global_type_substitution(from_type:, to_type:)
  @global_substitutions[from_type] = to_type
end

#register_model(klass, id: nil) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/lutaml/model/register.rb', line 15

def register_model(klass, id: nil)
  id ||= Utils.base_class_snake_case(klass).to_sym
  return Lutaml::Model::Type.register(id, klass) if klass <= Lutaml::Model::Type::Value
  raise NotRegistrableClassError.new(klass) unless klass.include?(Lutaml::Model::Registrable)

  @models[id.to_sym] = klass
  @models[klass.to_s] = klass
end

#register_model_tree(klass) ⇒ Object



36
37
38
39
40
41
# File 'lib/lutaml/model/register.rb', line 36

def register_model_tree(klass)
  register_model(klass)
  if klass.include?(Lutaml::Model::Serialize)
    register_attributes(klass.attributes)
  end
end

#resolve(klass_str) ⇒ Object



24
25
26
# File 'lib/lutaml/model/register.rb', line 24

def resolve(klass_str)
  @models[klass_str.to_s]
end

#substitutable?(klass) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/lutaml/model/register.rb', line 56

def substitutable?(klass)
  @global_substitutions.key?(klass)
end

#substitute(klass) ⇒ Object



60
61
62
# File 'lib/lutaml/model/register.rb', line 60

def substitute(klass)
  @global_substitutions[klass]
end