Module: Omml::Configuration::ContextRegistry

Included in:
Omml::Configuration
Defined in:
lib/omml/configuration/context_registry.rb

Overview

Extended only by Omml::Configuration together with the sibling modules.

Instance Method Summary collapse

Instance Method Details

#context(id = context_id) ⇒ Object



7
8
9
# File 'lib/omml/configuration/context_registry.rb', line 7

def context(id = context_id)
  Lutaml::Model::GlobalContext.context(id.to_sym)
end

#create_context(id:, registry: nil, fallback_to: [context_id], substitutions: []) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/omml/configuration/context_registry.rb', line 11

def create_context(id:, registry: nil, fallback_to: [context_id],
                   substitutions: [])
  normalized_id = normalize_context_id(id)
  if default_context_id?(normalized_id)
    return create_default_context(registry, fallback_to,
                                  substitutions)
  end

  create_custom_context(
    id: normalized_id, registry: registry,
    fallback_to: fallback_to, substitutions: substitutions
  )
end

#populate_context!Object



25
26
27
28
29
30
31
# File 'lib/omml/configuration/context_registry.rb', line 25

def populate_context!
  # Eager-load every model so its `inherited` hook fires and the class
  # is registered before we replay registrations into the new context.
  Omml::Models.eager_load!
  reset_context!
  register_models_in(populate_base_context)
end

#register_in(consumer_id, fallback_to: nil) ⇒ Lutaml::Model::Context

Register Omml's models into a consumer-owned context.

Consumers (e.g. Uniword) call this once during boot to make every OMML schema type resolvable from the consumer's own context. The consumer's registry is populated directly (via register_models_in) rather than via fallback, so the Omml register does not need to be in the chain — the consumer becomes self-sufficient for OMML types.

The default fallback is [:default] so consumer-defined types still resolve through lutaml-model's built-in type set when not shadowed by an Omml registration.

Examples:

Uniword boot

Omml::Configuration.register_in(:uniword)
Uniword::DocumentRoot.from_xml(xml, register: :uniword)

Parameters:

  • consumer_id (Symbol, String)

    The consumer's context id.

  • fallback_to (Array<Symbol>, nil) (defaults to: nil)

    Override fallback chain. Defaults to [:default].

Returns:

  • (Lutaml::Model::Context)

    The created context.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/omml/configuration/context_registry.rb', line 61

def register_in(consumer_id, fallback_to: nil)
  normalized_id = normalize_context_id(consumer_id)
  chain = Array(fallback_to || [:default])
  populate_context!
  create_context(
    id: normalized_id,
    registry: Lutaml::Model::TypeRegistry.new,
    fallback_to: chain,
  ).tap do |type_context|
    register_models_in(type_context)
  end
end

#require_context!(id = context_id) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/omml/configuration/context_registry.rb', line 33

def require_context!(id = context_id)
  normalized_id = id.to_sym
  type_context = context(normalized_id)
  return type_context if type_context

  raise Omml::Errors::MissingContextError, normalized_id
end

#reset_context!(id: context_id) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/omml/configuration/context_registry.rb', line 74

def reset_context!(id: context_id)
  normalized_id = normalize_context_id(id)
  existing_context = Lutaml::Model::GlobalContext.context(normalized_id)
  return unless existing_context

  Lutaml::Model::GlobalContext.unregister_context(normalized_id)
end