Class: Dentaku::DependencyResolver

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/dentaku/dependency_resolver.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars_to_dependencies_hash) ⇒ DependencyResolver

Returns a new instance of DependencyResolver.



11
12
13
14
15
# File 'lib/dentaku/dependency_resolver.rb', line 11

def initialize(vars_to_dependencies_hash)
  @key_mapping = Hash[vars_to_dependencies_hash.keys.map { |k| [k.downcase, k] }]
  # ensure variables are normalized strings
  @vars_to_deps = Hash[vars_to_dependencies_hash.map { |k, v| [k.downcase.to_s, v] }]
end

Class Method Details

.find_resolve_order(vars_to_dependencies_hash, case_sensitive = false) ⇒ Object



7
8
9
# File 'lib/dentaku/dependency_resolver.rb', line 7

def self.find_resolve_order(vars_to_dependencies_hash, case_sensitive = false)
  self.new(vars_to_dependencies_hash).sort
end

Instance Method Details

#sortObject



17
18
19
# File 'lib/dentaku/dependency_resolver.rb', line 17

def sort
  tsort.map { |k| @key_mapping.fetch(k, k) }
end

#tsort_each_child(node, &block) ⇒ Object



25
26
27
# File 'lib/dentaku/dependency_resolver.rb', line 25

def tsort_each_child(node, &block)
  @vars_to_deps.fetch(node.to_s, []).each(&block)
end

#tsort_each_node(&block) ⇒ Object



21
22
23
# File 'lib/dentaku/dependency_resolver.rb', line 21

def tsort_each_node(&block)
  @vars_to_deps.each_key(&block)
end