Sha256: c55758db6313146167c20e357d818c2fdab39fbf85a38e3a04b4ba8148b880bd

Contents?: true

Size: 1.57 KB

Versions: 15

Compression:

Stored size: 1.57 KB

Contents

require 'tsort'

module TwitterCldr
  module Resources
    class ImportResolver
      include TSort

      attr_reader :importers

      def initialize(importers = Resources.importer_classes_for_ruby_engine)
        @importers = importers
      end

      def import
        check_unmet_deps
        import_in_order
      end

      private

      def import_in_order
        tsort.each { |instance| instance.import }
      end

      def tsort_each_node(&block)
        instances.each(&block)
      end

      def tsort_each_child(instance, &block)
        deps_for(instance).map do |dep_class|
          yield instances.find { |ins| ins.class == dep_class }
        end
      end

      def check_unmet_deps
        instances.each do |instance|
          check_unmet_instance_deps(instance)
        end
      end

      def check_unmet_instance_deps(instance)
        unmet_deps = unmet_deps_for(instance)

        unless unmet_deps.empty?
          list = unmet_deps.map { |d| d.name }.join(', ')
          raise "#{instance.class} dependencies are not met: #{list}"
        end
      end

      def instances
        @instances ||= importers.map do |importer|
          importer.is_a?(Class) ? importer.new : importer
        end
      end

      def unmet_deps_for(instance)
        deps_for(instance).reject do |dep_class|
          instances.any? { |ins| ins.class == dep_class }
        end
      end

      def deps_for(instance)
        if dep_requirement = instance.requirements[:dependency]
          dep_requirement.importer_classes
        else
          []
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/twitter_cldr-4.4.5/lib/twitter_cldr/resources/import_resolver.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/twitter_cldr-4.4.5/lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-5.2.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-5.1.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-5.0.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-4.4.5 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-4.4.4 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-4.4.3 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-4.4.2 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-4.4.1 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-4.4.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-4.3.1 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-4.3.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-4.2.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-4.1.0 lib/twitter_cldr/resources/import_resolver.rb