Class: JsDuck::Relations
- Inherits:
-
Object
- Object
- JsDuck::Relations
- Defined in:
- lib/jsduck/relations.rb
Overview
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”.
Instance Attribute Summary collapse
-
#classes ⇒ Object
readonly
Returns list of all classes.
Instance Method Summary collapse
-
#[](classname) ⇒ Object
Looks up class by name, nil if not found.
- #each(&block) ⇒ Object
-
#ignore?(classname) ⇒ Boolean
Returns true if class is in list of ignored classes.
-
#initialize(classes = [], ignorables = []) ⇒ Relations
constructor
A new instance of Relations.
-
#length ⇒ Object
Used in tests to check the nr of classes.
-
#mixed_into(cls) ⇒ Object
Returns classes having particular mixin, empty array if none.
- #reg_mixed_into(cls) ⇒ Object
- #reg_subclasses(cls) ⇒ Object
-
#subclasses(cls) ⇒ Object
Returns subclasses of particular class, empty array if none.
-
#to_a ⇒ Object
Returns list of all classes.
Constructor Details
#initialize(classes = [], ignorables = []) ⇒ Relations
Returns a new instance of Relations.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 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 |
Instance Attribute Details
#classes ⇒ Object (readonly)
Returns list of all classes
14 15 16 |
# File 'lib/jsduck/relations.rb', line 14 def classes @classes end |
Instance Method Details
#[](classname) ⇒ Object
Looks up class by name, nil if not found
40 41 42 |
# File 'lib/jsduck/relations.rb', line 40 def [](classname) @lookup[classname] end |
#each(&block) ⇒ Object
49 50 51 |
# File 'lib/jsduck/relations.rb', line 49 def each(&block) @classes.each(&block) end |
#ignore?(classname) ⇒ Boolean
Returns true if class is in list of ignored classes.
45 46 47 |
# File 'lib/jsduck/relations.rb', line 45 def ignore?(classname) @external_classes.is?(classname) end |
#length ⇒ Object
Used in tests to check the nr of classes.
54 55 56 |
# File 'lib/jsduck/relations.rb', line 54 def length @classes.length end |
#mixed_into(cls) ⇒ Object
Returns classes having particular mixin, empty array if none
90 91 92 |
# File 'lib/jsduck/relations.rb', line 90 def mixed_into(cls) @mixes[cls[:name]] || [] end |
#reg_mixed_into(cls) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# 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) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# 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) ⇒ Object
Returns subclasses of particular class, empty array if none
75 76 77 |
# File 'lib/jsduck/relations.rb', line 75 def subclasses(cls) @subs[cls[:name]] || [] end |
#to_a ⇒ Object
Returns list of all classes. This method allows us to treat Relations as array and therefore easily mock it
60 61 62 |
# File 'lib/jsduck/relations.rb', line 60 def to_a @classes end |