1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
module Solve
class Artifact
include Comparable
# A reference to the graph this artifact belongs to
#
# @return [Solve::Graph]
attr_reader :graph
# The name of the artifact
#
# @return [String]
attr_reader :name
# The version of this artifact
#
# @return [Semverse::Version]
attr_reader :version
# @param [Solve::Graph] graph
# @param [#to_s] name
# @param [Semverse::Version, #to_s] version
def initialize(graph, name, version)
@graph = graph
@name = name
@version = Semverse::Version.new(version)
@dependencies = {}
end
# Check if the artifact has a dependency with the matching name and
# constraint
#
# @param [#to_s] name
# @param [#to_s] constraint
#
# @return [Boolean]
def dependency?(name, constraint)
!get_dependency(name, constraint).nil?
end
alias_method :has_dependency?, :dependency?
# Retrieve the dependency from the artifact with the matching name and constraint
#
# @param [#to_s] name
# @param [#to_s] constraint
#
# @return [Solve::Artifact, nil]
def dependency(name, constraint)
set_dependency(name, constraint)
end
# Return the collection of dependencies on this instance of artifact
#
# @return [Array<Solve::Dependency>]
def dependencies
@dependencies.values
end
# Return the Solve::Dependency from the collection of
# dependencies with the given name and constraint.
#
# @param [#to_s] name
# @param [String] constraint
#
# @example Adding dependencies
# artifact.depends('nginx')
# #=> #<Dependency nginx (>= 0.0.0)>
# artifact.depends('ntp', '= 1.0.0')
# #=> #<Dependency ntp (= 1.0.0)>
#
# @example Chaining dependencies
# artifact
# .depends('nginx')
# .depends('ntp', '~> 1.3')
#
# @return [Solve::Artifact]
def depends(name, constraint = ">= 0.0.0")
unless dependency?(name, constraint)
set_dependency(name, constraint)
end
self
end
def to_s
"#{name}-#{version}"
end
# @param [Object] other
#
# @return [Boolean]
def ==(other)
other.is_a?(self.class) &&
name == other.name &&
version == other.version
end
alias_method :eql?, :==
# @param [Semverse::Version] other
#
# @return [Integer]
def <=>(other)
version <=> other.version
end
private
def get_dependency(name, constraint)
@dependencies["#{name}-#{constraint}"]
end
def set_dependency(name, constraint)
@dependencies["#{name}-#{constraint}"] = Dependency.new(self, name, constraint)
end
end
end
|