Sha256: 58996254ad16f02f7ec362bf575f948d2775399626a3fed17cc596ede69d2978

Contents?: true

Size: 1.85 KB

Versions: 36

Compression:

Stored size: 1.85 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

36 entries across 36 versions & 3 rubygems

Version Path
eac_ruby_utils-0.94.1 lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.94.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.93.0 lib/eac_ruby_utils/gems_registry/gem.rb
avm-tools-0.116.1 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
avm-tools-0.116.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.92.1 lib/eac_ruby_utils/gems_registry/gem.rb
avm-tools-0.115.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.92.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.91.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.90.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.89.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.88.0 lib/eac_ruby_utils/gems_registry/gem.rb
avm-tools-0.114.2 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
avm-tools-0.114.1 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.87.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.86.0 lib/eac_ruby_utils/gems_registry/gem.rb
avm-tools-0.114.0 sub/eac_ruby_utils/lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.85.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.84.0 lib/eac_ruby_utils/gems_registry/gem.rb
eac_ruby_utils-0.83.0 lib/eac_ruby_utils/gems_registry/gem.rb