PageRenderTime 84ms CodeModel.GetById 58ms app.highlight 23ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://github.com/agross/netopenspace
Ruby | 154 lines | 81 code | 31 blank | 42 comment | 10 complexity | b33754cfcfed33949bc421e43c1def4e MD5 | raw file
  1require "rubygems/version"
  2
  3##
  4# A Requirement is a set of one or more version restrictions. It supports a
  5# few (<tt>=, !=, >, <, >=, <=, ~></tt>) different restriction operators.
  6
  7class Gem::Requirement
  8  include Comparable
  9
 10  OPS = { #:nodoc:
 11    "="  =>  lambda { |v, r| v == r },
 12    "!=" =>  lambda { |v, r| v != r },
 13    ">"  =>  lambda { |v, r| v > r  },
 14    "<"  =>  lambda { |v, r| v < r  },
 15    ">=" =>  lambda { |v, r| v >= r },
 16    "<=" =>  lambda { |v, r| v <= r },
 17    "~>" =>  lambda { |v, r| v >= r && v.release < r.bump }
 18  }
 19
 20  quoted  = OPS.keys.map { |k| Regexp.quote k }.join "|"
 21  PATTERN = /\A\s*(#{quoted})?\s*(#{Gem::Version::VERSION_PATTERN})\s*\z/
 22
 23  ##
 24  # Factory method to create a Gem::Requirement object.  Input may be
 25  # a Version, a String, or nil.  Intended to simplify client code.
 26  #
 27  # If the input is "weird", the default version requirement is
 28  # returned.
 29
 30  def self.create input
 31    case input
 32    when Gem::Requirement then
 33      input
 34    when Gem::Version, Array then
 35      new input
 36    else
 37      if input.respond_to? :to_str then
 38        new [input.to_str]
 39      else
 40        default
 41      end
 42    end
 43  end
 44
 45  ##
 46  # A default "version requirement" can surely _only_ be '>= 0'.
 47  #--
 48  # This comment once said:
 49  #
 50  # "A default "version requirement" can surely _only_ be '> 0'."
 51
 52  def self.default
 53    new '>= 0'
 54  end
 55
 56  ##
 57  # Parse +obj+, returning an <tt>[op, version]</tt> pair. +obj+ can
 58  # be a String or a Gem::Version.
 59  #
 60  # If +obj+ is a String, it can be either a full requirement
 61  # specification, like <tt>">= 1.2"</tt>, or a simple version number,
 62  # like <tt>"1.2"</tt>.
 63  #
 64  #     parse("> 1.0")                 # => [">", "1.0"]
 65  #     parse("1.0")                   # => ["=", "1.0"]
 66  #     parse(Gem::Version.new("1.0")) # => ["=,  "1.0"]
 67
 68  def self.parse obj
 69    return ["=", obj] if Gem::Version === obj
 70
 71    unless PATTERN =~ obj.to_s
 72      raise ArgumentError, "Illformed requirement [#{obj.inspect}]"
 73    end
 74
 75    [$1 || "=", Gem::Version.new($2)]
 76  end
 77
 78  ##
 79  # An array of requirement pairs. The first element of the pair is
 80  # the op, and the second is the Gem::Version.
 81
 82  attr_reader :requirements #:nodoc:
 83
 84  ##
 85  # Constructs a requirement from +requirements+. Requirements can be
 86  # Strings, Gem::Versions, or Arrays of those. +nil+ and duplicate
 87  # requirements are ignored. An empty set of +requirements+ is the
 88  # same as <tt>">= 0"</tt>.
 89
 90  def initialize *requirements
 91    requirements = requirements.flatten
 92    requirements.compact!
 93    requirements.uniq!
 94
 95    requirements << ">= 0" if requirements.empty?
 96    @none = (requirements == ">= 0")
 97    @requirements = requirements.map! { |r| self.class.parse r }
 98  end
 99
100  def none?
101    @none ||= (to_s == ">= 0")
102  end
103
104  def as_list # :nodoc:
105    requirements.map { |op, version| "#{op} #{version}" }.sort
106  end
107
108  def hash # :nodoc:
109    requirements.hash
110  end
111
112  def marshal_dump # :nodoc:
113    [@requirements]
114  end
115
116  def marshal_load array # :nodoc:
117    @requirements = array[0]
118  end
119
120  def prerelease?
121    requirements.any? { |r| r.last.prerelease? }
122  end
123
124  def pretty_print q # :nodoc:
125    q.group 1, 'Gem::Requirement.new(', ')' do
126      q.pp as_list
127    end
128  end
129
130  ##
131  # True if +version+ satisfies this Requirement.
132
133  def satisfied_by? version
134    # #28965: syck has a bug with unquoted '=' YAML.loading as YAML::DefaultKey
135    requirements.all? { |op, rv| (OPS[op] || OPS["="]).call version, rv }
136  end
137
138  def to_s # :nodoc:
139    as_list.join ", "
140  end
141
142  def <=> other # :nodoc:
143    to_s <=> other.to_s
144  end
145end
146
147# :stopdoc:
148# Gem::Version::Requirement is used in a lot of old YAML specs. It's aliased
149# here for backwards compatibility. I'd like to remove this, maybe in RubyGems
150# 2.0.
151
152::Gem::Version::Requirement = ::Gem::Requirement
153# :startdoc:
154