Module: Lutaml::Model::Schema::Renderers::Registration

Defined in:
lib/lutaml/model/schema/renderers/registration.rb

Overview

Stateless helper for emitting the self.register and self.register_class_with_id method block plus the trailing execution line, shared by the Model, RestrictedType, and Union renderers.

Cases:

module_namespace = nil          -> emit both methods + execution
namespace + keep_when_namespaced -> emit only `self.register`
namespace + !keep              -> emit nothing

Class Method Summary collapse

Class Method Details

.execution_line(class_name:, module_namespace:) ⇒ Object



27
28
29
30
31
# File 'lib/lutaml/model/schema/renderers/registration.rb', line 27

def execution_line(class_name:, module_namespace:)
  return "" if module_namespace

  "\n#{class_name}.register_class_with_id\n"
end

.full_block(class_name, indent, lazy) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lutaml/model/schema/renderers/registration.rb', line 42

def full_block(class_name, indent, lazy)
  target = Utils.snake_case(class_name).to_sym
  <<~REG.gsub(/^/, indent)

    def self.register
    #{indent}#{register_body(lazy)}
    end

    def self.register_class_with_id
    #{indent}context = Lutaml::Model::GlobalContext.context(Lutaml::Model::Config.default_register)
    #{indent}context.registry.register(:#{target}, self)
    end
  REG
end

.methods_block(class_name:, module_namespace:, indent:, lazy: false, keep_when_namespaced: false) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/lutaml/model/schema/renderers/registration.rb', line 19

def methods_block(class_name:, module_namespace:, indent:,
                  lazy: false, keep_when_namespaced: false)
  return "" if module_namespace && !keep_when_namespaced
  return register_only_block(indent, lazy) if module_namespace

  full_block(class_name, indent, lazy)
end

.register_body(lazy) ⇒ Object



57
58
59
# File 'lib/lutaml/model/schema/renderers/registration.rb', line 57

def register_body(lazy)
  lazy ? "@register ||= Lutaml::Model::Config.default_register" : "Lutaml::Model::Config.default_register"
end

.register_only_block(indent, lazy) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/lutaml/model/schema/renderers/registration.rb', line 33

def register_only_block(indent, lazy)
  <<~REG.gsub(/^/, indent)

    def self.register
    #{indent}#{register_body(lazy)}
    end
  REG
end