Class: AIPP::THash

Inherits:
Hash
  • Object
show all
Includes:
TSort
Defined in:
lib/aipp/t_hash.rb

Overview

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]

Instance Method Summary collapse

Methods inherited from Hash

#metch, #to_remarks

Instance Method Details

#tsort(node = nil) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/aipp/t_hash.rb', line 25

def tsort(node=nil)
  if node
    subhash = subhash_for node
    super().select { subhash.include? _1 }
  else
    super()
  end
end

#tsort_each_child(node) ⇒ Object



21
22
23
# File 'lib/aipp/t_hash.rb', line 21

def tsort_each_child(node, &)
  fetch(node).each(&)
end