PageRenderTime 71ms CodeModel.GetById 44ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/site_ruby/1.8/rubygems/dependency.rb

http://github.com/agross/netopenspace
Ruby | 210 lines | 108 code | 53 blank | 49 comment | 23 complexity | fc4dae144ce92a0eef916df60032a3f3 MD5 | raw file
  1require "rubygems/requirement"
  2
  3##
  4# The Dependency class holds a Gem name and a Gem::Requirement.
  5
  6class Gem::Dependency
  7
  8  ##
  9  # Valid dependency types.
 10  #--
 11  # When this list is updated, be sure to change
 12  # Gem::Specification::CURRENT_SPECIFICATION_VERSION as well.
 13
 14  TYPES = [
 15           :development,
 16           :runtime,
 17          ]
 18
 19  ##
 20  # Dependency name or regular expression.
 21
 22  attr_accessor :name
 23
 24  ##
 25  # Allows you to force this dependency to be a prerelease.
 26
 27  attr_writer :prerelease
 28
 29  ##
 30  # Constructs a dependency with +name+ and +requirements+. The last
 31  # argument can optionally be the dependency type, which defaults to
 32  # <tt>:runtime</tt>.
 33
 34  def initialize name, *requirements
 35    type         = Symbol === requirements.last ? requirements.pop : :runtime
 36    requirements = requirements.first if 1 == requirements.length # unpack
 37
 38    unless TYPES.include? type
 39      raise ArgumentError, "Valid types are #{TYPES.inspect}, "
 40        + "not #{type.inspect}"
 41    end
 42
 43    @name        = name
 44    @requirement = Gem::Requirement.create requirements
 45    @type        = type
 46    @prerelease  = false
 47
 48    # This is for Marshal backwards compatibility. See the comments in
 49    # +requirement+ for the dirty details.
 50
 51    @version_requirements = @requirement
 52  end
 53
 54  ##
 55  # A dependency's hash is the XOR of the hashes of +name+, +type+,
 56  # and +requirement+.
 57
 58  def hash # :nodoc:
 59    name.hash ^ type.hash ^ requirement.hash
 60  end
 61
 62  def inspect # :nodoc:
 63    "<%s type=%p name=%p requirements=%p>" %
 64      [self.class, self.type, self.name, requirement.to_s]
 65  end
 66
 67  ##
 68  # Does this dependency require a prerelease?
 69
 70  def prerelease?
 71    @prerelease || requirement.prerelease?
 72  end
 73
 74  def pretty_print q # :nodoc:
 75    q.group 1, 'Gem::Dependency.new(', ')' do
 76      q.pp name
 77      q.text ','
 78      q.breakable
 79
 80      q.pp requirement
 81
 82      q.text ','
 83      q.breakable
 84
 85      q.pp type
 86    end
 87  end
 88
 89  ##
 90  # What does this dependency require?
 91
 92  def requirement
 93    return @requirement if defined?(@requirement) and @requirement
 94
 95    # @version_requirements and @version_requirement are legacy ivar
 96    # names, and supported here because older gems need to keep
 97    # working and Dependency doesn't implement marshal_dump and
 98    # marshal_load. In a happier world, this would be an
 99    # attr_accessor. The horrifying instance_variable_get you see
100    # below is also the legacy of some old restructurings.
101    #
102    # Note also that because of backwards compatibility (loading new
103    # gems in an old RubyGems installation), we can't add explicit
104    # marshaling to this class until we want to make a big
105    # break. Maybe 2.0.
106    #
107    # Children, define explicit marshal and unmarshal behavior for
108    # public classes. Marshal formats are part of your public API.
109
110    if defined?(@version_requirement) && @version_requirement
111      version = @version_requirement.instance_variable_get :@version
112      @version_requirement  = nil
113      @version_requirements = Gem::Requirement.new version
114    end
115
116    @requirement = @version_requirements if defined?(@version_requirements)
117  end
118
119  def requirements_list
120    requirement.as_list
121  end
122
123  def to_s # :nodoc:
124    if type != :runtime then
125      "#{name} (#{requirement}, #{type})"
126    else
127      "#{name} (#{requirement})"
128    end
129  end
130
131  ##
132  # Dependency type.
133
134  def type
135    @type ||= :runtime
136  end
137
138  def == other # :nodoc:
139    Gem::Dependency === other &&
140      self.name        == other.name &&
141      self.type        == other.type &&
142      self.requirement == other.requirement
143  end
144
145  ##
146  # Dependencies are ordered by name.
147
148  def <=> other
149    self.name <=> other.name
150  end
151
152  ##
153  # Uses this dependency as a pattern to compare to +other+. This
154  # dependency will match if the name matches the other's name, and
155  # other has only an equal version requirement that satisfies this
156  # dependency.
157
158  def =~ other
159    unless Gem::Dependency === other
160      return unless other.respond_to?(:name) && other.respond_to?(:version)
161      other = Gem::Dependency.new other.name, other.version
162    end
163
164    return false unless name === other.name
165
166    reqs = other.requirement.requirements
167
168    return false unless reqs.length == 1
169    return false unless reqs.first.first == '='
170
171    version = reqs.first.last
172
173    requirement.satisfied_by? version
174  end
175
176  def match? name, version
177    return false unless self.name === name
178    return true if requirement.none?
179
180    requirement.satisfied_by? Gem::Version.new(version)
181  end
182
183  def matches_spec? spec
184    return false unless name === spec.name
185    return true  if requirement.none?
186
187    requirement.satisfied_by?(spec.version)
188  end
189
190  ##
191  # Merges the requirements of +other+ into this dependency
192
193  def merge other
194    unless name == other.name then
195      raise ArgumentError,
196            "#{self} and #{other} have different names"
197    end
198
199    default = Gem::Requirement.default
200    self_req  = self.requirement
201    other_req = other.requirement
202
203    return self.class.new name, self_req  if other_req == default
204    return self.class.new name, other_req if self_req  == default
205
206    self.class.new name, self_req.as_list.concat(other_req.as_list)
207  end
208
209end
210