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



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

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]



221
222
223
# File 'lib/spoom/model/model.rb', line 221

def symbols
  @symbols
end

#symbols_hierarchyObject (readonly)

: Poset



224
225
226
# File 'lib/spoom/model/model.rb', line 224

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:



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

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

  symbol
end

#finalize!Object

: -> void



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

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



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

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



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/spoom/model/model.rb', line 252

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



279
280
281
282
# File 'lib/spoom/model/model.rb', line 279

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

#supertypes(symbol) ⇒ Object

: (Symbol symbol) -> Array



273
274
275
276
# File 'lib/spoom/model/model.rb', line 273

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