Class: Spoom::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/spoom/model/model.rb,
lib/spoom/model/builder.rb,
lib/spoom/model/reference.rb,
lib/spoom/model/namespace_visitor.rb,
lib/spoom/model/references_visitor.rb

Defined Under Namespace

Classes: Attr, AttrAccessor, AttrReader, AttrWriter, Builder, Class, Comment, Constant, Error, Extend, Include, Method, Mixin, Module, Namespace, NamespaceVisitor, Prepend, Property, Reference, ReferencesVisitor, Sig, SingletonClass, Symbol, SymbolDef, UnresolvedSymbol, Visibility

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModel

: -> void



235
236
237
238
# File 'lib/spoom/model/model.rb', line 235

def initialize
  @symbols = T.let({}, T::Hash[String, Symbol])
  @symbols_hierarchy = T.let(Poset[Symbol].new, Poset[Symbol])
end

Instance Attribute Details

#symbolsObject (readonly)

All the symbols registered in this model : Hash[String, Symbol]



229
230
231
# File 'lib/spoom/model/model.rb', line 229

def symbols
  @symbols
end

#symbols_hierarchyObject (readonly)

: Poset



232
233
234
# File 'lib/spoom/model/model.rb', line 232

def symbols_hierarchy
  @symbols_hierarchy
end

Instance Method Details

#[](full_name) ⇒ Object

Get a symbol by it’s full name

Raises an error if the symbol is not found : (String full_name) -> Symbol

Raises:



244
245
246
247
248
249
# File 'lib/spoom/model/model.rb', line 244

def [](full_name)
  symbol = @symbols[full_name]
  raise Error, "Symbol not found: #{full_name}" unless symbol

  symbol
end

#finalize!Object

: -> void



293
294
295
# File 'lib/spoom/model/model.rb', line 293

def finalize!
  compute_symbols_hierarchy!
end

#register_symbol(full_name) ⇒ Object

Register a new symbol by it’s full name

If the symbol already exists, it will be returned. : (String full_name) -> Symbol



255
256
257
# File 'lib/spoom/model/model.rb', line 255

def register_symbol(full_name)
  @symbols[full_name] ||= Symbol.new(full_name)
end

#resolve_symbol(full_name, context:) ⇒ Object

: (String full_name, context: Symbol) -> Symbol



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/spoom/model/model.rb', line 260

def resolve_symbol(full_name, context:)
  if full_name.start_with?("::")
    full_name = full_name.delete_prefix("::")
    return @symbols[full_name] ||= UnresolvedSymbol.new(full_name)
  end

  target = T.let(@symbols[full_name], T.nilable(Symbol))
  return target if target

  parts = context.full_name.split("::")
  until parts.empty?
    target = @symbols["#{parts.join("::")}::#{full_name}"]
    return target if target

    parts.pop
  end

  @symbols[full_name] = UnresolvedSymbol.new(full_name)
end

#subtypes(symbol) ⇒ Object

: (Symbol symbol) -> Array



287
288
289
290
# File 'lib/spoom/model/model.rb', line 287

def subtypes(symbol)
  poe = @symbols_hierarchy[symbol]
  poe.descendants
end

#supertypes(symbol) ⇒ Object

: (Symbol symbol) -> Array



281
282
283
284
# File 'lib/spoom/model/model.rb', line 281

def supertypes(symbol)
  poe = @symbols_hierarchy[symbol]
  poe.ancestors
end