Sha256: e084c75cb8c2e3c57d93bc570b0f2b13e40d5da704251fda29d4acab58aeb981

Contents?: true

Size: 1.84 KB

Versions: 73

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require 'active_support/core_ext/string/inflections'
require 'eac_ruby_utils/recursive_builder'
require 'eac_ruby_utils/simple_cache'

module EacRubyUtils
  class GemsRegistry
    class Gem
      include ::Comparable
      include ::EacRubyUtils::SimpleCache

      attr_reader :registry, :gemspec

      def initialize(registry, gemspec)
        @registry = registry
        @gemspec = gemspec
      end

      def depend_on(gem)
        dependencies.lazy.map(&:name).include?(gem.gemspec.name)
      end

      def dependencies
        @dependencies ||= dependencies_uncached # dependencies_uncached
      end

      def <=>(other)
        sd = depend_on(other)
        od = other.depend_on(self)
        return 1 if sd && !od
        return -1 if od && !sd

        gemspec.name <=> other.gemspec.name
      end

      def found?
        lib_file_found? && registered_module.is_a?(::Module)
      end

      def lib_file_found?
        gemspec.require_paths.any? do |require_path|
          ::Pathname.new(require_path).expand_path(gemspec.gem_dir).join(path_to_require + '.rb')
            .file?
        end
      end

      def registered_module
        return nil unless lib_file_found?

        require path_to_require
        path_to_require.classify.constantize
      end

      # @return [String]
      def path_to_require
        gemspec.name.gsub('-', '/') + '/' + registry.module_suffix.underscore
      end

      def to_s
        "#{self.class.name}[#{gemspec.name}]"
      end

      private

      def dependencies_uncached
        ::EacRubyUtils::RecursiveBuilder
          .new(gemspec) { |item| gem_item_dependencies(item) }
          .result
      end

      def gem_item_dependencies(item)
        ::Gem::Specification.find_by_name(item.name).dependencies
      rescue ::Gem::MissingSpecError
        []
      end
    end
  end
end

Version data entries

73 entries across 73 versions & 3 rubygems

Version Path
eac_ruby_utils-0.102.1 lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.27.1 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.27.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.26.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.102.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.25.1 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.101.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.25.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.24.1 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.100.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.24.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.99.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.23.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.22.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.21.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.20.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.19.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.18.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.17.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.16.1 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb