Class: Ea::Mdg::StereotypeAliasRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/ea/mdg/stereotype_alias_registry.rb

Overview

Stereotype alias resolver. Loads EA's GMLStereotypes.xml (or equivalent) and maps variant spellings to a canonical name.

Source XML format:

<Stereotypes>
<Stereotype name="FeatureType">
  <Alias>Feature Type</Alias>
  <Alias>featureType</Alias>
  <Alias>featuretype</Alias>
</Stereotype>
</Stereotypes>

Usage:

registry = StereotypeAliasRegistry.from_path("GMLStereotypes.xml")
registry.canonicalize("featuretype")  # → "FeatureType"
registry.canonicalize("unknown")      # → "unknown" (passthrough)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aliases = {}) ⇒ StereotypeAliasRegistry

Returns a new instance of StereotypeAliasRegistry.

Parameters:

  • aliases (Hash{String => String}) (defaults to: {})

    alias → canonical name



25
26
27
# File 'lib/ea/mdg/stereotype_alias_registry.rb', line 25

def initialize(aliases = {})
  @aliases = aliases
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



22
23
24
# File 'lib/ea/mdg/stereotype_alias_registry.rb', line 22

def aliases
  @aliases
end

Class Method Details

.from_nokogiri(doc) ⇒ StereotypeAliasRegistry

Build from a parsed Nokogiri document. Tested separately so the parser path is exercised without file IO.

Parameters:

  • doc (Nokogiri::XML::Document)

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ea/mdg/stereotype_alias_registry.rb', line 44

def self.from_nokogiri(doc)
  aliases = {}
  doc.xpath("//Stereotype").each do |stereo|
    canonical = stereo["name"]
    next unless canonical

    aliases[canonical.downcase] = canonical
    stereo.xpath("Alias").each do |alias_node|
      text = alias_node.text.strip
      next if text.empty?

      aliases[text.downcase] = canonical
    end
  end
  new(aliases)
end

.from_path(path) ⇒ StereotypeAliasRegistry

Build a registry by parsing the GMLStereotypes.xml format.

Parameters:

  • path (String)

    path to XML file

Returns:



32
33
34
35
36
37
38
# File 'lib/ea/mdg/stereotype_alias_registry.rb', line 32

def self.from_path(path)
  return new if path.nil? || !File.exist?(path)

  require "nokogiri"
  doc = Nokogiri::XML(File.read(path))
  from_nokogiri(doc)
end

Instance Method Details

#canonical_namesArray<String>

Returns all canonical stereotype names.

Returns:

  • (Array<String>)

    all canonical stereotype names



72
73
74
# File 'lib/ea/mdg/stereotype_alias_registry.rb', line 72

def canonical_names
  aliases.values.uniq
end

#canonicalize(name) ⇒ String?

Map a variant spelling to the canonical form. Case-insensitive. Unknown stereotypes pass through unchanged.

Parameters:

  • name (String, nil)

Returns:

  • (String, nil)


65
66
67
68
69
# File 'lib/ea/mdg/stereotype_alias_registry.rb', line 65

def canonicalize(name)
  return nil if name.nil? || name.empty?

  aliases[name.downcase] || name
end