Class: Inferno::DSL::ProfileMetadata
- Inherits:
-
Object
- Object
- Inferno::DSL::ProfileMetadata
- Defined in:
- lib/inferno/dsl/profile_metadata.rb
Overview
A ProfileMetadata object is a small, inheritable data holder for generated metadata about a
profile or a group of tests -- most notably the Must Support metadata consumed by
MustSupportAssessment#missing_must_support_elements and
Assertions#assert_must_support_elements_present via their metadata: argument, alongside
whatever other generated metadata a test kit needs to carry around (searches, bindings,
references, etc).
It exists so that test kits don't need to reach for OpenStruct (which accepts any attribute
silently, making typos and generator/consumer drift hard to catch) or hand-roll their own
version of this exact pattern.
Test kits define a subclass and declare whatever attributes they need with .attribute.
must_supports is always available, since it's the attribute Inferno's Must Support logic
reads:
class GroupMetadata < Inferno::DSL::ProfileMetadata
attribute :searches
attribute :bindings
end
= GroupMetadata.from_file('path/to/generated/metadata.yml')
.must_supports #=> { elements: [...], extensions: [...], slices: [...] }
Class Method Summary collapse
-
.attribute(name) ⇒ void
Declare an attribute that instances of this class (and subclasses) may be initialized with.
-
.attribute_names ⇒ Array<Symbol>
The full list of attribute names declared on this class and its ancestors.
-
.from_file(path) ⇒ ProfileMetadata
Build an instance from a YAML file, eg one written by a metadata generator.
Instance Method Summary collapse
-
#initialize(metadata = {}) ⇒ ProfileMetadata
constructor
A new instance of ProfileMetadata.
-
#must_support_strings ⇒ Array<String>
Every Must Support element, slice, and extension in
must_supports, each represented as a single string and sorted alphabetically. -
#to_hash ⇒ Hash
The declared attributes and their current values, omitting unset (nil) ones.
Constructor Details
#initialize(metadata = {}) ⇒ ProfileMetadata
Returns a new instance of ProfileMetadata.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/inferno/dsl/profile_metadata.rb', line 66 def initialize( = {}) .each do |key, value| key = key.to_sym raise "Unknown #{self.class} attribute: #{key}" unless self.class.attribute_names.include?(key) public_send(:"#{key}=", value) end self.must_supports ||= {} end |
Class Method Details
.attribute(name) ⇒ void
This method returns an undefined value.
Declare an attribute that instances of this class (and subclasses) may be initialized with.
40 41 42 43 44 |
# File 'lib/inferno/dsl/profile_metadata.rb', line 40 def attribute(name) name = name.to_sym attribute_names << name unless attribute_names.include?(name) attr_accessor name end |
.attribute_names ⇒ Array<Symbol>
The full list of attribute names declared on this class and its ancestors.
33 34 35 |
# File 'lib/inferno/dsl/profile_metadata.rb', line 33 def attribute_names @attribute_names ||= superclass.respond_to?(:attribute_names) ? superclass.attribute_names.dup : [] end |
.from_file(path) ⇒ ProfileMetadata
Build an instance from a YAML file, eg one written by a metadata generator.
49 50 51 |
# File 'lib/inferno/dsl/profile_metadata.rb', line 49 def from_file(path) new(YAML.load_file(path, aliases: true)) end |
Instance Method Details
#must_support_strings ⇒ Array<String>
Every Must Support element, slice, and extension in must_supports, each represented as a
single string and sorted alphabetically. Unlike the list returned by a missing-elements
check, this includes everything that's expected to be supported, not just what's absent
from a particular set of resources.
Each item is represented the same way as in the list returned by a missing-elements check:
by its ElementDefinition id (including the names of any parent slices) without the leading
resource type (eg "category:us-core", "extension:us-core-race"), with any matched
fixed_value appended to elements (eg "code.coding.code = 45473-6"). See MustSupportStrings.
95 96 97 98 99 100 101 |
# File 'lib/inferno/dsl/profile_metadata.rb', line 95 def must_support_strings element_strings = Array.wrap(must_supports[:elements]).map { |el| MustSupportStrings.element_string(el) } slice_strings = Array.wrap(must_supports[:slices]).map { |slice| MustSupportStrings.slice_string(slice) } extension_strings = Array.wrap(must_supports[:extensions]).map { |ex| MustSupportStrings.extension_string(ex) } (element_strings + slice_strings + extension_strings).sort end |
#to_hash ⇒ Hash
Returns the declared attributes and their current values, omitting unset (nil) ones.
78 79 80 81 82 83 |
# File 'lib/inferno/dsl/profile_metadata.rb', line 78 def to_hash self.class.attribute_names.each_with_object({}) do |name, hash| value = public_send(name) hash[name] = value unless value.nil? end end |