Class: ChefUtils::VersionString
- Inherits:
-
String
- Object
- String
- ChefUtils::VersionString
- Defined in:
- lib/chef-utils/version_string.rb
Overview
String-like object for version strings.
Instance Attribute Summary collapse
-
#parsed_version ⇒ Gem::Version
readonly
Parsed version object for the string.
Compat wrappers for String collapse
-
#*(other) ⇒ String
Compat wrapper for * to behave like a normal String.
-
#+(other) ⇒ String
Compat wrapper for + to behave like a normal String.
Comparison operators collapse
-
#!=(other) ⇒ Boolean
Compat wrapper for != based on <=>.
-
#<(other) ⇒ Boolean
Compat wrapper for < based on <=>.
-
#<=(other) ⇒ Boolean
Compat wrapper for <= based on <=>.
-
#<=>(other) ⇒ Integer
Compare a VersionString to an object.
-
#==(other) ⇒ Boolean
Compat wrapper for == based on <=>.
-
#>(other) ⇒ Boolean
Compat wrapper for > based on <=>.
-
#>=(other) ⇒ Boolean
Compat wrapper for >= based on <=>.
Matching operators collapse
-
#=~(other) ⇒ Boolean
Matching operator to support checking against a requirement string.
-
#satisfies?(*constraints) ⇒ Boolean
private
Back-compat API for chef-sugar.
Instance Method Summary collapse
-
#initialize(val) ⇒ VersionString
constructor
Create a new VersionString from an input String.
Constructor Details
#initialize(val) ⇒ VersionString
Create a new VersionString from an input String.
30 31 32 33 34 35 36 37 38 |
# File 'lib/chef-utils/version_string.rb', line 30 def initialize(val) val ||= "" super(val) begin @parsed_version = ::Gem::Version.create(self) rescue ArgumentError @parsed_version = nil end end |
Instance Attribute Details
#parsed_version ⇒ Gem::Version (readonly)
Parsed version object for the string.
25 26 27 |
# File 'lib/chef-utils/version_string.rb', line 25 def parsed_version @parsed_version end |
Instance Method Details
#!=(other) ⇒ Boolean
Compat wrapper for != based on <=>.
93 94 95 |
# File 'lib/chef-utils/version_string.rb', line 93 def !=(other) (self <=> other) != 0 end |
#*(other) ⇒ String
Compat wrapper for * to behave like a normal String.
54 55 56 |
# File 'lib/chef-utils/version_string.rb', line 54 def *(other) to_s * other end |
#+(other) ⇒ String
Compat wrapper for + to behave like a normal String.
46 47 48 |
# File 'lib/chef-utils/version_string.rb', line 46 def +(other) to_s + other end |
#<(other) ⇒ Boolean
Compat wrapper for < based on <=>.
101 102 103 |
# File 'lib/chef-utils/version_string.rb', line 101 def <(other) (self <=> other) < 0 end |
#<=(other) ⇒ Boolean
Compat wrapper for <= based on <=>.
109 110 111 |
# File 'lib/chef-utils/version_string.rb', line 109 def <=(other) (self <=> other) < 1 end |
#<=>(other) ⇒ Integer
Compare a VersionString to an object. If compared to another VersionString then sort like ‘Gem::Version`, otherwise try to treat the other object as a version but fall back to normal string comparison.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/chef-utils/version_string.rb', line 66 def <=>(other) other_ver = case other when VersionString other.parsed_version else begin Gem::Version.create(other.to_s) rescue ArgumentError # Comparing to a string that isn't a version. return super end end parsed_version <=> other_ver end |
#==(other) ⇒ Boolean
Compat wrapper for == based on <=>.
85 86 87 |
# File 'lib/chef-utils/version_string.rb', line 85 def ==(other) (self <=> other) == 0 end |
#=~(other) ⇒ Boolean
Matching operator to support checking against a requirement string.
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/chef-utils/version_string.rb', line 139 def =~(other) case other when Regexp super else begin Gem::Requirement.create(other) =~ parsed_version rescue ArgumentError # one side of the comparison wasn't parsable super end end end |
#>(other) ⇒ Boolean
Compat wrapper for > based on <=>.
117 118 119 |
# File 'lib/chef-utils/version_string.rb', line 117 def >(other) (self <=> other) > 0 end |
#>=(other) ⇒ Boolean
Compat wrapper for >= based on <=>.
125 126 127 |
# File 'lib/chef-utils/version_string.rb', line 125 def >=(other) (self <=> other) > -1 end |
#satisfies?(*constraints) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Back-compat API for chef-sugar. The other APIs are preferable.
156 157 158 |
# File 'lib/chef-utils/version_string.rb', line 156 def satisfies?(*constraints) Gem::Requirement.new(*constraints).satisfied_by?(@parsed_version) end |