Class: StrongVersions::GemVersion
- Inherits:
-
Object
- Object
- StrongVersions::GemVersion
- Defined in:
- lib/strong_versions/gem_version.rb
Instance Method Summary collapse
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
-
#initialize(version) ⇒ GemVersion
constructor
A new instance of GemVersion.
- #missing? ⇒ Boolean
- #numeric ⇒ Object
- #suggestion ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #version_string ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(version) ⇒ GemVersion
Returns a new instance of GemVersion.
5 6 7 8 |
# File 'lib/strong_versions/gem_version.rb', line 5 def initialize(version) @version = normalize(version) || '' @parts = @version&.split('.') end |
Instance Method Details
#<(other) ⇒ Object
34 35 36 |
# File 'lib/strong_versions/gem_version.rb', line 34 def <(other) numeric < other.numeric end |
#<=(other) ⇒ Object
38 39 40 |
# File 'lib/strong_versions/gem_version.rb', line 38 def <=(other) numeric <= other.numeric end |
#>(other) ⇒ Object
42 43 44 |
# File 'lib/strong_versions/gem_version.rb', line 42 def >(other) numeric > other.numeric end |
#>=(other) ⇒ Object
46 47 48 |
# File 'lib/strong_versions/gem_version.rb', line 46 def >=(other) numeric >= other.numeric end |
#missing? ⇒ Boolean
66 67 68 69 70 71 |
# File 'lib/strong_versions/gem_version.rb', line 66 def missing? return false if stable? return false if unstable? true end |
#numeric ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/strong_versions/gem_version.rb', line 50 def numeric # Support extremely precise versions e.g. '1.2.3.4.5.6.7.8.9' components = @version.split('.').map(&:to_i) components += [0] * (10 - components.size) components.reverse.each_with_index.map do |component, index| component * 10.pow(index + 1) end.sum end |
#suggestion ⇒ Object
18 19 20 21 22 |
# File 'lib/strong_versions/gem_version.rb', line 18 def suggestion return '' if version_string.empty? "'~> #{version_string}'" end |
#to_s ⇒ Object
10 11 12 |
# File 'lib/strong_versions/gem_version.rb', line 10 def to_s @version end |
#valid? ⇒ Boolean
59 60 61 62 63 64 |
# File 'lib/strong_versions/gem_version.rb', line 59 def valid? return true if @version =~ /^[1-9][0-9]*\.\d+$/ # major.minor, e.g. "2.5" return true if @version =~ /^0\.\d+\.\d+$/ # 0.minor.patch, e.g. "0.1.8" false end |
#version_string ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/strong_versions/gem_version.rb', line 24 def version_string return '' unless standard? major, minor, patch = @parts return "#{major}.#{minor}" if stable? return "#{major}.#{minor}.#{patch}" if unstable? raise 'Unexpected condition met' end |
#zero? ⇒ Boolean
14 15 16 |
# File 'lib/strong_versions/gem_version.rb', line 14 def zero? @version == '0' end |