Class: Ea::Mdg::StereotypeAliasRegistry
- Inherits:
-
Object
- Object
- Ea::Mdg::StereotypeAliasRegistry
- 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
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
Class Method Summary collapse
-
.from_nokogiri(doc) ⇒ StereotypeAliasRegistry
Build from a parsed Nokogiri document.
-
.from_path(path) ⇒ StereotypeAliasRegistry
Build a registry by parsing the GMLStereotypes.xml format.
Instance Method Summary collapse
-
#canonical_names ⇒ Array<String>
All canonical stereotype names.
-
#canonicalize(name) ⇒ String?
Map a variant spelling to the canonical form.
-
#initialize(aliases = {}) ⇒ StereotypeAliasRegistry
constructor
A new instance of StereotypeAliasRegistry.
Constructor Details
#initialize(aliases = {}) ⇒ StereotypeAliasRegistry
Returns a new instance of StereotypeAliasRegistry.
25 26 27 |
# File 'lib/ea/mdg/stereotype_alias_registry.rb', line 25 def initialize(aliases = {}) @aliases = aliases end |
Instance Attribute Details
#aliases ⇒ Object (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.
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.
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_names ⇒ Array<String>
Returns 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.
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 |