Class: Gem::Requirement
- Inherits:
-
Object
- Object
- Gem::Requirement
- Includes:
- Comparable
- Defined in:
- lib/rubygems/requirement.rb
Constant Summary collapse
- OPS =
:nodoc:
{ #:nodoc: "=" => 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.release < r.bump } }
- PATTERN =
/\A\s*(#{quoted})?\s*(#{Gem::Version::VERSION_PATTERN})\s*\z/
Instance Attribute Summary collapse
-
#requirements ⇒ Object
readonly
An array of requirement pairs.
Class Method Summary collapse
-
.create(input) ⇒ Object
Factory method to create a Gem::Requirement object.
-
.default ⇒ Object
A default “version requirement” can surely only be ‘>= 0’.
-
.parse(obj) ⇒ Object
Parse
obj
, returning an[op, version]
pair.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
:nodoc:.
-
#as_list ⇒ Object
:nodoc:.
-
#hash ⇒ Object
:nodoc:.
-
#init_with(coder) ⇒ Object
:nodoc:.
-
#initialize(*requirements) ⇒ Requirement
constructor
Constructs a requirement from
requirements
. -
#marshal_dump ⇒ Object
:nodoc:.
-
#marshal_load(array) ⇒ Object
:nodoc:.
- #none? ⇒ Boolean
- #prerelease? ⇒ Boolean
-
#pretty_print(q) ⇒ Object
:nodoc:.
-
#satisfied_by?(version) ⇒ Boolean
(also: #===, #=~)
True if
version
satisfies this Requirement. -
#specific? ⇒ Boolean
True if the requirement will not always match the latest version.
-
#to_s ⇒ Object
:nodoc:.
-
#yaml_initialize(tag, vals) ⇒ Object
:nodoc:.
Constructor Details
#initialize(*requirements) ⇒ Requirement
Constructs a requirement from requirements
. Requirements can be Strings, Gem::Versions, or Arrays of those. nil
and duplicate requirements are ignored. An empty set of requirements
is the same as ">= 0"
.
99 100 101 102 103 104 105 106 107 |
# File 'lib/rubygems/requirement.rb', line 99 def initialize *requirements requirements = requirements.flatten requirements.compact! requirements.uniq! requirements << ">= 0" if requirements.empty? @none = (requirements == ">= 0") @requirements = requirements.map! { |r| self.class.parse r } end |
Instance Attribute Details
#requirements ⇒ Object (readonly)
An array of requirement pairs. The first element of the pair is the op, and the second is the Gem::Version.
91 92 93 |
# File 'lib/rubygems/requirement.rb', line 91 def requirements @requirements end |
Class Method Details
.create(input) ⇒ Object
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.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rubygems/requirement.rb', line 39 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 new [input.to_str] else default end end end |
.default ⇒ Object
A default “version requirement” can surely only be ‘>= 0’. – This comment once said:
“A default ”version requirement“ can surely only be ‘> 0’.”
61 62 63 |
# File 'lib/rubygems/requirement.rb', line 61 def self.default new '>= 0' end |
.parse(obj) ⇒ Object
Parse obj
, returning an [op, version]
pair. obj
can be a String or a Gem::Version.
If obj
is a String, it can be either a full requirement specification, like ">= 1.2"
, or a simple version number, like "1.2"
.
parse("> 1.0") # => [">", "1.0"]
parse("1.0") # => ["=", "1.0"]
parse(Gem::Version.new("1.0")) # => ["=, "1.0"]
77 78 79 80 81 82 83 84 85 |
# File 'lib/rubygems/requirement.rb', line 77 def self.parse obj return ["=", obj] if Gem::Version === obj unless PATTERN =~ obj.to_s raise ArgumentError, "Illformed requirement [#{obj.inspect}]" end [$1 || "=", Gem::Version.new($2)] end |
Instance Method Details
#<=>(other) ⇒ Object
:nodoc:
179 180 181 |
# File 'lib/rubygems/requirement.rb', line 179 def <=> other # :nodoc: to_s <=> other.to_s end |
#as_list ⇒ Object
:nodoc:
113 114 115 |
# File 'lib/rubygems/requirement.rb', line 113 def as_list # :nodoc: requirements.map { |op, version| "#{op} #{version}" }.sort end |
#hash ⇒ Object
:nodoc:
117 118 119 |
# File 'lib/rubygems/requirement.rb', line 117 def hash # :nodoc: requirements.hash end |
#init_with(coder) ⇒ Object
:nodoc:
141 142 143 |
# File 'lib/rubygems/requirement.rb', line 141 def init_with coder # :nodoc: yaml_initialize coder.tag, coder.map end |
#marshal_dump ⇒ Object
:nodoc:
121 122 123 124 125 |
# File 'lib/rubygems/requirement.rb', line 121 def marshal_dump # :nodoc: fix_syck_default_key_in_requirements [@requirements] end |
#marshal_load(array) ⇒ Object
:nodoc:
127 128 129 130 131 |
# File 'lib/rubygems/requirement.rb', line 127 def marshal_load array # :nodoc: @requirements = array[0] fix_syck_default_key_in_requirements end |
#none? ⇒ Boolean
109 110 111 |
# File 'lib/rubygems/requirement.rb', line 109 def none? @none ||= (to_s == ">= 0") end |
#prerelease? ⇒ Boolean
145 146 147 |
# File 'lib/rubygems/requirement.rb', line 145 def prerelease? requirements.any? { |r| r.last.prerelease? } end |
#pretty_print(q) ⇒ Object
:nodoc:
149 150 151 152 153 |
# File 'lib/rubygems/requirement.rb', line 149 def pretty_print q # :nodoc: q.group 1, 'Gem::Requirement.new(', ')' do q.pp as_list end end |
#satisfied_by?(version) ⇒ Boolean Also known as: ===, =~
True if version
satisfies this Requirement.
158 159 160 161 |
# File 'lib/rubygems/requirement.rb', line 158 def satisfied_by? version # #28965: syck has a bug with unquoted '=' YAML.loading as YAML::DefaultKey requirements.all? { |op, rv| (OPS[op] || OPS["="]).call version, rv } end |
#specific? ⇒ Boolean
True if the requirement will not always match the latest version.
169 170 171 172 173 |
# File 'lib/rubygems/requirement.rb', line 169 def specific? return true if @requirements.length > 1 # GIGO, > 1, > 2 is silly not %w[> >=].include? @requirements.first.first # grab the operator end |
#to_s ⇒ Object
:nodoc:
175 176 177 |
# File 'lib/rubygems/requirement.rb', line 175 def to_s # :nodoc: as_list.join ", " end |
#yaml_initialize(tag, vals) ⇒ Object
:nodoc:
133 134 135 136 137 138 139 |
# File 'lib/rubygems/requirement.rb', line 133 def yaml_initialize(tag, vals) # :nodoc: vals.each do |ivar, val| instance_variable_set "@#{ivar}", val end fix_syck_default_key_in_requirements end |