2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
4 # See LICENSE.txt for permissions.
10 # The Dependency class holds a Gem name and a Gem::Requirement
15 # Valid dependency types.
17 # When this list is updated, be sure to change
18 # Gem::Specification::CURRENT_SPECIFICATION_VERSION as well.
26 # Dependency name or regular expression.
38 attr_writer :version_requirements
41 # Orders dependencies by name only.
44 [@name] <=> [other.name]
48 # Constructs a dependency with +name+ and +requirements+.
50 def initialize(name, version_requirements, type=:runtime)
53 unless TYPES.include? type
54 raise ArgumentError, "Valid types are #{TYPES.inspect}, not #{@type.inspect}"
59 @version_requirements = Gem::Requirement.create version_requirements
60 @version_requirement = nil # Avoid warnings.
63 def version_requirements
64 normalize if defined? @version_requirement and @version_requirement
69 version_requirements.as_list
72 alias requirements_list requirement_list
75 ver = @version_requirement.instance_eval { @version }
76 @version_requirements = Gem::Requirement.new([ver])
77 @version_requirement = nil
81 "#{name} (#{version_requirements}, #{@type || :runtime})"
84 def ==(other) # :nodoc:
85 self.class === other &&
86 self.name == other.name &&
87 self.type == other.type &&
88 self.version_requirements == other.version_requirements
92 # Uses this dependency as a pattern to compare to the dependency +other+.
93 # This dependency will match if the name matches the other's name, and other
94 # has only an equal version requirement that satisfies this dependency.
97 return false unless self.class === other
100 pattern = /\A#{@name}\Z/ unless Regexp === pattern
102 return false unless pattern =~ other.name
104 reqs = other.version_requirements.requirements
106 return false unless reqs.length == 1
107 return false unless reqs.first.first == '='
109 version = reqs.first.last
111 version_requirements.satisfied_by? version
115 name.hash + type.hash + version_requirements.hash