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



240
241
242
243
# File 'lib/spoom/model/model.rb', line 240

def initialize
  @symbols = {} #: Hash[String, Symbol]
  @symbols_hierarchy = Poset.new #: Poset[Symbol]
end

Instance Attribute Details

#symbolsObject (readonly)

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



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

def symbols
  @symbols
end

#symbols_hierarchyObject (readonly)

: Poset



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

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:



249
250
251
252
253
254
# File 'lib/spoom/model/model.rb', line 249

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

  symbol
end

#finalize!Object

: -> void



298
299
300
# File 'lib/spoom/model/model.rb', line 298

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



260
261
262
# File 'lib/spoom/model/model.rb', line 260

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



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/spoom/model/model.rb', line 265

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 = @symbols[full_name] #: 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



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

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

#supertypes(symbol) ⇒ Object

: (Symbol symbol) -> Array



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

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