Class: Doubleshot::Resolver::GemResolver::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/doubleshot/resolver/gem_resolver/graph.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*repositories) ⇒ Graph

Returns a new instance of Graph.



43
44
45
46
47
48
49
# File 'lib/doubleshot/resolver/gem_resolver/graph.rb', line 43

def initialize(*repositories)
  @sources   = repositories.map do |repository|
    Source.new(repository)
  end
  @versions  = Hash.new
  @artifacts = Hash.new
end

Class Method Details

.artifact_key(name, version) ⇒ Symbol

Create a key representing an artifact for an instance of Graph

Parameters:

  • name (#to_s)
  • version (#to_s)

Returns:

  • (Symbol)


29
30
31
# File 'lib/doubleshot/resolver/gem_resolver/graph.rb', line 29

def self.artifact_key(name, version)
  "#{name}-#{version}".to_sym
end

.dependency_key(name, constraint) ⇒ Symbol

Create a key representing an dependency for an instance of Graph

Parameters:

  • name (#to_s)
  • constraint (#to_s)

Returns:

  • (Symbol)


39
40
41
# File 'lib/doubleshot/resolver/gem_resolver/graph.rb', line 39

def self.dependency_key(name, constraint)
  "#{name}-#{constraint}".to_sym
end

.key_for(object) ⇒ Symbol

Create a key for a graph from an instance of an Artifact or Dependency

Parameters:

Returns:

  • (Symbol)

Raises:

  • (ArgumentError)

    if an instance of an object of an unknown type is given



12
13
14
15
16
17
18
19
20
21
# File 'lib/doubleshot/resolver/gem_resolver/graph.rb', line 12

def self.key_for(object)
  case object
  when Doubleshot::Resolver::GemResolver::Artifact
    artifact_key(object.name, object.version)
  when Doubleshot::Resolver::GemResolver::Dependency
    dependency_key(object.name, object.constraint)
  else
    raise ArgumentError, "Could not generate graph key for Class: #{object.class}"
  end
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

Returns:

  • (Boolean)


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/doubleshot/resolver/gem_resolver/graph.rb', line 170

def ==(other)
  return false unless other.is_a?(self.class)

  self_artifacts = self.artifacts
  other_artifacts = other.artifacts

  self_dependencies = self_artifacts.inject([]) do |list, artifact|
    list << artifact.dependencies
  end.flatten

  other_dependencies = other_artifacts.inject([]) do |list, artifact|
    list << artifact.dependencies
  end.flatten

  self_artifacts.size == other_artifacts.size &&
  self_dependencies.size == other_dependencies.size &&
  self_artifacts.all? { |artifact| other_artifacts.include?(artifact) } &&
  self_dependencies.all? { |dependency| other_dependencies.include?(dependency) }
end

#add_artifact(artifact) ⇒ Doubleshot::Resolver::GemResolver::Artifact

Add a Doubleshot::Resolver::GemResolver::Artifact to the collection of artifacts and return the added Doubleshot::Resolver::GemResolver::Artifact. No change will be made if the artifact is already a member of the collection.



113
114
115
116
117
118
119
# File 'lib/doubleshot/resolver/gem_resolver/graph.rb', line 113

def add_artifact(artifact)
  unless has_artifact?(artifact.name, artifact.version)
    @artifacts[self.class.key_for(artifact)] = artifact
  end

  get_artifact(artifact.name, artifact.version)
end

#artifacts(name, version) ⇒ Doubleshot::Resolver::GemResolver::Artifact #artifactsArray<Doubleshot::Resolver::GemResolver::Artifact>

Overloads:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/doubleshot/resolver/gem_resolver/graph.rb', line 63

def artifacts(*args)
  if args.empty?
    return artifact_collection
  end
  unless args.length == 2
    raise ArgumentError, "Unexpected number of arguments. You gave: #{args.length}. Expected: 0 or 2."
  end

  name, version = args

  if name.nil? || version.nil?
    raise ArgumentError, "A name and version must be specified. You gave: #{args}."
  end

  artifact = Artifact.new(self, name, version)
  add_artifact(artifact)
end

#get_artifact(name, version) ⇒ Doubleshot::Resolver::GemResolver::Artifact?

Retrieve the artifact from the graph with the matching name and version

Parameters:

  • name (String)
  • version (Gem::Version, #to_s)

Returns:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/doubleshot/resolver/gem_resolver/graph.rb', line 127

def get_artifact(name, version)
  if @sources.empty?
    @artifacts.fetch(self.class.artifact_key(name, version.to_s), nil)
  else
    artifact = nil
    @sources.any? do |source|
      if spec = source.spec(name, version)
        artifact = Artifact.new(self, name, version)
        spec.runtime_dependencies.each do |dependency|
          dependency.requirements_list.each do |requirement|
            artifact.depends(dependency.name, requirement.to_s)
          end
        end
        true
      end
    end
    artifact
  end
end

#has_artifact?(name, version) ⇒ Boolean

Check if an artifact with a matching name and version is a member of this instance of graph

Parameters:

  • name (String)
  • version (Gem::Version, #to_s)

Returns:

  • (Boolean)


163
164
165
# File 'lib/doubleshot/resolver/gem_resolver/graph.rb', line 163

def has_artifact?(name, version)
  !get_artifact(name, version).nil?
end

#remove_artifact(artifact) ⇒ Object

Remove the given instance of artifact from the graph

Parameters:



150
151
152
153
154
# File 'lib/doubleshot/resolver/gem_resolver/graph.rb', line 150

def remove_artifact(artifact)
  if has_artifact?(artifact.name, artifact.version)
    @artifacts.delete(self.class.key_for(artifact))
  end
end

#versions(name, constraint = ">= 0.0.0") ⇒ Array<Doubleshot::Resolver::GemResolver::Artifact>

Return all the artifacts from the collection of artifacts with the given name.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/doubleshot/resolver/gem_resolver/graph.rb', line 87

def versions(name, constraint = ">= 0.0.0")
  constraint = constraint.is_a?(Gem::Requirement) ? constraint : Gem::Requirement.new(constraint)

  if @sources.empty?
    # ORIGINAL CODE FROM Solve PROJECT. DO NOT TOUCH!
    artifacts.select do |artifact|
      artifact.name == name && constraint.satisfied_by?(artifact.version)
    end
  else
    @sources.map do |source|
      source.versions(name).select do |version|
        constraint.satisfied_by?(version)
      end.map do |version|
        Artifact.new(self, name, version)
      end
    end.flatten
  end
end