Class: GemGrep::Index

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Index

Returns a new instance of Index.



30
31
32
33
# File 'lib/gem_grep.rb', line 30

def initialize(server)
  @server = server
  setup_marshal_index unless File.exists?(marshal_file)
end

Instance Attribute Details

#serverObject

Returns the value of attribute server.



29
30
31
# File 'lib/gem_grep.rb', line 29

def server
  @server
end

Instance Method Details

#download_marshal_indexObject



53
54
55
56
57
# File 'lib/gem_grep.rb', line 53

def download_marshal_index
  puts "Downloading compressed Marshal gemspec index to ~/.gem_grep. Patience is a bitch..."
  FileUtils.mkdir_p("~/.gem_grep")
  system("curl #{marshal_url} > #{marshal_compressed_file}")
end

#gem_indexObject



39
40
41
42
43
44
45
# File 'lib/gem_grep.rb', line 39

def gem_index
  @gem_index ||= begin
    puts "Loading large gem index. Patience is a bitch ..."
    spec_hash = Hash[*Marshal.load(File.read(marshal_file)).flatten]
    Gem::SourceIndex.new(spec_hash).extend(Gem::SuperSearch)
  end
end

#gem_serverObject



35
36
37
# File 'lib/gem_grep.rb', line 35

def gem_server
  {:rubygems=>"http://rubygems.org", :github=>"http://gems.github.com"}
end

#marshal_compressed_fileObject



63
64
65
# File 'lib/gem_grep.rb', line 63

def marshal_compressed_file
  File.expand_path "~/.gem_grep/marshal_#{server}.Z"
end

#marshal_fileObject



59
60
61
# File 'lib/gem_grep.rb', line 59

def marshal_file
  File.expand_path "~/.gem_grep/marshal_#{server}"
end

#marshal_urlObject



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

def marshal_url
  gem_server[server] + "/Marshal.#{Marshal::MAJOR_VERSION}.#{Marshal::MINOR_VERSION}.Z"
end

#setup_marshal_index(name = nil) ⇒ Object



47
48
49
50
51
# File 'lib/gem_grep.rb', line 47

def setup_marshal_index(name=nil)
  server = name if name
  download_marshal_index unless File.exists?(marshal_compressed_file)
  File.open(marshal_file, 'w') {|f| f.write Zlib::Inflate.inflate(File.read(marshal_compressed_file)) }
end