Class: Solve::Graph

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

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



3
4
5
6
# File 'lib/solve/graph.rb', line 3

def initialize
  @artifacts = {}
  @artifacts_by_name = Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

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

Parameters:

  • other (Object)

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/solve/graph.rb', line 66

def ==(other)
  return false unless other.is_a?(Graph)
  return false unless artifacts.size == other.artifacts.size

  self_artifacts = 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_dependencies.size == other_dependencies.size &&
    self_artifacts.all? { |artifact| other_artifacts.include?(artifact) } &&
    self_dependencies.all? { |dependency| other_dependencies.include?(dependency) }
end

#artifact(name, version) ⇒ Object

Add an artifact to the graph

Parameters:

  • name (String)


28
29
30
31
32
33
34
35
36
# File 'lib/solve/graph.rb', line 28

def artifact(name, version)
  unless artifact?(name, version)
    artifact = Artifact.new(self, name, version)
    @artifacts["#{name}-#{version}"] = artifact
    @artifacts_by_name[name] << artifact
  end

  @artifacts["#{name}-#{version}"]
end

#artifact?(name, version) ⇒ Boolean Also known as: has_artifact?

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

Parameters:

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

Returns:

  • (Boolean)


15
16
17
# File 'lib/solve/graph.rb', line 15

def artifact?(name, version)
  !find(name, version).nil?
end

#artifactsArray<Solve::Artifact>

Return the collection of artifacts

Returns:



41
42
43
# File 'lib/solve/graph.rb', line 41

def artifacts
  @artifacts.values
end

#find(name, version) ⇒ Object



20
21
22
# File 'lib/solve/graph.rb', line 20

def find(name, version)
  @artifacts["#{name}-#{version}"]
end

#versions(name, constraint = Semverse::DEFAULT_CONSTRAINT) ⇒ Array<Solve::Artifact>

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

Parameters:

  • name (String)

Returns:



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/solve/graph.rb', line 51

def versions(name, constraint = Semverse::DEFAULT_CONSTRAINT)
  constraint = Semverse::Constraint.coerce(constraint)

  if constraint == Semverse::DEFAULT_CONSTRAINT
    @artifacts_by_name[name]
  else
    @artifacts_by_name[name].select do |artifact|
      constraint.satisfies?(artifact.version)
    end
  end
end