Class: Lutaml::Cli::ElementIdentifier
- Inherits:
-
Object
- Object
- Lutaml::Cli::ElementIdentifier
- Defined in:
- lib/lutaml/cli/element_identifier.rb
Overview
ElementIdentifier parses and manages element references
Provides a unified syntax for referring to UML elements:
-
“package:ModelRoot::Core”
-
“class:ModelRoot::Core::Building”
-
“diagram:ClassDiagram1”
-
“attribute:ModelRoot::Core::Building::name”
Supports auto-detection when type prefix is omitted.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.detect_type(identifier) ⇒ Symbol
Detect element type from identifier pattern.
-
.has_type_prefix?(identifier) ⇒ Boolean
Check if this identifier has an explicit type prefix.
-
.parse(identifier) ⇒ ElementIdentifier
Parse an element identifier string.
Instance Method Summary collapse
-
#initialize(type, path) ⇒ ElementIdentifier
constructor
Initialize an identifier.
-
#parent_path ⇒ String?
Get the parent path (everything before last ::).
-
#qualified? ⇒ Boolean
Check if identifier is a qualified name (contains ::).
-
#simple_name ⇒ String
Get the simple name (last component after ::).
-
#to_h ⇒ Hash
Convert to hash representation.
-
#to_s ⇒ String
Get a human-readable string representation.
Constructor Details
#initialize(type, path) ⇒ ElementIdentifier
Initialize an identifier
62 63 64 65 |
# File 'lib/lutaml/cli/element_identifier.rb', line 62 def initialize(type, path) @type = type @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
25 26 27 |
# File 'lib/lutaml/cli/element_identifier.rb', line 25 def path @path end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
25 26 27 |
# File 'lib/lutaml/cli/element_identifier.rb', line 25 def type @type end |
Class Method Details
.detect_type(identifier) ⇒ Symbol
Detect element type from identifier pattern
Uses heuristics to determine type:
- Multiple
-
separators → likely a class
-
Starts with uppercase → likely a class or package
-
Contains “Diagram” → likely a diagram
-
Otherwise → package (safest default)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/lutaml/cli/element_identifier.rb', line 94 def self.detect_type(identifier) # rubocop:disable Metrics/MethodLength # Check for diagram patterns return :diagram if identifier.match?(/diagram/i) # Count package separators separator_count = identifier.scan("::").size case separator_count when 0 # No separators - single name # Could be package, class, or diagram if identifier.match?(/^[A-Z]/) # Starts with uppercase - likely a class or package name # Default to package as it's more general :package else # Lowercase start - could be attribute or diagram name :diagram end when 1..Float::INFINITY # One or more separators - likely qualified class name # e.g., Package::Class or ModelRoot::Package::Class :class else # Default :package end end |
.has_type_prefix?(identifier) ⇒ Boolean
Check if this identifier has an explicit type prefix
78 79 80 81 82 |
# File 'lib/lutaml/cli/element_identifier.rb', line 78 def self.has_type_prefix?(identifier) identifier.include?(":") && ResourceRegistry.type_registered?(identifier.split(":", 2).first.to_sym) end |
.parse(identifier) ⇒ ElementIdentifier
Parse an element identifier string
or just “path” registered
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/lutaml/cli/element_identifier.rb', line 34 def self.parse(identifier) # rubocop:disable Metrics/MethodLength if identifier.nil? || identifier.empty? raise ArgumentError, "Identifier cannot be nil or empty" end if identifier.include?(":") type, path = identifier.split(":", 2) type_sym = type.to_sym unless ResourceRegistry.type_registered?(type_sym) raise ArgumentError, "Unknown element type: #{type}. " \ "Valid types: " \ "#{ResourceRegistry.types.join(', ')}" end new(type_sym, path) else # Auto-detect type from pattern detected_type = detect_type(identifier) new(detected_type, identifier) end end |
Instance Method Details
#parent_path ⇒ String?
Get the parent path (everything before last ::)
140 141 142 143 144 145 |
# File 'lib/lutaml/cli/element_identifier.rb', line 140 def parent_path return nil unless qualified? parts = @path.split("::") parts[0...-1].join("::") end |
#qualified? ⇒ Boolean
Check if identifier is a qualified name (contains ::)
126 127 128 |
# File 'lib/lutaml/cli/element_identifier.rb', line 126 def qualified? @path.include?("::") end |
#simple_name ⇒ String
Get the simple name (last component after ::)
133 134 135 |
# File 'lib/lutaml/cli/element_identifier.rb', line 133 def simple_name qualified? ? @path.split("::").last : @path end |
#to_h ⇒ Hash
Convert to hash representation
150 151 152 153 154 155 156 157 |
# File 'lib/lutaml/cli/element_identifier.rb', line 150 def to_h { type: @type, path: @path, simple_name: simple_name, qualified: qualified?, } end |
#to_s ⇒ String
Get a human-readable string representation
70 71 72 |
# File 'lib/lutaml/cli/element_identifier.rb', line 70 def to_s "#{type}:#{path}" end |