Class: Lutaml::Model::Mapping
- Inherits:
-
Object
- Object
- Lutaml::Model::Mapping
- Defined in:
- lib/lutaml/model/mapping/mapping.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#add_listener(listener) ⇒ void
Add a listener to this mapping.
-
#all_listeners ⇒ Array<Lutaml::Model::Listener>
Get all listeners across all targets.
- #ensure_mappings_imported!(register_id = nil) ⇒ Object
-
#inherit_from(parent) ⇒ void
Inherit listeners from another mapping class.
-
#initialize ⇒ Mapping
constructor
A new instance of Mapping.
-
#listeners_for(target) ⇒ Array<Lutaml::Model::Listener>
Get listeners for a specific target (element name/key).
- #mappings ⇒ Object
-
#omit_element(target) ⇒ void
Remove ALL listeners for a given target.
-
#omit_listener(target, id:) ⇒ void
Remove a specific listener by ID.
-
#parent_mapping ⇒ Class?
Get the parent mapping class if any.
Constructor Details
#initialize ⇒ Mapping
Returns a new instance of Mapping.
4 5 6 7 8 9 10 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 4 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.
25 26 27 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 25 def add_listener(listener) listeners_for(listener.target) << listener end |
#all_listeners ⇒ Array<Lutaml::Model::Listener>
Get all listeners across all targets.
32 33 34 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 32 def all_listeners @listeners.values.flatten.freeze end |
#ensure_mappings_imported!(register_id = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 77 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.
61 62 63 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 61 def inherit_from(parent) @parent_mapping = parent end |
#listeners_for(target) ⇒ Array<Lutaml::Model::Listener>
Get listeners for a specific target (element name/key).
16 17 18 19 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 16 def listeners_for(target) target_str = target.to_s if target @listeners[target_str] ||= [] end |
#mappings ⇒ Object
72 73 74 75 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 72 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.
40 41 42 43 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 40 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.
50 51 52 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 50 def omit_listener(target, id:) listeners_for(target).reject! { |l| l.id == id } end |
#parent_mapping ⇒ Class?
Get the parent mapping class if any.
68 69 70 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 68 def parent_mapping @parent_mapping end |