Class: Riml::ClassDependencyGraph
- Inherits:
-
Object
- Object
- Riml::ClassDependencyGraph
- Includes:
- TSort
- Defined in:
- lib/riml/class_dependency_graph.rb
Overview
Used for reordering ‘riml_include`s based on class dependencies.
Instance Attribute Summary collapse
-
#definition_graph ⇒ Object
readonly
Returns the value of attribute definition_graph.
-
#encountered_graph ⇒ Object
readonly
Returns the value of attribute encountered_graph.
Instance Method Summary collapse
- #class_defined(filename, class_name, superclass_name) ⇒ Object
- #class_encountered(filename, class_name) ⇒ Object
-
#initialize ⇒ ClassDependencyGraph
constructor
definition_graph: { “faster_car.riml” => { “s:FasterCar” => “s:Car” }, “car.riml” => { “s:Car” => nil } } encountered_graph: { “faster_car.riml” => [“s:FasterCar”, “s:Car”], “car.riml” => [“s:Car”] }.
-
#prepare_filename_graph! ⇒ Object
Computes ‘@filename_graph` from `@encountered_graph` and `@definition_graph`.
-
#tsort ⇒ Object
(also: #filename_order)
order in which filenames need to be included based off internal ‘@definition_graph` and `@encountered_graph`.
- #tsort_each_child(node, &block) ⇒ Object
- #tsort_each_node(&block) ⇒ Object
Constructor Details
#initialize ⇒ ClassDependencyGraph
definition_graph: { “faster_car.riml” => { “s:FasterCar” => “s:Car” }, “car.riml” => { “s:Car” => nil } } encountered_graph: { “faster_car.riml” => [“s:FasterCar”, “s:Car”], “car.riml” => [“s:Car”] }
13 14 15 16 17 |
# File 'lib/riml/class_dependency_graph.rb', line 13 def initialize @definition_graph = {} @encountered_graph = {} @filename_graph = nil end |
Instance Attribute Details
#definition_graph ⇒ Object (readonly)
Returns the value of attribute definition_graph.
9 10 11 |
# File 'lib/riml/class_dependency_graph.rb', line 9 def definition_graph @definition_graph end |
#encountered_graph ⇒ Object (readonly)
Returns the value of attribute encountered_graph.
9 10 11 |
# File 'lib/riml/class_dependency_graph.rb', line 9 def encountered_graph @encountered_graph end |
Instance Method Details
#class_defined(filename, class_name, superclass_name) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/riml/class_dependency_graph.rb', line 19 def class_defined(filename, class_name, superclass_name) @definition_graph[filename] ||= {} @definition_graph[filename][class_name] = superclass_name class_encountered(filename, class_name) class_encountered(filename, superclass_name) if superclass_name end |
#class_encountered(filename, class_name) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/riml/class_dependency_graph.rb', line 26 def class_encountered(filename, class_name) @encountered_graph[filename] ||= [] unless @encountered_graph[filename].include?(class_name) @encountered_graph[filename] << class_name end end |
#prepare_filename_graph! ⇒ Object
Computes ‘@filename_graph` from `@encountered_graph` and `@definition_graph`. This graph is used by `tsort` to sort the filenames for inclusion.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/riml/class_dependency_graph.rb', line 45 def prepare_filename_graph! @filename_graph = {} @encountered_graph.each do |filename, encountered_classes| dependent_class_names = if @definition_graph[filename].nil? encountered_classes else class_names_defined_in_file = @definition_graph[filename].keys # all superclass names that this file depends on class_names_dependent_by_superclass = @definition_graph[filename].values.compact - class_names_defined_in_file class_names_dependent_by_use = encountered_classes - class_names_defined_in_file class_names_dependent_by_superclass + class_names_dependent_by_use end dependent_class_names.each do |dep| dependent_definition_fname = @definition_graph.detect { |fname, hash| hash.has_key?(dep) }.first rescue nil if dependent_definition_fname @filename_graph[filename] ||= [] unless @filename_graph[filename].include?(dependent_definition_fname) @filename_graph[filename] << dependent_definition_fname end end end end end |
#tsort ⇒ Object Also known as: filename_order
order in which filenames need to be included based off internal ‘@definition_graph` and `@encountered_graph`
36 37 38 39 |
# File 'lib/riml/class_dependency_graph.rb', line 36 def tsort prepare_filename_graph! if @filename_graph.nil? super end |
#tsort_each_child(node, &block) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/riml/class_dependency_graph.rb', line 74 def tsort_each_child(node, &block) if @filename_graph[node] @filename_graph[node].each(&block) else [] end end |
#tsort_each_node(&block) ⇒ Object
70 71 72 |
# File 'lib/riml/class_dependency_graph.rb', line 70 def tsort_each_node(&block) @filename_graph.each_key(&block) end |