Class: Herve::Version

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

Overview

Version handling

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#segmentsObject (readonly)

Version segments



14
15
16
# File 'lib/herve/version.rb', line 14

def segments
  @segments
end

#versionObject (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_segmentsObject

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

Returns:

  • (Boolean)


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

#inspectObject



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

Returns:

  • (Boolean)


27
28
29
# File 'lib/herve/version.rb', line 27

def prerelease?
  @segments.any? { |s| s.is_a?(String) }
end

#releaseObject

Give a new version from self without prerelease part



53
54
55
56
57
58
59
# File 'lib/herve/version.rb', line 53

def release
  pre_index = pre_index_in_segments
  return clone if pre_index.nil?

  new_segments = @segments[0...pre_index]
  Version.new(new_segments.join("."))
end