Class: Bundler::Index

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIndex

Returns a new instance of Index.



16
17
18
19
20
# File 'lib/bundler/index.rb', line 16

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

Instance Attribute Details

#sourcesObject (readonly)

Returns the value of attribute sources.



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

def sources
  @sources
end

Class Method Details

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

Yields:

  • (i)


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

def self.build
  i = new
  yield i
  i
end

Instance Method Details

#<<(spec) ⇒ Object



77
78
79
80
81
# File 'lib/bundler/index.rb', line 77

def <<(spec)
  @specs[spec.name]["#{spec.version}-#{spec.platform}"] = spec

  spec
end

#==(o) ⇒ Object



117
118
119
120
121
122
# File 'lib/bundler/index.rb', line 117

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

#add_source(index) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/bundler/index.rb', line 124

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

#each(&blk) ⇒ Object



83
84
85
86
87
# File 'lib/bundler/index.rb', line 83

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

#empty?Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/bundler/index.rb', line 37

def empty?
  each { return false }
  true
end

#initialize_copy(o) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/bundler/index.rb', line 22

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

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

#inspectObject



33
34
35
# File 'lib/bundler/index.rb', line 33

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

#local_search(query, base = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/bundler/index.rb', line 61

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)
  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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bundler/index.rb', line 44

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
end

#sizeObject



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

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

#source_typesObject



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

def source_types
  sources.map{|s| s.class }.uniq
end

#unmet_dependency_namesObject

returns a list of the dependencies



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

def unmet_dependency_names
  dependency_names = specs.values.map do |hash_of_s|
    hash_of_s.values.map do |s|
      s.dependencies.map{|d| d.name }
    end
  end.flatten.uniq
  dependency_names.select{|name| name != 'bundler' && specs_by_name(name).empty? }
end

#use(other, override_dupes = false) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/bundler/index.rb', line 99

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