Sha256: 1ab702360d1d2bfe231a299b50ccf934bcf570dcbba6e0e8adae004e16b5927f

Contents?: true

Size: 1.83 KB

Versions: 25

Compression:

Stored size: 1.83 KB

Contents

require 'tsort'

module TwitterCldr
  module Resources
    class ImportResolver
      include TSort

      attr_reader :importers, :options

      def initialize(importers = Resources.importer_classes_for_ruby_engine, options = {})
        @importers = importers
        @options = options
      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|
          dep = instances.find { |ins| ins.class == dep_class }
          yield dep if dep

          unless options[:allow_missing_dependencies]
            raise "Could not find dependency #{dep_class.name}"
          end
        end
      end

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

      def check_unmet_instance_deps(instance)
        return if options[:allow_missing_dependencies]

        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

25 entries across 25 versions & 1 rubygems

Version Path
twitter_cldr-6.12.1 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.12.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.11.5 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.11.4 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.11.3 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.11.2 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.11.1 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.11.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.10.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.9.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.8.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.7.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.6.2 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.6.1 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.6.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.5.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.4.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.3.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.2.0 lib/twitter_cldr/resources/import_resolver.rb
twitter_cldr-6.1.0 lib/twitter_cldr/resources/import_resolver.rb