Class: Solve::Graph
- Inherits:
-
Object
- Object
- Solve::Graph
- Defined in:
- lib/solve/graph.rb
Overview
Class Method Summary collapse
-
.artifact_key(name, version) ⇒ Symbol
Create a key representing an artifact for an instance of Graph.
-
.dependency_key(name, constraint) ⇒ Symbol
Create a key representing an dependency for an instance of Graph.
-
.key_for(object) ⇒ Symbol
Create a key for a graph from an instance of an Artifact or Dependency.
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
-
#add_artifact(artifact) ⇒ Solve::Artifact
Add a Solve::Artifact to the collection of artifacts and return the added Solve::Artifact.
- #artifacts(*args) ⇒ Object
-
#get_artifact(name, version) ⇒ Solve::Artifact?
Retrieve the artifact from the graph with the matching name and version.
-
#has_artifact?(name, version) ⇒ Boolean
Check if an artifact with a matching name and version is a member of this instance of graph.
-
#initialize ⇒ Graph
constructor
A new instance of Graph.
-
#remove_artifact(artifact) ⇒ Object
Remove the given instance of artifact from the graph.
-
#versions(name, constraint = ">= 0.0.0") ⇒ Array<Solve::Artifact>
Return all the artifacts from the collection of artifacts with the given name.
Constructor Details
#initialize ⇒ Graph
Returns a new instance of Graph.
44 45 46 |
# File 'lib/solve/graph.rb', line 44 def initialize @artifacts = Hash.new end |
Class Method Details
.artifact_key(name, version) ⇒ Symbol
Create a key representing an artifact for an instance of Graph
29 30 31 |
# File 'lib/solve/graph.rb', line 29 def 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
39 40 41 |
# File 'lib/solve/graph.rb', line 39 def 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
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/solve/graph.rb', line 12 def key_for(object) case object when Solve::Artifact artifact_key(object.name, object.version) when Solve::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?
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/solve/graph.rb', line 140 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) ⇒ Solve::Artifact
Add a Solve::Artifact to the collection of artifacts and return the added Solve::Artifact. No change will be made if the artifact is already a member of the collection.
99 100 101 102 103 104 105 |
# File 'lib/solve/graph.rb', line 99 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) ⇒ Solve::Artifact #artifacts ⇒ Array<Solve::Artifact>
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/solve/graph.rb', line 60 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) ⇒ Solve::Artifact?
Retrieve the artifact from the graph with the matching name and version
113 114 115 |
# File 'lib/solve/graph.rb', line 113 def get_artifact(name, version) @artifacts.fetch(self.class.artifact_key(name, version.to_s), nil) end |
#has_artifact?(name, version) ⇒ Boolean
Check if an artifact with a matching name and version is a member of this instance of graph
133 134 135 |
# File 'lib/solve/graph.rb', line 133 def has_artifact?(name, version) !get_artifact(name, version).nil? end |
#remove_artifact(artifact) ⇒ Object
Remove the given instance of artifact from the graph
120 121 122 123 124 |
# File 'lib/solve/graph.rb', line 120 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<Solve::Artifact>
Return all the artifacts from the collection of artifacts with the given name.
84 85 86 87 88 89 90 |
# File 'lib/solve/graph.rb', line 84 def versions(name, constraint = ">= 0.0.0") constraint = constraint.is_a?(Constraint) ? constraint : Constraint.new(constraint) artifacts.select do |art| art.name == name && constraint.satisfies?(art.version) end end |