Class: Bundler::Index

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/bundler/index.rb

Constant Summary

RUBY =
"ruby".freeze
NULL =
"\0".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIndex



20
21
22
23
24
25
# File 'lib/bundler/index.rb', line 20

def initialize
  @sources = []
  @cache = {}
  @specs = Hash.new {|h, k| h[k] = {} }
  @all_specs = Hash.new {|h, k| h[k] = [] }
end

Instance Attribute Details

#sourcesObject (readonly)

Returns the value of attribute sources



14
15
16
# File 'lib/bundler/index.rb', line 14

def sources
  @sources
end

Class Method Details

.build {|i| ... } ⇒ Object

Yields:

  • (i)


8
9
10
11
12
# File 'lib/bundler/index.rb', line 8

def self.build
  i = new
  yield i
  i
end

Instance Method Details

#<<(spec) ⇒ Object



91
92
93
94
95
# File 'lib/bundler/index.rb', line 91

def <<(spec)
  @specs[spec.name][spec.full_name] = spec

  spec
end

#==(other) ⇒ Object



135
136
137
138
139
140
# File 'lib/bundler/index.rb', line 135

def ==(other)
  all? do |spec|
    other_spec = other[spec].first
    other_spec && (spec.dependencies & other_spec.dependencies).empty? && spec.source == other_spec.source
  end
end

#add_source(index) ⇒ Object

Raises:

  • (ArgumentError)


142
143
144
145
146
# File 'lib/bundler/index.rb', line 142

def add_source(index)
  raise ArgumentError, "Source must be an index, not #{index.class}" unless index.is_a?(Index)
  @sources << index
  @sources.uniq! # need to use uniq! here instead of checking for the item before adding
end

#dependency_namesObject



110
111
112
113
114
# File 'lib/bundler/index.rb', line 110

def dependency_names
  names = []
  each {|s| names.push(*s.dependencies.map(&:name)) }
  names.uniq
end

#each(&blk) ⇒ Object



97
98
99
100
101
# File 'lib/bundler/index.rb', line 97

def each(&blk)
  specs.values.each do |spec_sets|
    spec_sets.values.each(&blk)
  end
end

#empty?Boolean



46
47
48
49
# File 'lib/bundler/index.rb', line 46

def empty?
  each { return false }
  true
end

#initialize_copy(o) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bundler/index.rb', line 27

def initialize_copy(o)
  super
  @sources = @sources.dup
  @cache = {}
  @specs = Hash.new {|h, k| h[k] = {} }
  @all_specs = Hash.new {|h, k| h[k] = [] }

  o.specs.each do |name, hash|
    @specs[name] = hash.dup
  end
  o.all_specs.each do |name, array|
    @all_specs[name] = array.dup
  end
end

#inspectObject



42
43
44
# File 'lib/bundler/index.rb', line 42

def inspect
  "#<#{self.class}:0x#{object_id} sources=#{sources.map(&:inspect)} specs.size=#{specs.size}>"
end

#local_search(query, base = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/bundler/index.rb', line 78

def local_search(query, base = nil)
  case query
  when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification then search_by_spec(query)
  when String then specs_by_name(query)
  when Gem::Dependency then search_by_dependency(query, base)
  when DepProxy then search_by_dependency(query.dep, base)
  else
    raise "You can't search for a #{query.inspect}."
  end
end

#search(query, base = nil) ⇒ Object Also known as: []

Search this index's specs, and any source indexes that this index knows about, returning all of the results.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bundler/index.rb', line 61

def search(query, base = nil)
  results = local_search(query, base)
  seen = Set.new(results.map {|spec| [spec.name, spec.version, spec.platform] })

  @sources.each do |source|
    source.search(query, base).each do |spec|
      lookup = [spec.name, spec.version, spec.platform]
      unless seen.include?(lookup)
        results << spec
        seen << lookup
      end
    end
  end

  results.sort_by {|s| [s.version, s.platform.to_s == RUBY ? NULL : s.platform.to_s] }
end

#search_all(name) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/bundler/index.rb', line 51

def search_all(name)
  all_matches = @all_specs[name] + local_search(name)
  @sources.each do |source|
    all_matches.concat(source.search_all(name))
  end
  all_matches
end

#sizeObject



129
130
131
132
133
# File 'lib/bundler/index.rb', line 129

def size
  @sources.inject(@specs.size) do |size, source|
    size += source.size
  end
end

#unmet_dependency_namesObject

returns a list of the dependencies



104
105
106
107
108
# File 'lib/bundler/index.rb', line 104

def unmet_dependency_names
  names = dependency_names
  names.delete_if {|n| n == "bundler" }
  names.select {|n| search(n).empty? }
end

#use(other, override_dupes = false) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/bundler/index.rb', line 116

def use(other, override_dupes = false)
  return unless other
  other.each do |s|
    if (dupes = search_by_spec(s)) && dupes.any?
      @all_specs[s.name] = [s] + dupes
      next unless override_dupes
      self << s
    end
    self << s
  end
  self
end