Class: RPM::Version

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

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
69
# 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") if not 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

Class Method Details

.parse_evr(evr) ⇒ Array

Parses a “epoch:version-release” string

Returns:

  • (Array)

    tuple [epoch, version, release]

Raises:

  • (ArgumentError)


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

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

  idx = version.rindex(?-)
  version, release = version[0..idx-1], version[idx+1..-1] if idx
  
  idx = version.index(/\D/)
  if (idx && version[idx] == ?:)
    epoch = version[0..idx-1]
    version = version[idx+1..-1]
  end
  return 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.



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

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

#eString

Returns the epoch component or nil

Returns:

  • (String)

    the epoch component or nil



84
85
86
# File 'lib/rpm/version.rb', line 84

def e
  @e
end

#hashString

Hash based on the version content

Returns:

  • (String)


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

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



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

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



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

def older?(other)
  self < other
end

#rString

Returns the release component or nil

Returns:

  • (String)

    the release component or nil



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

def r
  @r
end

#to_sObject

Alias for to_vr

See Also:



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

def to_s
  to_vr
end

#to_vrString

Note:

The epoch is not included

String representation in the form “v-r”

Returns:

  • (String)


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

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

#to_vre(opts = {}) ⇒ String

Note:

The epoch is included if present

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

Returns:

  • (String)


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

def to_vre(opts={})
  vr = to_vr
  vre = @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)


148
149
150
151
# File 'lib/rpm/version.rb', line 148

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

#vString

Returns the version component

Returns:

  • (String)

    the version component



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

def v
  @v
end