Class: RPM::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rpm/version.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(vr, e = nil) ⇒ Version #new(v, r, e = nil) ⇒ Version

Returns a new instance of Version.

Examples:

RPM:: Version.new "1.0.0-3"
RPM:: Version.new "1.04"
RPM:: Version.new "1.0.0-3k", 1
RPM:: Version.new "2.0.3", "5k"

Overloads:

  • #new(vr, e = nil) ⇒ Version

    Creates a version object from a string representation

    Parameters:

    • vr (String)

      version and release in the form “v-r”

    • e (Number) (defaults to: nil)

      epoch

  • #new(v, r, e = nil) ⇒ Version

    Creates a version object from a string representation

    Parameters:

    • v (String)

      version

    • r (String)

      release

    • e (Number) (defaults to: nil)

      epoch



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rpm/version.rb', line 46

def initialize(*argv)
  case argv.size
  when 0
    raise(ArgumentError('wrong number of arguments (0 for 1..3)'))
  when 1
    RPM::Utils.check_type(argv[0], String)
    @e, @v, @r = RPM::Version.parse_evr(argv[0])
  when 2
    # (vr, e)
    RPM::Utils.check_type(argv[0], String)
    @e, @v, @r = RPM::Version.parse_evr(argv[0])
    raise(TypeError, 'illegal argument value') unless e.nil?
    @e = argv[1].to_i
  when 3
    RPM::Utils.check_type(argv[0], String)
    RPM::Utils.check_type(argv[1], String)
    @v = argv[0]
    @r = argv[1]
    @e = argv[2].to_i
  else
    raise(ArgumentError("too many arguments (#{args.size} for 1..3)"))
  end
end

Instance Attribute Details

#eString (readonly)

Returns the epoch component or nil.

Returns:

  • (String)

    the epoch component or nil



79
80
81
# File 'lib/rpm/version.rb', line 79

def e
  @e
end

#rString (readonly)

Returns the release component or nil.

Returns:

  • (String)

    the release component or nil



75
76
77
# File 'lib/rpm/version.rb', line 75

def r
  @r
end

#vString (readonly)

Returns the version component.

Returns:

  • (String)

    the version component



71
72
73
# File 'lib/rpm/version.rb', line 71

def v
  @v
end

Class Method Details

.parse_evr(evr) ⇒ Array

Parses a “epoch:version-release” string

Returns:

  • (Array)

    tuple [epoch, version, release]

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rpm/version.rb', line 8

def self.parse_evr(evr)
  raise ArgumentError, "version can't be nil" if evr.nil?
  version = evr
  epoch = nil
  release = nil

  idx = version.rindex('-')
  if idx
    release = version[idx + 1..-1]
    version = version[0..idx - 1]
  end

  idx = version.index(/\D/)
  if idx && version[idx] == ':'
    epoch = version[0..idx - 1]
    version = version[idx + 1..-1]
  end
  [epoch ? epoch.to_i : nil, version, release]
end

Instance Method Details

#<=>(other) ⇒ Number

Comparison between versions

Examples:

v1 = RPM::Version.new("3.0-0",1)
v2 = RPM::Version.new("3.1-0",1)
v1 <=> v2
=> -1

Parameters:

Returns:

  • (Number)

    -1 if other is greater than, 0 if other is equal to, and +1 if other is less than version.



92
93
94
95
# File 'lib/rpm/version.rb', line 92

def <=>(other)
  RPM::Utils.check_type(other, RPM::Version)
  RPM::C.rpmvercmp(to_vre_epoch_zero, other.to_vre_epoch_zero)
end

#hashString

Hash based on the version content

Returns:

  • (String)


132
133
134
135
136
# File 'lib/rpm/version.rb', line 132

def hash
  h = @e.nil? ? 0 : @e
  h = (h << 1) ^ @r.hash
  h = (h << 1) ^ @v.hash
end

#newer?(other) ⇒ Boolean

Returns true if the version is newer than other.

Parameters:

  • other (Version)

    Version to compare against

Returns:

  • (Boolean)

    true if the version is newer than other



99
100
101
# File 'lib/rpm/version.rb', line 99

def newer?(other)
  self > other
end

#older?(other) ⇒ Boolean

Returns true if the version is older than other.

Parameters:

  • other (Version)

    Version to compare against

Returns:

  • (Boolean)

    true if the version is older than other



105
106
107
# File 'lib/rpm/version.rb', line 105

def older?(other)
  self < other
end

#to_sObject

Alias for to_vr

See Also:



126
127
128
# File 'lib/rpm/version.rb', line 126

def to_s
  to_vr
end

#to_vrString

Note:

The epoch is not included

String representation in the form “v-r”

Returns:

  • (String)


112
113
114
# File 'lib/rpm/version.rb', line 112

def to_vr
  @r.nil? ? @v.to_s : "#{@v}-#{@r}"
end

#to_vre(_opts = {}) ⇒ String

Note:

The epoch is included if present

String representation in the form “e:v-r”

Returns:

  • (String)


119
120
121
122
# File 'lib/rpm/version.rb', line 119

def to_vre(_opts = {})
  vr = to_vr
  @e.nil? ? vr : "#{@e}:#{vr}"
end

#to_vre_epoch_zeroString

Note:

The epoch is included always. As 0 if not present

String representation in the form “e:v-r”

Returns:

  • (String)


141
142
143
144
# File 'lib/rpm/version.rb', line 141

def to_vre_epoch_zero
  vr = to_vr
  @e.nil? ? "0:#{vr}" : "#{@e}:#{vr}"
end