123456789_123456789_123456789_123456789_123456789_

Class: Gem::Resolver::APISpecification

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Gem::Resolver::Specification
Defined in: lib/rubygems/resolver/api_specification.rb

Overview

Represents a specification retrieved via the Compact Index API.

This is used to avoid loading the full Specification object when all we need is the name, version, and dependencies.

Class Method Summary

Specification - Inherited

.new

Sets default instance variables for the specification.

Instance Attribute Summary

Specification - Inherited

#created_at

The time this gem version was published, when the source provides it (compact index v2), nil otherwise.

#dependencies

The dependencies of the gem for this specification.

#installable_platform?

Returns true if this specification is installable on this platform.

#name

The name of the gem for this specification.

#platform

The platform this gem works on.

#required_ruby_version

The required_ruby_version constraint for this specification.

#required_rubygems_version

The required_ruby_version constraint for this specification.

#set

The set this specification came from.

#source

The source for this specification.

#spec
#version

The version of the gem for this specification.

#local?

Instance Method Summary

Specification - Inherited

#download,
#full_name

The name and version of the specification.

#install

Installs this specification using the ::Gem::Installer options.

#fetch_development_dependencies

Fetches development dependencies if the source does not provide them by default (see APISpecification).

Constructor Details

.new(set, api_data) ⇒ APISpecification

[ GitHub ]

  
# File 'lib/rubygems/resolver/api_specification.rb', line 14

def self.new(set, api_data)
  cache_key = [set, api_data]
  cache = @@cache[cache_key]
  return cache if cache
  @@cache[cache_key] = super
end

#initialize(set, api_data) ⇒ APISpecification

Creates an APISpecification for the given set from the Compact Index API api_data.

See https://guides.rubygems.org/rubygems-org-compact-index-api for the format of the api_data.

[ GitHub ]

  
# File 'lib/rubygems/resolver/api_specification.rb', line 28

def initialize(set, api_data)
  super()

  @set = set
  @name = api_data[:name]
  @version = Gem::Version.new(api_data[:number]).freeze
  @platform = Gem::Platform.new(api_data[:platform]).freeze
  @original_platform = api_data[:platform].freeze
  @dependencies = api_data[:dependencies].map do |name, ver|
    Gem::Dependency.new(name, ver.split(/\s*,\s*/)).freeze
  end.freeze
  @required_ruby_version = Gem::Requirement.new(api_data.dig(:requirements, :ruby)).freeze
  @required_rubygems_version = Gem::Requirement.new(api_data.dig(:requirements, :rubygems)).freeze
  @created_at = parse_created_at(api_data.dig(:requirements, :created_at))&.freeze
end

Instance Attribute Details

#installable_platform?Boolean (readonly)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubygems/resolver/api_specification.rb', line 62

def installable_platform? # :nodoc:
  Gem::Platform.match_gem? @platform, @name
end

Instance Method Details

#==(other)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubygems/resolver/api_specification.rb', line 44

def ==(other) # :nodoc:
  self.class === other &&
    @set          == other.set &&
    @name         == other.name &&
    @version      == other.version &&
    @platform     == other.platform
end

#fetch_development_dependencies

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubygems/resolver/api_specification.rb', line 56

def fetch_development_dependencies # :nodoc:
  spec = source.fetch_spec Gem::NameTuple.new @name, @version, @platform

  @dependencies = spec.dependencies
end

#hash

[ GitHub ]

  
# File 'lib/rubygems/resolver/api_specification.rb', line 52

def hash
  @set.hash ^ @name.hash ^ @version.hash ^ @platform.hash
end

#parse_created_at(value) (private)

[ GitHub ]

  
# File 'lib/rubygems/resolver/api_specification.rb', line 115

def parse_created_at(value)
  value = value.first if value.is_a?(Array)
  return unless value.is_a?(String)

  require "time"
  begin
    Time.iso8601(value)
  rescue ArgumentError
    nil
  end
end

#pretty_print(q)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubygems/resolver/api_specification.rb', line 66

def pretty_print(q) # :nodoc:
  q.group 2, "[APISpecification", "]" do
    q.breakable
    q.text "name: #{name}"

    q.breakable
    q.text "version: #{version}"

    q.breakable
    q.text "platform: #{platform}"

    q.breakable
    q.text "dependencies:"
    q.breakable
    q.pp @dependencies

    q.breakable
    q.text "set uri: #{@set.dep_uri}"
  end
end

#source

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubygems/resolver/api_specification.rb', line 109

def source # :nodoc:
  @set.source
end

#spec

This method is for internal use only.

A ::Gem::Specification stub built from the Compact Index data for this specification. The compact index carries everything needed to download and install the gem, so the Marshal gemspec is not fetched. Development dependencies are not included; see #fetch_development_dependencies.

[ GitHub ]

  
# File 'lib/rubygems/resolver/api_specification.rb', line 94

def spec # :nodoc:
  @spec ||= Gem::Specification.new do |s|
    s.name     = @name
    s.version  = @version
    s.platform = @platform
    s.original_platform = @original_platform
    s.required_ruby_version = @required_ruby_version
    s.required_rubygems_version = @required_rubygems_version

    @dependencies.each do |dependency|
      s.add_runtime_dependency dependency.name, *dependency.requirement.as_list
    end
  end
end