class AIPP::THash

Topologically sortable hash for dealing with dependencies

Example:

dependency_hash = THash[
  dns: %i(net),
  webserver: %i(dns logger),
  net: [],
  logger: []
]
# Sort to resolve dependencies of the entire hash
dependency_hash.tsort         # => [:net, :dns, :logger, :webserver]
# Sort to resolve dependencies of one node only
dependency_hash.tsort(:dns)   # => [:net, :dns]

Public Instance Methods

tsort(node=nil) click to toggle source
Calls superclass method
   # File lib/aipp/t_hash.rb
25 def tsort(node=nil)
26   if node
27     subhash = subhash_for node
28     super().select { subhash.include? _1 }
29   else
30     super()
31   end
32 end
tsort_each_child(node, &block) click to toggle source
   # File lib/aipp/t_hash.rb
21 def tsort_each_child(node, &block)
22   fetch(node).each(&block)
23 end

Private Instance Methods

subhash_for(node, memo=[]) click to toggle source
   # File lib/aipp/t_hash.rb
36 def subhash_for(node, memo=[])
37   memo.tap do |m|
38     fail TSort::Cyclic if m.include? node
39     m << node
40     fetch(node).each { subhash_for(_1, m) }
41   end
42 end