lib/polariscope/scanner/gem_versions.rb in polariscope-0.3.0 vs lib/polariscope/scanner/gem_versions.rb in polariscope-0.4.0

- old
+ new

@@ -1,31 +1,44 @@ # frozen_string_literal: true +require_relative 'ruby_versions' + require 'set' module Polariscope module Scanner class GemVersions + RUBY_NAME = 'ruby' + def initialize(dependency_names, spec_type:) @dependency_names = dependency_names.to_set @spec_type = spec_type - @gem_versions = Hash.new { |h, k| h[k] = [] } + @gem_versions = Hash.new { |h, k| h[k] = Set.new } fetch_gems + fetch_ruby_versions if dependency_names.include?(RUBY_NAME) end def versions_for(gem_name) - @gem_versions[gem_name] + gem_versions[gem_name] end private + attr_reader :dependency_names + attr_reader :spec_type + attr_reader :gem_versions + + def fetch_ruby_versions + gem_versions[RUBY_NAME] = RubyVersions.available_versions + end + def fetch_gems - gem_tuples = Gem::SpecFetcher.fetcher.detect(@spec_type) do |name_tuple| - @dependency_names.include?(name_tuple.name) - end + gem_tuples.each { |(name_tuple, _)| gem_versions[name_tuple.name] << name_tuple.version } + end - gem_tuples.each { |gem_tuple| @gem_versions[gem_tuple.first.name] << gem_tuple.first.version } + def gem_tuples + Gem::SpecFetcher.fetcher.detect(spec_type) { |name_tuple| dependency_names.include?(name_tuple.name) } end end end end