Class: Gem::Micro::Source

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/microgem/source.rb

Constant Summary collapse

SPECS_FILE =
"specs.4.8"
SPECS_ARCHIVE_FILE =
"#{SPECS_FILE}.gz"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#config, #ensure_dir, #log, #replace, #tmpdir

Constructor Details

#initialize(host, directory) ⇒ Source

Initializes a Source with host and the directory where the index should be.

s = Source.new('gems.rubyforge.org', '/path/to/gem/indices/')
s.specs_url # => "http://gems.rubyforge.org/specs.4.8.gz"
s.index_file # => "/path/to/indices/gems.rubyforge.org"


40
41
42
# File 'lib/microgem/source.rb', line 40

def initialize(host, directory)
  @host, @directory = host, directory
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



32
33
34
# File 'lib/microgem/source.rb', line 32

def host
  @host
end

Class Method Details

.gem_spec(name, version) ⇒ Object

Returns a gemspec from any of the sources matching the given name and version.



14
15
16
17
18
19
20
# File 'lib/microgem/source.rb', line 14

def self.gem_spec(name, version)
  sources.each do |source|
    if spec = source.gem_spec(name, version)
      return spec
    end
  end
end

.sourcesObject

Returns an array of available Source instances as specified on Gem::Micro::Config.instance.



6
7
8
9
10
# File 'lib/microgem/source.rb', line 6

def self.sources
  @sources ||= Config.sources.map do |source|
    new(source, Config.gem_home)
  end
end

.update!Object

Calls #update! on all sources.



23
24
25
# File 'lib/microgem/source.rb', line 23

def self.update!
  sources.each { |source| source.update! }
end

Instance Method Details

#exist?Boolean

Returns whether or not the index exists at index_file.

Returns:

  • (Boolean)


67
68
69
# File 'lib/microgem/source.rb', line 67

def exist?
  File.exist?(index_file)
end

#gem_spec(name, version) ⇒ Object

Returns a Gem::Specification matching the given name and version.



97
98
99
100
101
# File 'lib/microgem/source.rb', line 97

def gem_spec(name, version)
  if spec = spec(name, version)
    BareSpecification.new(self, name, spec[1]).gem_spec
  end
end

#index_fileObject

Returns the full path to the index file.



45
46
47
# File 'lib/microgem/source.rb', line 45

def index_file
  File.join(@directory, @host)
end

#inspectObject



103
104
105
# File 'lib/microgem/source.rb', line 103

def inspect
  "<#<Gem::Micro::Source:#{object_id} host=\"#{@host}\" index=\"#{index_file}\">"
end

#spec(name, version) ⇒ Object

Returns a gem name and it’s version matching the given name and version.



92
93
94
# File 'lib/microgem/source.rb', line 92

def spec(name, version)
  specs.select { |spec| spec[0] == name && (version.any? || spec[1] == version) }.last
end

#specsObject

Loads and returns an array of all gem names and versions.

Creates an index with update! if the index doesn’t exist yet.



82
83
84
85
86
87
88
# File 'lib/microgem/source.rb', line 82

def specs
  unless @specs
    update! unless exist?
    @specs = Marshal.load(File.read(index_file))
  end
  @specs
end

#specs_urlObject

Returns the url for the complete marshalled list of specs.



62
63
64
# File 'lib/microgem/source.rb', line 62

def specs_url
  "http://#{@host}/#{SPECS_ARCHIVE_FILE}"
end

#update!Object

Downloads and unpacks a index file to index_file.



72
73
74
75
76
77
# File 'lib/microgem/source.rb', line 72

def update!
  log(:info, "Updating source: #{@host}")
  Downloader.get(specs_url, work_archive_file)
  Unpacker.gzip(work_archive_file)
  FileUtils.mv(work_index_file, index_file)
end

#work_archive_fileObject

Returns the full path to the temporary work directory to where the index archive should be downloaded.



57
58
59
# File 'lib/microgem/source.rb', line 57

def work_archive_file
  File.join(tmpdir, SPECS_ARCHIVE_FILE)
end

#work_index_fileObject

Returns the full path to the temporary work directory to where the index should be unpacked.



51
52
53
# File 'lib/microgem/source.rb', line 51

def work_index_file
  File.join(tmpdir, SPECS_FILE)
end