Class: Calabash::Android::Version
- Inherits:
-
Object
- Object
- Calabash::Android::Version
- Defined in:
- lib/calabash-android/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 ‘<major>.<minor>.<patch>`.
TODO Expand to handle versions with more than 1 “.” and no “.” ^ Needs to handle arbitrary versions from Info.plists. In particular it
needs to handle a unix timestamp - found the DeviceAgent-Runner.app.
Instance Attribute Summary collapse
-
#major ⇒ Integer
The major version.
-
#minor ⇒ Integer
The minor version.
-
#patch ⇒ Integer
The patch version.
-
#pre ⇒ Boolean
True if this is a pre-release version.
-
#pre_version ⇒ Integer
If this is a pre-release version, returns the pre-release version; otherwise this is nil.
Class Method Summary collapse
-
.compare(a, b) ⇒ Integer
Compare version ‘a` to version `b`.
Instance Method Summary collapse
-
#!=(other) ⇒ Boolean
Compare this version to another for inequality.
-
#<(other) ⇒ Boolean
Is this version less-than another version?.
-
#<=(other) ⇒ Boolean
Is this version less-than or equal to another version?.
-
#<=>(other) ⇒ Integer
Compare version ‘a` to version `b`.
-
#==(other) ⇒ Boolean
Compare this version to another for equality.
-
#>(other) ⇒ Boolean
Is this version greater-than another version?.
-
#>=(other) ⇒ Boolean
Is this version greater-than or equal to another version?.
-
#eql?(other) ⇒ Boolean
Compare this version to another for object equality.
-
#hash ⇒ Object
The hash method for this instance.
-
#initialize(version) ⇒ Version
constructor
Creates a new Version instance with all the attributes set.
- #inspect ⇒ Object
-
#to_s ⇒ String
Returns an string representation of this version.
Constructor Details
#initialize(version) ⇒ Version
Creates a new Version instance with all the attributes set.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/calabash-android/version.rb', line 65 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
#major ⇒ Integer
Returns the major version.
26 27 28 |
# File 'lib/calabash-android/version.rb', line 26 def major @major end |
#minor ⇒ Integer
Returns the minor version.
30 31 32 |
# File 'lib/calabash-android/version.rb', line 30 def minor @minor end |
#patch ⇒ Integer
Returns the patch version.
34 35 36 |
# File 'lib/calabash-android/version.rb', line 34 def patch @patch end |
#pre ⇒ Boolean
Returns true if this is a pre-release version.
38 39 40 |
# File 'lib/calabash-android/version.rb', line 38 def pre @pre end |
#pre_version ⇒ Integer
Returns if this is a pre-release version, returns the pre-release version; otherwise this is nil.
43 44 45 |
# File 'lib/calabash-android/version.rb', line 43 def pre_version @pre_version end |
Class Method Details
.compare(a, b) ⇒ Integer
Compare version ‘a` to version `b`.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/calabash-android/version.rb', line 179 def self.compare(a, b) if a.major != b.major return a.major.to_i > b.major.to_i ? 1 : -1 end a_minor = a.minor ? a.minor.to_i : 0 b_minor = b.minor ? b.minor.to_i : 0 if a_minor != b_minor return a_minor > b_minor.to_i ? 1 : -1 end a_patch = a.patch ? a.patch.to_i : 0 b_patch = b.patch ? b.patch.to_i : 0 if a_patch != b_patch return a_patch.to_i > b_patch.to_i ? 1 : -1 end return -1 if a.pre && (!a.pre_version) && b.pre_version return 1 if a.pre_version && b.pre && (!b.pre_version) return -1 if a.pre && (!b.pre) return 1 if (!a.pre) && b.pre return -1 if a.pre_version && (!b.pre_version) return 1 if (!a.pre_version) && 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.
127 128 129 |
# File 'lib/calabash-android/version.rb', line 127 def != (other) Version.compare(self, other) != 0 end |
#<(other) ⇒ Boolean
Is this version less-than another version?
134 135 136 |
# File 'lib/calabash-android/version.rb', line 134 def < (other) Version.compare(self, other) < 0 end |
#<=(other) ⇒ Boolean
Is this version less-than or equal to another version?
148 149 150 |
# File 'lib/calabash-android/version.rb', line 148 def <= (other) Version.compare(self, other) <= 0 end |
#<=>(other) ⇒ Integer
Compare version ‘a` to version `b`.
167 168 169 |
# File 'lib/calabash-android/version.rb', line 167 def <=> (other) Version.compare(self, other) end |
#==(other) ⇒ Boolean
Compare this version to another for equality.
120 121 122 |
# File 'lib/calabash-android/version.rb', line 120 def == (other) Version.compare(self, other) == 0 end |
#>(other) ⇒ Boolean
Is this version greater-than another version?
141 142 143 |
# File 'lib/calabash-android/version.rb', line 141 def > (other) Version.compare(self, other) > 0 end |
#>=(other) ⇒ Boolean
Is this version greater-than or equal to another version?
155 156 157 |
# File 'lib/calabash-android/version.rb', line 155 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.
100 101 102 |
# File 'lib/calabash-android/version.rb', line 100 def eql?(other) hash == other.hash end |
#hash ⇒ Object
The hash method for this instance.
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/calabash-android/version.rb', line 105 def hash str = [major, minor, patch].map do |str| str ? str : "0" end.join(".") if pre str = "#{str}.#{pre}" end str.hash end |
#inspect ⇒ Object
93 94 95 |
# File 'lib/calabash-android/version.rb', line 93 def inspect "#<Version #{to_s}>" end |
#to_s ⇒ String
Returns an string representation of this version.
87 88 89 90 91 |
# File 'lib/calabash-android/version.rb', line 87 def to_s str = [major, minor, patch].compact.join('.') str = "#{str}.#{pre}" if pre str end |