Class: Chef::VersionString

Inherits:
String
  • Object
show all
Defined in:
lib/chef/version_string.rb

Overview

String-like object for version strings.

Since:

  • 13.2

Instance Attribute Summary collapse

Compat wrappers for String collapse

Comparison operators collapse

Matching operators collapse

Instance Method Summary collapse

Methods inherited from String

#to_wstring

Methods included from Mixin::WideString

#utf8_to_wide, #wide_to_utf8, #wstring

Methods included from Shell::Extensions::String

#on_off_to_bool

Constructor Details

#initialize(val) ⇒ VersionString

Create a new VersionString from an input String.

Parameters:

  • val (String)

    Version string to parse.

Since:

  • 13.2



29
30
31
32
# File 'lib/chef/version_string.rb', line 29

def initialize(val)
  super
  @parsed_version = ::Gem::Version.create(self)
end

Instance Attribute Details

#parsed_versionGem::Version (readonly)

Parsed version object for the string.

Returns:

  • (Gem::Version)

Since:

  • 13.2



24
25
26
# File 'lib/chef/version_string.rb', line 24

def parsed_version
  @parsed_version
end

Instance Method Details

#!=(other) ⇒ Boolean

Compat wrapper for != based on <=>.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



87
88
89
# File 'lib/chef/version_string.rb', line 87

def !=(other)
  (self <=> other) != 0
end

#*(other) ⇒ String

Compat wrapper for * to behave like a normal String.

Parameters:

  • other (Integer)

Returns:

Since:

  • 13.2



48
49
50
# File 'lib/chef/version_string.rb', line 48

def *(other)
  to_s * other
end

#+(other) ⇒ String

Compat wrapper for + to behave like a normal String.

Parameters:

Returns:

Since:

  • 13.2



40
41
42
# File 'lib/chef/version_string.rb', line 40

def +(other)
  to_s + other
end

#<(other) ⇒ Boolean

Compat wrapper for < based on <=>.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



95
96
97
# File 'lib/chef/version_string.rb', line 95

def <(other)
  (self <=> other) < 0
end

#<=(other) ⇒ Boolean

Compat wrapper for <= based on <=>.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



103
104
105
# File 'lib/chef/version_string.rb', line 103

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.

Parameters:

  • other (Object)

Returns:

  • (Integer)

Since:

  • 13.2



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chef/version_string.rb', line 60

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 <=>.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



79
80
81
# File 'lib/chef/version_string.rb', line 79

def ==(other)
  (self <=> other) == 0
end

#=~(other) ⇒ Boolean

Matching operator to support checking against a requirement string.

Examples:

Match against a Regexp

Chef::VersionString.new('1.0.0') =~ /^1/

Match against a requirement

Chef::VersionString.new('1.0.0') =~ '~> 1.0'

Parameters:

Returns:

  • (Boolean)

Since:

  • 13.2



133
134
135
136
137
138
139
140
# File 'lib/chef/version_string.rb', line 133

def =~(other)
  case other
  when Regexp
    super
  else
    Gem::Requirement.create(other) =~ parsed_version
  end
end

#>(other) ⇒ Boolean

Compat wrapper for > based on <=>.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



111
112
113
# File 'lib/chef/version_string.rb', line 111

def >(other)
  (self <=> other) > 0
end

#>=(other) ⇒ Boolean

Compat wrapper for >= based on <=>.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



119
120
121
# File 'lib/chef/version_string.rb', line 119

def >=(other)
  (self <=> other) > -1
end