Sha256: 6b86f30bc1af3e02843c3e7c724f5f444cd7317772335d75eb08fd51f4a71cb1

Contents?: true

Size: 1.79 KB

Versions: 14

Compression:

Stored size: 1.79 KB

Contents

# -*- encoding : utf-8 -*-
require 'tsort'

module IB
  class DependencyResolver

    class TSortHash < ::Hash
      include TSort

      alias tsort_each_node each_key

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

    module TSortable
      def to_node
        sub_class_dependencies_nodes = sub_class_dependencies.inject({}) do |sum, i|
          sum.merge!({ i => [] })
        end

        (super_class ? node_with_super_class : node_without_super_class).
          merge(sub_class_dependencies_nodes)
      end

      def node_with_super_class
        {
          sub_class   => sub_class_dependencies,
          super_class => [],
        }
      end

      def node_without_super_class
        { sub_class => sub_class_dependencies }
      end

      def merge!(a, b)
        b.each do |k, v|
          if a.key?(k)
            a[k].concat(v)
          else
            a[k] = v
          end
        end
        a
      end
      module_function :merge!
    end

    attr_reader :dependency_graph, :files

    def initialize(files)
      @files = files
      @dependency_graph = struct_class_dependency_graph
    end

    def sort_classes
      @dependency_graph.tsort
    end

    def sort_files
      sort_classes.map do |klass|
        files.select do |file, interfaces|
          interfaces.any? {|i| i.has_sub_class?(klass) }
        end.keys[0]
      end.uniq.compact
    end

    def sort
      sorted_files = {}
      sort_files.each do |file|
        sorted_files.store(file, @files[file])
      end
      sorted_files
    end

    private
    def struct_class_dependency_graph
      nodes = @files.values.flatten.map {|i| i.extend TSortable }.map(&:to_node)
      nodes.inject(TSortHash.new) do |sum, x|
        TSortable.merge!(sum, x)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
ib-1.0.1 lib/ib/dependency_resolver.rb
ib-1.0 lib/ib/dependency_resolver.rb
ib-0.8.0 lib/ib/dependency_resolver.rb
ib-0.7.2 lib/ib/dependency_resolver.rb
ib-0.7.1 lib/ib/dependency_resolver.rb
ib-0.7.0 lib/ib/dependency_resolver.rb
ib-0.6.0 lib/ib/dependency_resolver.rb
ib-0.5.0 lib/ib/dependency_resolver.rb
ib-0.4.9 lib/ib/dependency_resolver.rb
ib-0.4.8 lib/ib/dependency_resolver.rb
ib-0.4.7 lib/ib/dependency_resolver.rb
ib-0.4.6 lib/ib/dependency_resolver.rb
ib-0.4.5 lib/ib/dependency_resolver.rb
ib-0.4.4 lib/ib/dependency_resolver.rb