Class: Lutaml::Model::Mapping
- Inherits:
-
Object
- Object
- Lutaml::Model::Mapping
- Includes:
- DeepDupable
- Defined in:
- lib/lutaml/model/mapping/mapping.rb
Direct Known Subclasses
KeyValue::Mapping, Liquid::Mapping, Rdf::Mapping, Xml::Mapping
Instance Attribute Summary collapse
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.
- #deep_dup ⇒ Object
- #duplicate_mappings ⇒ Object
- #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).
-
#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.
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
#mappings ⇒ Object
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.
39 40 41 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 39 def add_listener(listener) listeners_for(listener.target) << listener end |
#all_listeners ⇒ Array<Lutaml::Model::Listener>
Get all listeners across all targets.
46 47 48 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 46 def all_listeners @listeners.values.flatten.freeze end |
#deep_dup ⇒ Object
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_mappings ⇒ Object
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.
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).
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.
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.
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_mapping ⇒ Class?
Get the parent mapping class if any.
82 83 84 |
# File 'lib/lutaml/model/mapping/mapping.rb', line 82 def parent_mapping @parent_mapping end |