lib/solve/graph.rb in solve-0.2.1 vs lib/solve/graph.rb in solve-0.3.0
- old
+ new
@@ -1,11 +1,50 @@
module Solve
+ # @author Jamie Winsor <jamie@vialstudios.com>
class Graph
+ class << self
+ # Create a key for a graph from an instance of an Artifact or Dependency
+ #
+ # @param [Solve::Artifact, Solve::Dependency] object
+ #
+ # @raise [ArgumentError] if an instance of an object of an unknown type is given
+ #
+ # @return [Symbol]
+ 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
+
+ # Create a key representing an artifact for an instance of Graph
+ #
+ # @param [#to_s] name
+ # @param [#to_s] version
+ #
+ # @return [Symbol]
+ def artifact_key(name, version)
+ "#{name}-#{version}".to_sym
+ end
+
+ # Create a key representing an dependency for an instance of Graph
+ #
+ # @param [#to_s] name
+ # @param [#to_s] constraint
+ #
+ # @return [Symbol]
+ def dependency_key(name, constraint)
+ "#{name}-#{constraint}".to_sym
+ end
+ end
+
def initialize
@artifacts = Hash.new
- @demands = Hash.new
- @dep_graph = DepSelector::DependencyGraph.new
end
# @overload artifacts(name, version)
# Return the Solve::Artifact from the collection of artifacts
# with the given name and version.
@@ -34,119 +73,72 @@
artifact = Artifact.new(self, name, version)
add_artifact(artifact)
end
+ # Return all the artifacts from the collection of artifacts
+ # with the given name.
+ #
+ # @param [String] name
+ #
+ # @return [Array<Solve::Artifact>]
+ 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
+
# 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.
#
# @param [Solve::Artifact] artifact
#
# @return [Solve::Artifact]
def add_artifact(artifact)
- unless has_artifact?(artifact)
- @dep_graph.package(artifact.name).add_version(DepSelector::Version.new(artifact.version.to_s))
- @artifacts[artifact.to_s] = artifact
+ unless has_artifact?(artifact.name, artifact.version)
+ @artifacts[self.class.key_for(artifact)] = artifact
end
- artifact
+ get_artifact(artifact.name, artifact.version)
end
- # @param [Solve::Artifact, nil] artifact
- def remove_artifact(artifact)
- if has_artifact?(artifact)
- @dep_graph.packages.delete(artifact.to_s)
- @artifacts.delete(artifact.to_s)
- end
- end
-
- # @param [Solve::Artifact] artifact
+ # Retrieve the artifact from the graph with the matching name and version
#
- # @return [Boolean]
- def has_artifact?(artifact)
- @artifacts.has_key?(artifact.to_s)
- end
-
- # @overload demands(name, constraint)
- # Return the Solve::Demand from the collection of demands
- # with the given name and constraint.
+ # @param [String] name
+ # @param [Solve::Version, #to_s] version
#
- # @param [#to_s]
- # @param [Solve::Constraint, #to_s]
- #
- # @return [Solve::Demand]
- # @overload demands(name)
- # Return the Solve::Demand from the collection of demands
- # with the given name.
- #
- # @param [#to_s]
- #
- # @return [Solve::Demand]
- # @overload demands
- # Return the collection of demands
- #
- # @return [Array<Solve::Demand>]
- def demands(*args)
- if args.empty?
- return demand_collection
- end
- if args.length > 2
- raise ArgumentError, "Unexpected number of arguments. You gave: #{args.length}. Expected: 2 or less."
- end
-
- name, constraint = args
- constraint ||= ">= 0.0.0"
-
- if name.nil?
- raise ArgumentError, "A name must be specified. You gave: #{args}."
- end
-
- demand = Demand.new(self, name, constraint)
- add_demand(demand)
+ # @return [Solve::Artifact, nil]
+ def get_artifact(name, version)
+ @artifacts.fetch(self.class.artifact_key(name, version.to_s), nil)
end
- # Add a Solve::Demand to the collection of demands and
- # return the added Solve::Demand. No change will be made
- # if the demand is already a member of the collection.
+ # Remove the given instance of artifact from the graph
#
- # @param [Solve::Demand] demand
- #
- # @return [Solve::Demand]
- def add_demand(demand)
- unless has_demand?(demand)
- @demands[demand.to_s] = demand
+ # @param [Solve::Artifact, nil] artifact
+ def remove_artifact(artifact)
+ if has_artifact?(artifact.name, artifact.version)
+ @artifacts.delete(self.class.key_for(artifact))
end
-
- demand
end
- alias_method :demand, :add_demand
- # @param [Solve::Demand, nil] demand
- def remove_demand(demand)
- if has_demand?(demand)
- @demands.delete(demand.to_s)
- end
- end
-
- # @param [Solve::Demand] demand
+ # Check if an artifact with a matching name and version is a member of this instance
+ # of graph
#
+ # @param [String] name
+ # @param [Solve::Version, #to_s] version
+ #
# @return [Boolean]
- def has_demand?(demand)
- @demands.has_key?(demand.to_s)
+ def has_artifact?(name, version)
+ !get_artifact(name, version).nil?
end
private
- attr_reader :dep_graph
-
# @return [Array<Solve::Artifact>]
def artifact_collection
@artifacts.collect { |name, artifact| artifact }
- end
-
- # @return [Array<Solve::Demand>]
- def demand_collection
- @demands.collect { |name, demand| demand }
end
end
end