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.
29 30 31 32 33 34 35 36 37 |
# File 'lib/chef-utils/version_string.rb', line 29 def initialize(val) val = "" unless 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.
24 25 26 |
# File 'lib/chef-utils/version_string.rb', line 24 def parsed_version @parsed_version end |
Instance Method Details
#!=(other) ⇒ Boolean
Compat wrapper for != based on <=>.
92 93 94 |
# File 'lib/chef-utils/version_string.rb', line 92 def !=(other) (self <=> other) != 0 end |
#*(other) ⇒ String
Compat wrapper for * to behave like a normal String.
53 54 55 |
# File 'lib/chef-utils/version_string.rb', line 53 def *(other) to_s * other end |
#+(other) ⇒ String
Compat wrapper for + to behave like a normal String.
45 46 47 |
# File 'lib/chef-utils/version_string.rb', line 45 def +(other) to_s + other end |
#<(other) ⇒ Boolean
Compat wrapper for < based on <=>.
100 101 102 |
# File 'lib/chef-utils/version_string.rb', line 100 def <(other) (self <=> other) < 0 end |
#<=(other) ⇒ Boolean
Compat wrapper for <= based on <=>.
108 109 110 |
# File 'lib/chef-utils/version_string.rb', line 108 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.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/chef-utils/version_string.rb', line 65 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 <=>.
84 85 86 |
# File 'lib/chef-utils/version_string.rb', line 84 def ==(other) (self <=> other) == 0 end |
#=~(other) ⇒ Boolean
Matching operator to support checking against a requirement string.
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/chef-utils/version_string.rb', line 138 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 <=>.
116 117 118 |
# File 'lib/chef-utils/version_string.rb', line 116 def >(other) (self <=> other) > 0 end |
#>=(other) ⇒ Boolean
Compat wrapper for >= based on <=>.
124 125 126 |
# File 'lib/chef-utils/version_string.rb', line 124 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.
155 156 157 |
# File 'lib/chef-utils/version_string.rb', line 155 def satisfies?(*constraints) Gem::Requirement.new(*constraints).satisfied_by?(@parsed_version) end |