Class: Lutaml::Model::Mapping

Inherits:
Object
  • Object
show all
Includes:
DeepDupable
Defined in:
lib/lutaml/model/mapping/mapping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Mapping

Returns a new instance of Mapping.



21
22
23
24
25
26
27
# File 'lib/lutaml/model/mapping/mapping.rb', line 21

def initialize
  @mappings = []
  @listeners = {} # target => [Listener, ...]
  @parent_mapping = nil
  @importable_mappings = []
  @mappings_imported = ::Hash.new { |h, k| h[k] = false }
end

Instance Attribute Details

#mappings ⇒ Object

Raises:

  • (NotImplementedError)


99
100
101
102
# File 'lib/lutaml/model/mapping/mapping.rb', line 99

def mappings
  raise NotImplementedError,
        "#{self.class.name} must implement `mappings`."
end

Instance Method Details

#add_listener(listener) ⇒ void

This method returns an undefined value.

Add a listener to this mapping.

Parameters:



52
53
54
# File 'lib/lutaml/model/mapping/mapping.rb', line 52

def add_listener(listener)
  listeners_for(listener.target) << listener
end

#all_listeners ⇒ Array<Lutaml::Model::Listener>

Get all listeners across all targets.

Returns:



59
60
61
# File 'lib/lutaml/model/mapping/mapping.rb', line 59

def all_listeners
  @listeners.values.flatten.freeze
end

#deep_dup ⇒ Object



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

def deep_dup
  duped = self.class.new
  duped.mappings = duplicate_mappings
  duped
end

#duplicate_mappings ⇒ Object



35
36
37
# File 'lib/lutaml/model/mapping/mapping.rb', line 35

def duplicate_mappings
  Lutaml::Model::Utils.deep_dup(@mappings)
end

#ensure_mappings_imported!(register_id = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/lutaml/model/mapping/mapping.rb', line 104

def ensure_mappings_imported!(register_id = nil)
  register_object = register(register_id)
  return if @mappings_imported[register_object.id]

  importable_mappings.each do |model|
    import_model_mappings(
      register_object.get_class_without_register(model),
      register_object.id,
    )
  end

  @mappings_imported[register_object.id] = true
end

#inherit_from(parent) ⇒ void

This method returns an undefined value.

Inherit listeners from another mapping class.

This copies all listeners from the parent mapping into this one. When override by ID is needed, the child's listener takes precedence.

Parameters:

  • parent (Class) —

    A Lutaml::Model::Mapping subclass



88
89
90
# File 'lib/lutaml/model/mapping/mapping.rb', line 88

def inherit_from(parent)
  @parent_mapping = parent
end

#listeners_for(target) ⇒ Array<Lutaml::Model::Listener>

Get listeners for a specific target (element name/key).

Parameters:

  • target (String, Symbol) —

    The element name or key

Returns:



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

def listeners_for(target)
  target_str = target.to_s if target
  @listeners[target_str] ||= []
end

#namespace_class ⇒ Object



15
16
17
# File 'lib/lutaml/model/mapping/mapping.rb', line 15

def namespace_class
  nil
end

#omit_element(target) ⇒ void

This method returns an undefined value.

Remove ALL listeners for a given target.

Parameters:

  • target (String, Symbol) —

    The element name or key



67
68
69
70
# File 'lib/lutaml/model/mapping/mapping.rb', line 67

def omit_element(target)
  target_str = target.to_s if target
  @listeners.delete(target_str)
end

#omit_listener(target, id:) ⇒ void

This method returns an undefined value.

Remove a specific listener by ID.

Parameters:

  • target (String, Symbol) —

    The element name or key

  • id (Symbol, String) —

    The listener ID to remove



77
78
79
# File 'lib/lutaml/model/mapping/mapping.rb', line 77

def omit_listener(target, id:)
  listeners_for(target).reject! { |l| l.id == id }
end

#parent_mapping ⇒ Class?

Get the parent mapping class if any.

Returns:

  • (Class, nil)


95
96
97
# File 'lib/lutaml/model/mapping/mapping.rb', line 95

def parent_mapping
  @parent_mapping
end

#root_mappings? ⇒ Boolean

Uniform interface across mapping families: every mapping answers these, so consumers call them unconditionally. The families that carry the feature override (KeyValue's root_mappings, Xml's namespace_class); the base defaults make the absence explicit instead of probed.

Returns:

  • (Boolean)


11
12
13
# File 'lib/lutaml/model/mapping/mapping.rb', line 11

def root_mappings?
  false
end