Class: ChefUtils::VersionString

Inherits:
String
  • Object
show all
Defined in:
lib/chef-utils/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

Constructor Details

#initialize(val) ⇒ VersionString

Create a new VersionString from an input String.

Parameters:

  • val (String)

    Version string to parse.

Since:

  • 13.2



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_versionGem::Version (readonly)

Parsed version object for the string.

Returns:

  • (Gem::Version)

Since:

  • 13.2



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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



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.

Parameters:

  • other (Integer)

Returns:

  • (String)

Since:

  • 13.2



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.

Parameters:

  • other (String)

Returns:

  • (String)

Since:

  • 13.2



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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



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.

Parameters:

  • other (Object)

Returns:

  • (Integer)

Since:

  • 13.2



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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



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.

Examples:

Match against a Regexp

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

Match against a requirement

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

Parameters:

  • other (Regexp, String)

Returns:

  • (Boolean)

Since:

  • 13.2



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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



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.

Returns:

  • (Boolean)

Since:

  • 13.2



156
157
158
# File 'lib/chef-utils/version_string.rb', line 156

def satisfies?(*constraints)
  Gem::Requirement.new(*constraints).satisfied_by?(@parsed_version)
end