Class: SemVer

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

Constant Summary collapse

VERSION =
/^v?(\d+)\.(\d+)\.(\d+)([A-Za-z][0-9A-Za-z-]*|)$/
SIMPLE_RANGE =
/^v?(\d+|[xX])(?:\.(\d+|[xX])(?:\.(\d+|[xX]))?)?$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ver) ⇒ SemVer

Returns a new instance of SemVer.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/semver.rb', line 17

def initialize(ver)
  unless SemVer.valid?(ver)
    raise ArgumentError.new("Invalid version string '#{ver}'!")
  end

  @major, @minor, @tiny, @special = VERSION.match(ver).captures.map do |x|
    # Because Kernel#Integer tries to interpret hex and octal strings, which
    # we specifically do not want, and which cannot be overridden in 1.8.7.
    Float(x).to_i rescue x
  end
end

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



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

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



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

def minor
  @minor
end

#specialObject (readonly)

Returns the value of attribute special.



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

def special
  @special
end

#tinyObject (readonly)

Returns the value of attribute tiny.



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

def tiny
  @tiny
end

Class Method Details

.find_matching(pattern, versions) ⇒ Object



11
12
13
# File 'lib/semver.rb', line 11

def self.find_matching(pattern, versions)
  versions.select { |v| v.matched_by?("#{pattern}") }.sort.last
end

.valid?(ver) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/semver.rb', line 7

def self.valid?(ver)
  VERSION =~ ver
end

Instance Method Details

#<=>(other) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/semver.rb', line 29

def <=>(other)
  other = SemVer.new("#{other}") unless other.is_a? SemVer
  return self.major <=> other.major unless self.major == other.major
  return self.minor <=> other.minor unless self.minor == other.minor
  return self.tiny  <=> other.tiny  unless self.tiny  == other.tiny

  return 0  if self.special  == other.special
  return 1  if self.special  == ''
  return -1 if other.special == ''

  return self.special <=> other.special
end

#inspectObject Also known as: to_s



61
62
63
# File 'lib/semver.rb', line 61

def inspect
  "v#{@major}.#{@minor}.#{@tiny}#{@special}"
end

#matched_by?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/semver.rb', line 42

def matched_by?(pattern)
  # For the time being, this is restricted to exact version matches and
  # simple range patterns.  In the future, we should implement some or all of
  # the comparison operators here:
  # https://github.com/isaacs/node-semver/blob/d474801/semver.js#L340

  case pattern
  when SIMPLE_RANGE
    pattern = SIMPLE_RANGE.match(pattern).captures
    pattern[1] = @minor unless pattern[1] && pattern[1] !~ /x/i
    pattern[2] = @tiny  unless pattern[2] && pattern[2] !~ /x/i
    [@major, @minor, @tiny] == pattern.map { |x| x.to_i }
  when VERSION
    self == SemVer.new(pattern)
  else
    false
  end
end