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.106.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.38.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.37.2 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.105.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.37.1 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.37.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.36.1 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.36.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.35.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.34.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.33.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.32.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.104.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.31.1 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.31.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.30.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.29.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.103.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.28.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_tools-0.27.2 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb