Sha256: 8ba8f5e4a2cd6b3550583243a5b7a101f3a90bf0fd711032f03bd6022f686d7a

Contents?: true

Size: 1020 Bytes

Versions: 70

Compression:

Stored size: 1020 Bytes

Contents

# frozen_string_literal: true

require 'set'
require 'tsort'

module LegacyFacter
  module Core
    class DirectedGraph < Hash
      include TSort

      def acyclic?
        cycles.empty?
      end

      def cycles
        cycles = []
        each_strongly_connected_component do |component|
          cycles << component if component.size > 1
        end
        cycles
      end

      alias tsort_each_node each_key

      def tsort_each_child(node)
        fetch(node, []).each do |child|
          yield child
        end
      end

      def tsort
        missing = Set.new(values.flatten) - Set.new(keys)

        unless missing.empty?
          raise MissingVertex, "Cannot sort elements; cannot depend on missing elements #{missing.to_a}"
        end

        super
      rescue TSort::Cyclic
        raise CycleError, "Cannot sort elements; found the following cycles: #{cycles.inspect}"
      end

      class CycleError < StandardError; end
      class MissingVertex < StandardError; end
    end
  end
end

Version data entries

70 entries across 70 versions & 1 rubygems

Version Path
facter-4.0.37 lib/facter/custom_facts/core/directed_graph.rb
facter-4.0.36 lib/facter/custom_facts/core/directed_graph.rb
facter-4.0.35 lib/facter/custom_facts/core/directed_graph.rb
facter-4.0.34 lib/facter/custom_facts/core/directed_graph.rb
facter-4.0.33 lib/facter/custom_facts/core/directed_graph.rb
facter-4.0.32 lib/facter/custom_facts/core/directed_graph.rb
facter-4.0.31 lib/facter/custom_facts/core/directed_graph.rb
facter-4.0.30 lib/facter/custom_facts/core/directed_graph.rb
facter-4.0.29 lib/custom_facts/core/directed_graph.rb
facter-4.0.28 lib/custom_facts/core/directed_graph.rb
facter-4.0.26 lib/custom_facts/core/directed_graph.rb
facter-4.0.25 lib/custom_facts/core/directed_graph.rb
facter-4.0.24 lib/custom_facts/core/directed_graph.rb
facter-4.0.23 lib/custom_facts/core/directed_graph.rb
facter-4.0.22 lib/custom_facts/core/directed_graph.rb
facter-4.0.21 lib/custom_facts/core/directed_graph.rb
facter-4.0.20 lib/custom_facts/core/directed_graph.rb
facter-4.0.19 lib/custom_facts/core/directed_graph.rb
facter-4.0.18 lib/custom_facts/core/directed_graph.rb
facter-4.0.17 lib/custom_facts/core/directed_graph.rb