Class: Solve::Graph
- Inherits:
-
Object
- Object
- Solve::Graph
- Defined in:
- lib/solve/graph.rb
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.
43 44 45 |
# File 'lib/solve/graph.rb', line 43 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
28 29 30 |
# File 'lib/solve/graph.rb', line 28 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
38 39 40 |
# File 'lib/solve/graph.rb', line 38 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
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/solve/graph.rb', line 11 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?
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/solve/graph.rb', line 139 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.
98 99 100 101 102 103 104 |
# File 'lib/solve/graph.rb', line 98 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>
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/solve/graph.rb', line 59 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
112 113 114 |
# File 'lib/solve/graph.rb', line 112 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
132 133 134 |
# File 'lib/solve/graph.rb', line 132 def has_artifact?(name, version) !get_artifact(name, version).nil? end |
#remove_artifact(artifact) ⇒ Object
Remove the given instance of artifact from the graph
119 120 121 122 123 |
# File 'lib/solve/graph.rb', line 119 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.
83 84 85 86 87 88 89 |
# File 'lib/solve/graph.rb', line 83 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 |