Class: Herve::Version
- Inherits:
-
Object
- Object
- Herve::Version
- Includes:
- Comparable
- Defined in:
- lib/herve/version.rb
Overview
Version handling
Instance Attribute Summary collapse
-
#segments ⇒ Object
readonly
Version segments.
-
#version ⇒ Object
readonly
Full version, as String.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares this version with
otherreturning -1, 0, or 1 if the other version is larger, the same, or smaller than this one. -
#canonical_segments ⇒ Object
Get segments with trailing zeros in release and prerelease parts removed.
-
#eql?(other) ⇒ Boolean
eql? is strict equality.
-
#initialize(version) ⇒ Version
constructor
A new instance of Version.
- #initialize_copy(_other) ⇒ Object
- #inspect ⇒ Object
-
#prerelease? ⇒ Boolean
Say if self is a prerelase.
-
#release ⇒ Object
Give a new version from self without prerelease part.
Constructor Details
#initialize(version) ⇒ Version
Returns a new instance of Version.
16 17 18 19 |
# File 'lib/herve/version.rb', line 16 def initialize(version) @version = normalized = normalize_version(version) @segments = parse_segments(normalized) end |
Instance Attribute Details
#segments ⇒ Object (readonly)
Version segments
14 15 16 |
# File 'lib/herve/version.rb', line 14 def segments @segments end |
#version ⇒ Object (readonly)
Full version, as String
12 13 14 |
# File 'lib/herve/version.rb', line 12 def version @version end |
Instance Method Details
#<=>(other) ⇒ Object
Compares this version with other returning -1, 0, or 1 if the
other version is larger, the same, or smaller than this
one. Attempts to compare to something that's not a
Herve::Version or a valid version String return nil.
70 71 72 73 74 75 76 77 78 |
# File 'lib/herve/version.rb', line 70 def <=>(other) begin return self <=> self.class.new(other) if other.is_a?(String) rescue VersionError # rubocop:disable Lint/SuppressedException end return nil unless other.is_a?(Version) compare_segments(canonical_segments, other.canonical_segments) end |
#canonical_segments ⇒ Object
Get segments with trailing zeros in release and prerelease parts removed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/herve/version.rb', line 32 def canonical_segments cseg = @segments.dup pre_index = pre_index_in_segments || cseg.size (pre_index - 1).downto(1) do |i| break unless cseg[i].zero? cseg.delete_at(i) end if pre_index < cseg.size (cseg.size - 1).downto(pre_index + 1) do |i| break unless cseg[i].zero? cseg.delete_at(i) end end cseg end |
#eql?(other) ⇒ Boolean
eql? is strict equality. Versions are eql? if, and only if, hey have same version, with same precision
62 63 64 |
# File 'lib/herve/version.rb', line 62 def eql?(other) (self.class == other.class) && (segments == other.segments) end |
#initialize_copy(_other) ⇒ Object
21 22 23 24 |
# File 'lib/herve/version.rb', line 21 def initialize_copy(_other) @version = @version.dup @segments = @segments.dup end |
#inspect ⇒ Object
80 81 82 |
# File 'lib/herve/version.rb', line 80 def inspect "#<#{self.class} #{version.inspect}>" end |
#prerelease? ⇒ Boolean
Say if self is a prerelase
27 28 29 |
# File 'lib/herve/version.rb', line 27 def prerelease? @segments.any? { |s| s.is_a?(String) } end |