Class: Stickler::Repository::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/stickler/repository/index.rb

Overview

A repository index is a container holding all the SpecLite elements in the repository. All the gem specs that this index holds are derived from actual files on the file system.

It is modelled somewhat like a Gem::SourceIndex.

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec_dir) ⇒ Index

Returns a new instance of Index.



23
24
25
26
27
28
29
30
# File 'lib/stickler/repository/index.rb', line 23

def initialize( spec_dir )
  @specs              = []
  @spec_dir           = spec_dir
  @last_modified_time = nil
  @last_entry_count   = nil
  @spec_glob          = File.join( @spec_dir, "*.gemspec" )
  load_specs
end

Instance Attribute Details

#last_entry_countObject (readonly)

The number of entries in the spec directory



21
22
23
# File 'lib/stickler/repository/index.rb', line 21

def last_entry_count
  @last_entry_count
end

#last_modified_timeObject (readonly)

The last time the repository directory was modified



18
19
20
# File 'lib/stickler/repository/index.rb', line 18

def last_modified_time
  @last_modified_time
end

#spec_dirObject

The directory the specs live



15
16
17
# File 'lib/stickler/repository/index.rb', line 15

def spec_dir
  @spec_dir
end

Instance Method Details

#add_spec_from_file(path) ⇒ Object

Raises:



114
115
116
117
118
119
120
121
122
123
# File 'lib/stickler/repository/index.rb', line 114

def add_spec_from_file( path )
  return nil unless File.exist?( path )
  code = File.read( path )
  full_spec = eval( code, binding, path )
  raise Error, "File #{path} is not Gem::Specification in ruby format" unless full_spec.is_a?( Gem::Specification )

  full_spec.untaint
  spec = Stickler::SpecLite.new( full_spec.name, full_spec.version, full_spec.platform )
  @specs << spec
end

#current_entry_countObject



87
88
89
# File 'lib/stickler/repository/index.rb', line 87

def current_entry_count
  Dir.glob( @spec_glob ).size
end

#current_modified_timeObject



83
84
85
# File 'lib/stickler/repository/index.rb', line 83

def current_modified_time
  File.stat( self.spec_dir ).mtime
end

#latest_specsObject

return all the latest specs in the repository, do not include pre-release gems



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stickler/repository/index.rb', line 41

def latest_specs
  latest = {}
  specs.each do |s|
    next if s.prerelease?
    key = "#{s.name}.#{s.platform}"
    if old_spec = latest[key] then
      if old_spec.version < s.version then
        latest[key] = s
      end
    else
      latest[key] = s
    end
  end
  latest.values
end

#load_specsObject



71
72
73
# File 'lib/stickler/repository/index.rb', line 71

def load_specs
  load_specs_in_dir( self.spec_dir )
end

#load_specs_in_dir(spec_dir) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/stickler/repository/index.rb', line 102

def load_specs_in_dir( spec_dir )
  return nil unless File.directory?( spec_dir )
  @specs.clear
  self.spec_dir = spec_dir

  Dir.foreach( spec_dir ) do |entry|
    next unless entry =~ /\.gemspec\Z/
    add_spec_from_file( File.join( spec_dir, entry ) )
  end
  update_reload_conditions
end

#prerelease_specsObject

return just the list of pre-release specs



60
61
62
# File 'lib/stickler/repository/index.rb', line 60

def prerelease_specs
  specs.select { |s| s.prerelease? }
end

#released_specsObject

return just the list of release specs



67
68
69
# File 'lib/stickler/repository/index.rb', line 67

def released_specs
  specs.select { |s| not s.prerelease? }
end

#reload_necessary?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/stickler/repository/index.rb', line 75

def reload_necessary?
  return true unless @last_modified_time
  return true unless @last_entry_count
  return true if (self.current_modified_time > @last_modified_time )
  return true if (self.current_entry_count  != @last_entry_count   )
  return false
end

#search(find_spec) ⇒ Object



125
126
127
128
129
# File 'lib/stickler/repository/index.rb', line 125

def search( find_spec )
  specs.select do |spec|
    spec =~ find_spec
  end
end

#specsObject



32
33
34
35
# File 'lib/stickler/repository/index.rb', line 32

def specs
  load_specs if reload_necessary?
  return @specs
end

#update_reload_conditionsObject



97
98
99
100
# File 'lib/stickler/repository/index.rb', line 97

def update_reload_conditions
  @last_modified_time = self.current_modified_time
  @last_entry_count   = self.current_entry_count
end