Class: XSemVer::PreRelease

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

Overview

Represents the pre-release portion of a SemVer string.

Constant Summary collapse

ONLY_DIGITS =
/\A\d+\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prerelease_string) ⇒ PreRelease

Returns a new instance of PreRelease.



15
16
17
# File 'lib/pre_release.rb', line 15

def initialize(prerelease_string)
  @ids = prerelease_string.split(".")
end

Instance Attribute Details

#idsObject (readonly)

Returns the value of attribute ids.



8
9
10
# File 'lib/pre_release.rb', line 8

def ids
  @ids
end

Instance Method Details

#<=>(other) ⇒ Object

The SemVer 2.0.0-rc2 spec uses this example for determining pre-release precedence:

1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0.

Pre-release precedence is calculated using the following rules, which are listed above their corresponding code.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pre_release.rb', line 33

def <=>(other)
  
  # A SemVer with no prerelease data is 'greater' than a SemVer with any prerelease data.
  # If both prereleases are empty, they are equal.
  return  1 if  empty? && !other.empty?
  return -1 if !empty? &&  other.empty?
  return  0 if  empty? &&  other.empty?
  
  [ids.size, other.ids.size].max.times do |n|
    id = ids[n]
    oid = other.ids[n]
    
    # A pre-release with fewer ids is less than a pre-release with more ids. (1.0.0-alpha < 1.0.0-alpha.1)
    return 1 if oid.nil?
    return -1 if id.nil?
    
    # If a pre-release id consists of only numbers, it is compared numerically.
    if id =~ ONLY_DIGITS && oid =~ ONLY_DIGITS
      id = id.to_i
      oid = oid.to_i
    end
    
    # If a pre-release id contains one or more letters, it is compared alphabetically.
    comparison = (id <=> oid)
    return comparison unless comparison == 0
  end
  
  0
end

#empty?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/pre_release.rb', line 23

def empty?
  ids.empty?
end

#to_sObject



19
20
21
# File 'lib/pre_release.rb', line 19

def to_s
  ids.join "."
end