Class: RunLoop::Version

Inherits:
Object
  • Object
show all
Defined in:
lib/run_loop/version.rb

Overview

A model of a software release version that can be used to compare two versions.

Calabash and RunLoop try very hard to comply with Semantic Versioning rules. However, the semantic versioning spec is incompatible with RubyGem's patterns for pre-release gems.

> “But returning to the practical: No release version of SemVer is compatible with Rubygems.” - _David Kellum_

Calabash and RunLoop version numbers will be in the form `<

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

Creates a new Version instance with all the attributes set.

Examples:

version = Version.new(0.10.1)
version.major       => 0
version.minor       => 10
version.patch       => 1
version.pre         => false
version.pre_version => nil
version = Version.new(1.6.3.pre5)
version.major       => 1
version.minor       => 6
version.patch       => 3
version.pre         => true
version.pre_version => 5

Parameters:

  • version (String)

    the version string to parse.

Raises:

  • (ArgumentError)

    if version is not in the form 5, 6.1, 7.1.2, 8.2.3.pre1


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/run_loop/version.rb', line 59

def initialize(version)
  tokens = version.strip.split('.')
  count = tokens.count
  if tokens.empty?
    raise ArgumentError, "expected '#{version}' to be like 5, 6.1, 7.1.2, 8.2.3.pre1"
  end

  if count < 4 and tokens.any? { |elm| elm =~ /\D/ }
    raise ArgumentError, "expected '#{version}' to be like 5, 6.1, 7.1.2, 8.2.3.pre1"
  end

  if count == 4
    @pre = tokens[3]
    pre_tokens = @pre.scan(/\D+|\d+/)
    @pre_version = pre_tokens[1].to_i if pre_tokens.count == 2
  end

  @major, @minor, @patch = version.split('.').map(&:to_i)
end

Instance Attribute Details

#majorInteger

Returns the major version.

Returns:

  • (Integer)

    the major version


20
21
22
# File 'lib/run_loop/version.rb', line 20

def major
  @major
end

#minorInteger

Returns the minor version.

Returns:

  • (Integer)

    the minor version


24
25
26
# File 'lib/run_loop/version.rb', line 24

def minor
  @minor
end

#patchInteger

Returns the patch version.

Returns:

  • (Integer)

    the patch version


28
29
30
# File 'lib/run_loop/version.rb', line 28

def patch
  @patch
end

#preBoolean

Returns true if this is a pre-release version.

Returns:

  • (Boolean)

    true if this is a pre-release version


32
33
34
# File 'lib/run_loop/version.rb', line 32

def pre
  @pre
end

#pre_versionInteger

Returns if this is a pre-release version, returns the pre-release version; otherwise this is nil.

Returns:

  • (Integer)

    if this is a pre-release version, returns the pre-release version; otherwise this is nil


37
38
39
# File 'lib/run_loop/version.rb', line 37

def pre_version
  @pre_version
end

Class Method Details

.compare(a, b) ⇒ Integer

Compare version `a` to version `b`.

Examples:

compare Version.new(0.10.0), Version.new(0.9.0)  =>  1
compare Version.new(0.9.0),  Version.new(0.10.0) => -1
compare Version.new(0.9.0),  Version.new(0.9.0)  =>  0

Returns:

  • (Integer)

    an integer `(-1, 1)`


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/run_loop/version.rb', line 153

def self.compare(a, b)

  if a.major != b.major
    return a.major > b.major ? 1 : -1
  end

  if a.minor != b.minor
    return a.minor.to_i  > b.minor.to_i ? 1 : -1
  end

  if a.patch != b.patch
    return a.patch.to_i > b.patch.to_i ? 1 : -1
  end

  return -1 if a.pre and (not a.pre_version) and b.pre_version
  return 1 if a.pre_version and b.pre and (not b.pre_version)

  return -1 if a.pre and (not b.pre)
  return 1 if (not a.pre) and b.pre

  return -1 if a.pre_version and (not b.pre_version)
  return 1 if (not a.pre_version) and b.pre_version

  if a.pre_version != b.pre_version
    return a.pre_version.to_i > b.pre_version.to_i ? 1 : -1
  end
  0
end

Instance Method Details

#!=(other) ⇒ Boolean

Compare this version to another for inequality.

Parameters:

  • other (Version)

    the version to compare against

Returns:

  • (Boolean)

    true if this Version is not the same as `other`


113
114
115
# File 'lib/run_loop/version.rb', line 113

def != (other)
  Version.compare(self, other) != 0
end

#<(other) ⇒ Boolean

Is this version less-than another version?

Parameters:

  • other (Version)

    the version to compare against

Returns:

  • (Boolean)

    true if this Version is less-than `other`


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

def < (other)
  Version.compare(self, other) < 0
end

#<=(other) ⇒ Boolean

Is this version less-than or equal to another version?

Parameters:

  • other (Version)

    the version to compare against

Returns:

  • (Boolean)

    true if this Version is less-than or equal `other`


134
135
136
# File 'lib/run_loop/version.rb', line 134

def <= (other)
  Version.compare(self, other) <= 0
end

#==(other) ⇒ Boolean

Compare this version to another for equality.

Parameters:

  • other (Version)

    the version to compare against

Returns:

  • (Boolean)

    true if this Version is the same as `other`


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

def == (other)
  Version.compare(self, other) == 0
end

#>(other) ⇒ Boolean

Is this version greater-than another version?

Parameters:

  • other (Version)

    the version to compare against

Returns:

  • (Boolean)

    true if this Version is greater-than `other`


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

def > (other)
  Version.compare(self, other) > 0
end

#>=(other) ⇒ Boolean

Is this version greater-than or equal to another version?

Parameters:

  • other (Version)

    the version to compare against

Returns:

  • (Boolean)

    true if this Version is greater-than or equal `other`


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

def >= (other)
  Version.compare(self, other) >= 0
end

#eql?(other) ⇒ Boolean

Compare this version to another for object equality. This allows Version instances to be used as Hash keys.

Parameters:

  • other (Version)

    the version to compare against.

Returns:

  • (Boolean)

94
95
96
# File 'lib/run_loop/version.rb', line 94

def eql?(other)
  hash == other.hash
end

#hashObject

The hash method for this instance.


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

def hash
  to_s.hash
end

#inspectObject


87
88
89
# File 'lib/run_loop/version.rb', line 87

def inspect
  "#<Version #{to_s}>"
end

#to_sString

Returns an string representation of this version.