Class: Lutaml::Uml::InheritanceWalker
- Inherits:
-
Object
- Object
- Lutaml::Uml::InheritanceWalker
- Defined in:
- lib/lutaml/uml/inheritance_walker.rb
Overview
Service class for safely walking UML generalization chains.
Walks from a class up through its generalization hierarchy (parent classes), collecting information about each ancestor. Supports cycle detection to prevent infinite loops in malformed models.
Instance Method Summary collapse
-
#ancestors_of(klass) ⇒ Array<Lutaml::Uml::Class>
Get all ancestors of a class in order (immediate parent first).
-
#initialize(repository) ⇒ InheritanceWalker
constructor
A new instance of InheritanceWalker.
-
#supertype_of(klass) ⇒ Lutaml::Uml::Class?
Get the direct supertype (immediate parent) of a class.
-
#walk(klass) {|ancestor, level| ... } ⇒ Array<[klass, level]>
Walk the generalization chain from a starting class.
Constructor Details
#initialize(repository) ⇒ InheritanceWalker
Returns a new instance of InheritanceWalker.
18 19 20 21 |
# File 'lib/lutaml/uml/inheritance_walker.rb', line 18 def initialize(repository) @repository = repository @visited = Set.new end |
Instance Method Details
#ancestors_of(klass) ⇒ Array<Lutaml::Uml::Class>
Get all ancestors of a class in order (immediate parent first).
59 60 61 62 63 |
# File 'lib/lutaml/uml/inheritance_walker.rb', line 59 def ancestors_of(klass) result = [] walk(klass) { |ancestor, _level| result << ancestor } result end |
#supertype_of(klass) ⇒ Lutaml::Uml::Class?
Get the direct supertype (immediate parent) of a class.
51 52 53 |
# File 'lib/lutaml/uml/inheritance_walker.rb', line 51 def supertype_of(klass) @repository.supertype_of(klass) end |
#walk(klass) {|ancestor, level| ... } ⇒ Array<[klass, level]>
Walk the generalization chain from a starting class.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/lutaml/uml/inheritance_walker.rb', line 33 def walk(klass) return [] unless klass.respond_to?(:generalization) && klass.generalization ancestors = [] @visited.clear collect_ancestors(klass, ancestors) ancestors.reverse_each.with_index(1) do |ancestor, level| break if @visited.include?(ancestor.xmi_id) @visited.add(ancestor.xmi_id) yield(ancestor, level) if block_given? end ancestors.reverse_each.with_index(1) end |