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



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

Parsed version object for the string.

Returns:

  • (Gem::Version)

Since:

  • 13.2



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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



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.

Parameters:

  • other (Integer)

Returns:

  • (String)

Since:

  • 13.2



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.

Parameters:

  • other (String)

Returns:

  • (String)

Since:

  • 13.2



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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



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.

Parameters:

  • other (Object)

Returns:

  • (Integer)

Since:

  • 13.2



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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



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.

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



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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



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

Parameters:

  • other (Object)

Returns:

  • (Boolean)

Since:

  • 13.2



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.

Returns:

  • (Boolean)

Since:

  • 13.2



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

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