Class: Lutaml::Model::Mapping

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

Instance Method Summary collapse

Methods included from DeepDupable

#deep_dup

Constructor Details

#initializeMapping

Returns a new instance of Mapping.



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

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

Instance Method Details

#add_listener(listener) ⇒ void

This method returns an undefined value.

Add a listener to this mapping.

Parameters:



27
28
29
# File 'lib/lutaml/model/mapping/mapping.rb', line 27

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

#all_listenersArray<Lutaml::Model::Listener>

Get all listeners across all targets.

Returns:



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

def all_listeners
  @listeners.values.flatten.freeze
end

#ensure_mappings_imported!(register_id = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lutaml/model/mapping/mapping.rb', line 79

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



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

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:



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

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

#mappingsObject

Raises:

  • (NotImplementedError)


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

def mappings
  raise NotImplementedError,
        "#{self.class.name} must implement `mappings`."
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



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

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



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

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)


70
71
72
# File 'lib/lutaml/model/mapping/mapping.rb', line 70

def parent_mapping
  @parent_mapping
end