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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
# See LICENSE.txt for permissions.
#++
require 'rubygems/version'
##
# Requirement version includes a prefaced comparator in addition
# to a version number.
#
# A Requirement object can actually contain multiple, er,
# requirements, as in (> 1.2, < 2.0).
class Gem::Requirement
include Comparable
attr_reader :requirements
OPS = {
"=" => lambda { |v, r| v == r },
"!=" => lambda { |v, r| v != r },
">" => lambda { |v, r| v > r },
"<" => lambda { |v, r| v < r },
">=" => lambda { |v, r| v >= r },
"<=" => lambda { |v, r| v <= r },
"~>" => lambda { |v, r| v >= r && v < r.bump }
}
OP_RE = /#{OPS.keys.map{ |k| Regexp.quote k }.join '|'}/o
##
# Factory method to create a Gem::Requirement object. Input may be a
# Version, a String, or nil. Intended to simplify client code.
#
# If the input is "weird", the default version requirement is returned.
def self.create(input)
case input
when Gem::Requirement then
input
when Gem::Version, Array then
new input
else
if input.respond_to? :to_str then
self.new [input.to_str]
else
self.default
end
end
end
##
# A default "version requirement" can surely _only_ be '>= 0'.
#--
# This comment once said:
#
# "A default "version requirement" can surely _only_ be '> 0'."
def self.default
self.new ['>= 0']
end
##
# Constructs a Requirement from +requirements+ which can be a String, a
# Gem::Version, or an Array of those. See parse for details on the
# formatting of requirement strings.
def initialize(requirements)
@requirements = case requirements
when Array then
requirements.map do |requirement|
parse(requirement)
end
else
[parse(requirements)]
end
@version = nil # Avoid warnings.
end
##
# Marshal raw requirements, rather than the full object
def marshal_dump # :nodoc:
[@requirements]
end
##
# Load custom marshal format
def marshal_load(array) # :nodoc:
@requirements = array[0]
@version = nil
end
def to_s # :nodoc:
as_list.join(", ")
end
def as_list
normalize
@requirements.collect { |req|
"#{req[0]} #{req[1]}"
}
end
def normalize
return if not defined? @version or @version.nil?
@requirements = [parse(@version)]
@nums = nil
@version = nil
@op = nil
end
##
# True if this requirement satisfied by the Gem::Version +version+.
def satisfied_by?(version)
normalize
@requirements.all? { |op, rv| satisfy?(op, version, rv) }
end
##
# Is "+version+ +op+ +required_version+" satisfied?
def satisfy?(op, version, required_version)
OPS[op].call(version, required_version)
end
##
# Parse the version requirement obj returning the operator and version.
#
# The requirement can be a String or a Gem::Version. A String can be an
# operator (<, <=, =, =>, >, !=, ~>), a version number, or both, operator
# first.
def parse(obj)
case obj
when /^\s*(#{OP_RE})\s*([0-9.]+)\s*$/o then
[$1, Gem::Version.new($2)]
when /^\s*([0-9.]+)\s*$/ then
['=', Gem::Version.new($1)]
when /^\s*(#{OP_RE})\s*$/o then
[$1, Gem::Version.new('0')]
when Gem::Version then
['=', obj]
else
fail ArgumentError, "Illformed requirement [#{obj.inspect}]"
end
end
def <=>(other) # :nodoc:
to_s <=> other.to_s
end
def hash # :nodoc:
to_s.hash
end
end
|