Module: Lutaml::Lml::Executor::AdapterHelpers
- Included in:
- CsvAdapter, XmlAdapter
- Defined in:
- lib/lutaml/lml/executor/adapter_helpers.rb
Overview
Shared helpers for the I/O adapters. Both CsvAdapter and XmlAdapter need to:
- resolve a `map_to` reference to a compiled class
- look up attribute values by name in an import/export definition
- find the compiled class that an instance belongs to (for export)
This module owns those three concerns so each adapter can stay focused on its format-specific I/O.
Instance Method Summary collapse
-
#attribute_value(attributes, name) ⇒ Object
Returns the string value of the named attribute, or nil.
-
#find_attribute(attributes, name) ⇒ Object
Walks an attributes collection looking for an entry whose ‘.name` matches `name`.
-
#find_class_for_instance(first_instance, compiled) ⇒ Object
Returns [class_name, klass] for the first compiled class whose instances include ‘first_instance`, or nil if no match.
-
#resolve_target_class(attributes, compiled) ⇒ Object
Returns the compiled class referenced by the ‘map_to` attribute, or nil if no such attribute or no such compiled class.
Instance Method Details
#attribute_value(attributes, name) ⇒ Object
Returns the string value of the named attribute, or nil.
26 27 28 29 |
# File 'lib/lutaml/lml/executor/adapter_helpers.rb', line 26 def attribute_value(attributes, name) attr = find_attribute(attributes, name) attr ? attr.value.to_s : nil end |
#find_attribute(attributes, name) ⇒ Object
Walks an attributes collection looking for an entry whose ‘.name` matches `name`. Returns the attribute, or nil.
33 34 35 |
# File 'lib/lutaml/lml/executor/adapter_helpers.rb', line 33 def find_attribute(attributes, name) Array(attributes).find { |a| a.name == name } end |
#find_class_for_instance(first_instance, compiled) ⇒ Object
Returns [class_name, klass] for the first compiled class whose instances include ‘first_instance`, or nil if no match.
39 40 41 |
# File 'lib/lutaml/lml/executor/adapter_helpers.rb', line 39 def find_class_for_instance(first_instance, compiled) compiled.find { |_name, klass| first_instance.is_a?(klass) } end |
#resolve_target_class(attributes, compiled) ⇒ Object
Returns the compiled class referenced by the ‘map_to` attribute, or nil if no such attribute or no such compiled class.
18 19 20 21 22 23 |
# File 'lib/lutaml/lml/executor/adapter_helpers.rb', line 18 def resolve_target_class(attributes, compiled) attr = find_attribute(attributes, "map_to") return nil unless attr compiled[attr.value.to_s] end |