module Librarian
  class Resolution
    attr_reader :dependencies, :manifests

    def initialize(dependencies, manifests)
      @dependencies, @manifests = dependencies, manifests
    end

    def correct?
      manifests && begin
        manifests_hash = Hash[manifests.map{|m| [m.name, m]}]
        deps_match = dependencies.all? do |dependency|
          manifest = manifests_hash[dependency.name]
          dependency.satisfied_by?(manifest)
        end
        mans_match = manifests.all? do |manifest|
          manifest.dependencies.all? do |dependency|
            manifest = manifests_hash[dependency.name]
            dependency.satisfied_by?(manifest)
          end
        end
        deps_match && mans_match
      end
    end
  end
end

module Librarian
  class Resolution
    attr_reader :dependencies, :manifests, :manifests_index

    def initialize(dependencies, manifests)
      @dependencies, @manifests = dependencies, manifests
      @manifests_index = build_manifests_index(manifests)
    end

    def correct?
      manifests && manifests_consistent_with_dependencies? && manifests_internally_consistent?
    end

  private

    def build_manifests_index(manifests)
      Hash[manifests.map{|m| [m.name, m]}] if manifests
    end

    def manifests_consistent_with_dependencies?
      dependencies.all? do |dependency|
        manifest = manifests_index[dependency.name]
        dependency.satisfied_by?(manifest)
      end
    end

    def manifests_internally_consistent?
      manifests.all? do |manifest|
        manifest.dependencies.all? do |dependency|
          manifest = manifests_index[dependency.name]
          dependency.satisfied_by?(manifest)
        end
      end
    end

  end
end

Split apart the piecies of the Resolution correctness check into their own methods with good names.
Split apart the piecies of the Resolution correctness check into their own methods with good names.
