Class: Gemirro::VersionsFile

Inherits:
Object
  • Object
show all
Defined in:
lib/gemirro/versions_file.rb

Overview

The VersionsFile class acts as a small Ruby wrapper around the RubyGems file that contains all Gems and their associated versions.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(versions) ⇒ VersionsFile



42
43
44
45
# File 'lib/gemirro/versions_file.rb', line 42

def initialize(versions)
  @versions      = versions
  @versions_hash = create_versions_hash
end

Instance Attribute Details

#versionsArray (readonly)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gemirro/versions_file.rb', line 13

class VersionsFile
  attr_reader :versions, :versions_hash

  ##
  # Reads the versions file from the specified String.
  #
  # @param [String] spec_content
  # @param [String] prerelease_content
  # @return [Gemirro::VersionsFile]
  #
  def self.load(spec_content, prerelease_content)
    buffer = StringIO.new(spec_content)
    reader = Zlib::GzipReader.new(buffer)
    versions = Marshal.load(reader.read)

    buffer = StringIO.new(prerelease_content)
    reader = Zlib::GzipReader.new(buffer)
    versions.concat(Marshal.load(reader.read))

    instance = new(versions)

    reader.close

    instance
  end

  ##
  # @param [Array] versions
  #
  def initialize(versions)
    @versions      = versions
    @versions_hash = create_versions_hash
  end

  ##
  # Creates a Hash based on the Array containing all versions. This Hash is
  # used to more easily (and faster) iterate over all the gems/versions.
  #
  # @return [Hash]
  #
  def create_versions_hash
    hash = Hash.new { |h, k| h[k] = [] }

    versions.each do |version|
      hash[version[0]] << version
    end

    hash
  end

  ##
  # Returns an Array containing all the available versions for a Gem.
  #
  # @param [String] gem
  # @return [Array]
  #
  def versions_for(gem)
    versions_hash[gem].map { |version| [version[1], version[2]] }
  end
end

#versions_hashHash (readonly)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gemirro/versions_file.rb', line 13

class VersionsFile
  attr_reader :versions, :versions_hash

  ##
  # Reads the versions file from the specified String.
  #
  # @param [String] spec_content
  # @param [String] prerelease_content
  # @return [Gemirro::VersionsFile]
  #
  def self.load(spec_content, prerelease_content)
    buffer = StringIO.new(spec_content)
    reader = Zlib::GzipReader.new(buffer)
    versions = Marshal.load(reader.read)

    buffer = StringIO.new(prerelease_content)
    reader = Zlib::GzipReader.new(buffer)
    versions.concat(Marshal.load(reader.read))

    instance = new(versions)

    reader.close

    instance
  end

  ##
  # @param [Array] versions
  #
  def initialize(versions)
    @versions      = versions
    @versions_hash = create_versions_hash
  end

  ##
  # Creates a Hash based on the Array containing all versions. This Hash is
  # used to more easily (and faster) iterate over all the gems/versions.
  #
  # @return [Hash]
  #
  def create_versions_hash
    hash = Hash.new { |h, k| h[k] = [] }

    versions.each do |version|
      hash[version[0]] << version
    end

    hash
  end

  ##
  # Returns an Array containing all the available versions for a Gem.
  #
  # @param [String] gem
  # @return [Array]
  #
  def versions_for(gem)
    versions_hash[gem].map { |version| [version[1], version[2]] }
  end
end

Class Method Details

.load(spec_content, prerelease_content) ⇒ Gemirro::VersionsFile

Reads the versions file from the specified String.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gemirro/versions_file.rb', line 23

def self.load(spec_content, prerelease_content)
  buffer = StringIO.new(spec_content)
  reader = Zlib::GzipReader.new(buffer)
  versions = Marshal.load(reader.read)

  buffer = StringIO.new(prerelease_content)
  reader = Zlib::GzipReader.new(buffer)
  versions.concat(Marshal.load(reader.read))

  instance = new(versions)

  reader.close

  instance
end

Instance Method Details

#create_versions_hashHash

Creates a Hash based on the Array containing all versions. This Hash is used to more easily (and faster) iterate over all the gems/versions.



53
54
55
56
57
58
59
60
61
# File 'lib/gemirro/versions_file.rb', line 53

def create_versions_hash
  hash = Hash.new { |h, k| h[k] = [] }

  versions.each do |version|
    hash[version[0]] << version
  end

  hash
end

#versions_for(gem) ⇒ Array

Returns an Array containing all the available versions for a Gem.



69
70
71
# File 'lib/gemirro/versions_file.rb', line 69

def versions_for(gem)
  versions_hash[gem].map { |version| [version[1], version[2]] }
end