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

#initializeMapping

Returns a new instance of Mapping.



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

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

Instance Attribute Details

#mappingsObject

Raises:

  • (NotImplementedError)


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

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:



39
40
41
# File 'lib/lutaml/model/mapping/mapping.rb', line 39

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

#all_listenersArray<Lutaml::Model::Listener>

Get all listeners across all targets.

Returns:



46
47
48
# File 'lib/lutaml/model/mapping/mapping.rb', line 46

def all_listeners
  @listeners.values.flatten.freeze
end

#deep_dupObject



16
17
18
19
20
# File 'lib/lutaml/model/mapping/mapping.rb', line 16

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

#duplicate_mappingsObject



22
23
24
# File 'lib/lutaml/model/mapping/mapping.rb', line 22

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

#ensure_mappings_imported!(register_id = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/lutaml/model/mapping/mapping.rb', line 91

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



75
76
77
# File 'lib/lutaml/model/mapping/mapping.rb', line 75

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:



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

def listeners_for(target)
  target_str = target.to_s if target
  @listeners[target_str] ||= []
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



54
55
56
57
# File 'lib/lutaml/model/mapping/mapping.rb', line 54

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



64
65
66
# File 'lib/lutaml/model/mapping/mapping.rb', line 64

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

#parent_mappingClass?

Get the parent mapping class if any.

Returns:

  • (Class, nil)


82
83
84
# File 'lib/lutaml/model/mapping/mapping.rb', line 82

def parent_mapping
  @parent_mapping
end