Module: Dcc::ContextConfiguration

Included in:
Si::V1::Configuration, Si::V2::Configuration, V2::Configuration, V3::Configuration
Defined in:
lib/dcc/context_configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_context_idSymbol

Returns the context id used when none is supplied explicitly.

Returns:

  • (Symbol)

    the context id used when none is supplied explicitly.



78
79
80
# File 'lib/dcc/context_configuration.rb', line 78

def default_context_id
  @default_context_id || context_id
end

Instance Method Details

#clear_custom_modelsObject

Reset any custom default context.



97
98
99
# File 'lib/dcc/context_configuration.rb', line 97

def clear_custom_models
  @default_context_id = nil
end

#contextLutaml::Model::Context?

Returns the live context for this version, or nil if not yet populated.

Returns:

  • (Lutaml::Model::Context, nil)

    the live context for this version, or nil if not yet populated.



26
27
28
# File 'lib/dcc/context_configuration.rb', line 26

def context
  Lutaml::Model::GlobalContext.context(context_id)
end

#context_idSymbol

Returns the version's canonical context id (e.g. :dcc_v3).

Returns:

  • (Symbol)

    the version's canonical context id (e.g. :dcc_v3).



20
21
22
# File 'lib/dcc/context_configuration.rb', line 20

def context_id
  self::CONTEXT_ID
end

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

Create or replace a context. If id matches the version's canonical context id, the built-in context is rebuilt via populate_context!.

Parameters:

  • id (Symbol, String)

    context name.

  • registry (Lutaml::Model::TypeRegistry, nil) (defaults to: nil)

    custom registry.

  • fallback_to (Array<Symbol>) (defaults to: [context_id])

    fallback chain for type lookup.

  • substitutions (Array<Hash{from_type:, to_type:}>) (defaults to: [])

    type substitutions.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dcc/context_configuration.rb', line 37

def create_context(id:, registry: nil, fallback_to: [context_id], substitutions: [])
  normalized_id = ::Dcc::ContextOptions.normalize_context_reference(id)
  return populate_context! if normalized_id == context_id

  unregister_existing(normalized_id)
  create_type_context(
    id: normalized_id,
    registry: registry || Lutaml::Model::TypeRegistry.new,
    fallback_to: normalize_fallbacks(fallback_to),
    substitutions: substitutions,
  )
end

#custom_models=(models) ⇒ Object

Convenience: bulk-register model substitutions.

Parameters:

  • models (Hash{Class => Class})

    from_type => to_type.



88
89
90
91
92
93
94
# File 'lib/dcc/context_configuration.rb', line 88

def custom_models=(models)
  substitutions = models.map do |from_type, to_type|
    { from_type: from_type, to_type: to_type }
  end
  create_context(id: :custom_models, substitutions: substitutions)
  @default_context_id = :custom_models
end

#fallback_contextsArray<Symbol>

Contexts that this one falls back to when type lookup fails. Override in version modules to pull in cross-namespace types (e.g. DCC contexts fall back to D-SI contexts for :real, :hybrid).

Returns:

  • (Array<Symbol>)


60
61
62
# File 'lib/dcc/context_configuration.rb', line 60

def fallback_contexts
  [:default]
end

#populate_context!Object

Rebuild the version's built-in context from registered_models.



51
52
53
54
# File 'lib/dcc/context_configuration.rb', line 51

def populate_context!
  unregister_existing(context_id)
  register_models_in(base_type_context)
end

#register_model(klass, id:) ⇒ Class

Register a model for an XML element name and remember it for rebuilds.

Parameters:

  • klass (Class)

    the model class.

  • id (Symbol, String)

    XML element local name (e.g. :digitalCalibrationCertificate).

Returns:

  • (Class)

    the registered class.



69
70
71
72
73
74
75
# File 'lib/dcc/context_configuration.rb', line 69

def register_model(klass, id:)
  normalized_id = id.to_sym
  registered_models[normalized_id] = klass
  (context || populate_base_context).registry.register(normalized_id, klass)
  clear_cache(klass)
  klass
end

#registered_model_class(id) ⇒ Class?

Returns the registered class.

Parameters:

  • id (Symbol)

    the model id.

Returns:

  • (Class, nil)

    the registered class.



109
110
111
# File 'lib/dcc/context_configuration.rb', line 109

def registered_model_class(id)
  registered_models[id.to_sym]
end

#registered_model_idsArray<Symbol>

Public read-only accessors for cross-context re-export.

Returns:

  • (Array<Symbol>)

    ids registered in this context.



103
104
105
# File 'lib/dcc/context_configuration.rb', line 103

def registered_model_ids
  registered_models.keys
end