class JsDuck::Relations

Provides information about relations between classes.

Also provides a place to look up classes by name.

The constructor is initialized with array of all available classes and list of class names to ignore. By default the latter list contains only JavaScript base class “Object”.

Attributes

classes[R]

Returns list of all classes

Public Class Methods

new(classes = [], ignorables = []) click to toggle source
# File lib/jsduck/relations.rb, line 16
def initialize(classes = [], ignorables = [])
  @classes = classes
  @external_classes = ExternalClasses.new(ignorables)

  # First build class lookup table; building lookup tables for
  # mixins and subclasses will depend on that.
  @lookup = {}
  @classes.each do |cls|
    @lookup[cls[:name]] = cls
    (cls[:alternateClassNames] || []).each do |alt_name|
      @lookup[alt_name] = cls
    end
    cls.relations = self
  end

  @subs = {}
  @mixes = {}
  @classes.each do |cls|
    reg_subclasses(cls)
    reg_mixed_into(cls)
  end
end

Public Instance Methods

[](classname) click to toggle source

Looks up class by name, nil if not found

# File lib/jsduck/relations.rb, line 40
def [](classname)
  @lookup[classname]
end
each(&block) click to toggle source
# File lib/jsduck/relations.rb, line 49
def each(&block)
  @classes.each(&block)
end
ignore?(classname) click to toggle source

Returns true if class is in list of ignored classes.

# File lib/jsduck/relations.rb, line 45
def ignore?(classname)
  @external_classes.is?(classname)
end
length() click to toggle source

Used in tests to check the nr of classes.

# File lib/jsduck/relations.rb, line 54
def length
  @classes.length
end
mixed_into(cls) click to toggle source

Returns classes having particular mixin, empty array if none

# File lib/jsduck/relations.rb, line 90
def mixed_into(cls)
  @mixes[cls[:name]] || []
end
reg_mixed_into(cls) click to toggle source
# File lib/jsduck/relations.rb, line 79
def reg_mixed_into(cls)
  cls.mixins.each do |mix|
    if @mixes[mix[:name]]
      @mixes[mix[:name]] << cls
    else
      @mixes[mix[:name]] = [cls]
    end
  end
end
reg_subclasses(cls) click to toggle source
# File lib/jsduck/relations.rb, line 64
def reg_subclasses(cls)
  if !cls.parent
    # do nothing
  elsif @subs[cls.parent[:name]]
    @subs[cls.parent[:name]] << cls
  else
    @subs[cls.parent[:name]] = [cls]
  end
end
subclasses(cls) click to toggle source

Returns subclasses of particular class, empty array if none

# File lib/jsduck/relations.rb, line 75
def subclasses(cls)
  @subs[cls[:name]] || []
end
to_a() click to toggle source

Returns list of all classes. This method allows us to treat Relations as array and therefore easily mock it

# File lib/jsduck/relations.rb, line 60
def to_a
  @classes
end