Class: SemverCheck::Compare

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

Overview

Class to compare SemVer strings

Constant Summary collapse

SEMVER_PATTERN =
/\A
  (?<semver>
    (?<major>0|[1-9]\d*)
    \.
    (?<minor>0|[1-9]\d*)
    \.
    (?<patch>0|[1-9]\d*)
    (?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)
      (?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)
    )?
    (?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?
  )
\Z/x
MAPPING_TABLE =
[
  %w[n 1],
  %w[sn 1],
  %w[ssn 1],
  %w[o -1],
  %w[so -1],
  %w[sso -1],
  %w[sss 0]
].freeze
ORDER =
%i[major minor patch prerelease].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Compare

Returns a new instance of Compare.



39
40
41
# File 'lib/semver_check.rb', line 39

def initialize(version)
  @version = prepare_version(version)
end

Instance Attribute Details

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#!=(other) ⇒ Object



49
50
51
# File 'lib/semver_check.rb', line 49

def !=(other)
  [-1, 1].include?(self.<=>(other))
end

#<=>(other) ⇒ Object



43
44
45
46
47
# File 'lib/semver_check.rb', line 43

def <=>(other)
  return nil unless other.is_a? Compare

  comparison(other)
end

#to_sObject



53
54
55
# File 'lib/semver_check.rb', line 53

def to_s
  @version[:semver]
end