Class: WritersRoom::CrossReference
- Inherits:
-
Object
- Object
- WritersRoom::CrossReference
- Defined in:
- lib/writers_room/cross_reference.rb
Overview
Scans element bodies for name/alias references and builds a reference graph.
Instance Attribute Summary collapse
-
#project_path ⇒ Object
readonly
Returns the value of attribute project_path.
-
#references ⇒ Object
readonly
Returns the value of attribute references.
Instance Method Summary collapse
- #graph ⇒ Object
-
#initialize(project_path) ⇒ CrossReference
constructor
A new instance of CrossReference.
- #referenced_by(slug) ⇒ Object
- #references_for(slug) ⇒ Object
- #scan ⇒ Object
Constructor Details
#initialize(project_path) ⇒ CrossReference
Returns a new instance of CrossReference.
10 11 12 13 |
# File 'lib/writers_room/cross_reference.rb', line 10 def initialize(project_path) @project_path = File.(project_path) @references = {} # slug => [referenced_slugs] end |
Instance Attribute Details
#project_path ⇒ Object (readonly)
Returns the value of attribute project_path.
8 9 10 |
# File 'lib/writers_room/cross_reference.rb', line 8 def project_path @project_path end |
#references ⇒ Object (readonly)
Returns the value of attribute references.
8 9 10 |
# File 'lib/writers_room/cross_reference.rb', line 8 def references @references end |
Instance Method Details
#graph ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/writers_room/cross_reference.rb', line 36 def graph nodes = Set.new edges = [] @references.each do |from, to_list| nodes << from to_list.each do |to| nodes << to edges << { from: from, to: to } end end { nodes: nodes.to_a, edges: edges } end |
#referenced_by(slug) ⇒ Object
32 33 34 |
# File 'lib/writers_room/cross_reference.rb', line 32 def referenced_by(slug) @references.select { |_, refs| refs.include?(slug) }.keys end |
#references_for(slug) ⇒ Object
28 29 30 |
# File 'lib/writers_room/cross_reference.rb', line 28 def references_for(slug) @references[slug] || [] end |
#scan ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/writers_room/cross_reference.rb', line 15 def scan @references = {} all_elements = load_all_elements name_map = build_name_map(all_elements) all_elements.each do |element| refs = find_references(element.body, name_map, exclude: element.slug) @references[element.slug] = refs unless refs.empty? end @references end |