Module: Lutaml::Uml::ModelHelpers
- Included in:
- Lutaml::UmlRepository::Exporters::Markdown::Formatting, Lutaml::UmlRepository::Exporters::MarkdownExporter, Lutaml::UmlRepository::StaticSite::DataTransformer, Lutaml::UmlRepository::StaticSite::Serializers::AttributeSerializer, Lutaml::UmlRepository::StaticSite::Serializers::ClassSerializer, Lutaml::UmlRepository::StaticSite::Serializers::InheritanceResolver, Lutaml::UmlRepository::StaticSite::Serializers::PackageSerializer, Lutaml::UmlRepository::StaticSite::Serializers::PackageTreeBuilder
- Defined in:
- lib/lutaml/uml/model_helpers.rb
Overview
Shared helper methods for UML model objects. These methods are used across transformers, serializers, and presenters to avoid duplication of common model traversal and formatting logic.
Instance Method Summary collapse
-
#class_type_for(uml_class) ⇒ String
Extract the leaf class name from a fully-qualified class name or class object.
-
#format_cardinality(cardinality) ⇒ String?
Format a cardinality object as a “min..max” string.
-
#normalize_stereotypes(stereotype) ⇒ Array<String>
Normalize a stereotype value to a consistent Array format.
-
#package_path_for(uml_element) ⇒ String
Build a package-only namespace path (no class names).
-
#parse_cardinality(min, max) ⇒ Hash{Symbol => String, nil}
Parse a cardinality string or hash into a max: hash.
-
#qualified_name_for(uml_element) ⇒ String
Build a fully qualified name by walking the namespace chain of a UML element.
Instance Method Details
#class_type_for(uml_class) ⇒ String
Extract the leaf class name from a fully-qualified class name or class object.
91 92 93 94 95 96 97 |
# File 'lib/lutaml/uml/model_helpers.rb', line 91 def class_type_for(uml_class) case uml_class when String then uml_class.split("::").last when Class then uml_class.name.split("::").last else uml_class.class.name.split("::").last end end |
#format_cardinality(cardinality) ⇒ String?
Format a cardinality object as a “min..max” string.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/lutaml/uml/model_helpers.rb', line 107 def format_cardinality(cardinality) return nil unless cardinality min = cardinality.respond_to?(:min) ? cardinality.min : cardinality[:min] max = cardinality.respond_to?(:max) ? cardinality.max : cardinality[:max] return nil if min.nil? && max.nil? min_str = min.nil? ? "0" : min.to_s max_str = max.nil? ? "*" : max.to_s "#{min_str}..#{max_str}" end |
#normalize_stereotypes(stereotype) ⇒ Array<String>
Normalize a stereotype value to a consistent Array format.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/lutaml/uml/model_helpers.rb', line 19 def normalize_stereotypes(stereotype) return [] if stereotype.nil? case stereotype when Array then stereotype.map(&:to_s) when String then [stereotype] when Symbol then [stereotype.to_s] else [stereotype.to_s] end end |
#package_path_for(uml_element) ⇒ String
Build a package-only namespace path (no class names).
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lutaml/uml/model_helpers.rb', line 62 def package_path_for(uml_element) return uml_element.name unless uml_element.respond_to?(:namespace) parts = [] current = uml_element while current if current.is_a?(Lutaml::Uml::Package) parts.unshift(current.name) if current.name current = current.namespace if current.respond_to?(:namespace) elsif current.is_a?(Lutaml::Uml::TopElement) # Stop at the first TopElement (class, enum, etc.) — namespace above is package current = current.namespace if current.respond_to?(:namespace) else break end end parts.join("::") end |
#parse_cardinality(min, max) ⇒ Hash{Symbol => String, nil}
Parse a cardinality string or hash into a max: hash.
124 125 126 |
# File 'lib/lutaml/uml/model_helpers.rb', line 124 def parse_cardinality(min, max) {min: min, max: max} end |
#qualified_name_for(uml_element) ⇒ String
Build a fully qualified name by walking the namespace chain of a UML element.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/lutaml/uml/model_helpers.rb', line 37 def qualified_name_for(uml_element) return uml_element.name unless uml_element.respond_to?(:namespace) parts = [] current = uml_element while current parts.unshift(current.name) if current.name current = if current.respond_to?(:namespace) && current.namespace current.namespace else break end end parts.join("::") end |