Module: Lutaml::Uml::ModelHelpers

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

Instance Method Details

#class_type_for(uml_class) ⇒ String

Extract the leaf class name from a fully-qualified class name or class object.

Examples:

class_type_for("Lutaml::Uml::Class")    # => "Class"
class_type_for(some_enum_object)          # => "Enumeration"

Parameters:

  • uml_class (String, Class)

    A class name string or a Lutaml::Uml::* class instance

Returns:

  • (String)

    The leaf class name



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.

Examples:

format_cardinality(Lutaml::Uml::Cardinality.new(min: 0, max: 5))  # => "0..5"
format_cardinality({min: 1, max: nil})                           # => "1..*"

Parameters:

Returns:

  • (String, nil)

    Formatted cardinality string or nil



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.

Examples:

normalize_stereotypes(nil)                    # => []
normalize_stereotypes("enumeration")        # => ["enumeration"]
normalize_stereotypes(["a", "b"])           # => ["a", "b"]
normalize_stereotypes(:enumeration)         # => ["enumeration"]

Parameters:

  • stereotype (String, Array, nil, Symbol)

    The stereotype value

Returns:

  • (Array<String>)

    Array of stereotype strings



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).

Examples:

package_path_for(class_in_nested_package)  # => "ModelName::ParentPackage::ChildPackage"

Parameters:

Returns:

  • (String)

    Package path joined by ‘::’



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.

Parameters:

  • min (String, nil)
  • max (String, nil)

Returns:

  • (Hash{Symbol => String, nil})

    Hash with :min and :max keys



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.

Examples:

qualified_name_for(class_in_package)  # => "ModelName::PackageName::ClassName"

Parameters:

Returns:

  • (String)

    Fully qualified name joined by ‘::’



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