Sha256: 9d8d4d1819fe2df68055f97283f2ec927e56afdda007c53aba62fad68ca0f029

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Unwrappr
  # Represents the version of a gem. Helps in comparing two versions to
  # identify differences and extracting the major, minor and patch components
  # that make up semantic versioning. https://semver.org/
  class GemVersion
    include Comparable

    def initialize(version_string)
      @version_string = version_string
      @version = Gem::Version.create(version_string)
      @major = segment(0)
      @minor = segment(1)
      @patch = segment(2)
    end

    attr_reader :major, :minor, :patch, :version

    def major_difference?(other)
      (major != other.major)
    end

    def minor_difference?(other)
      (major == other.major) &&
        (minor != other.minor)
    end

    def patch_difference?(other)
      (major == other.major) &&
        (minor == other.minor) &&
        (patch != other.patch)
    end

    def <=>(other)
      @version <=> other.version
    end

    def to_s
      @version_string
    end

    private

    def segment(index)
      segment = @version.canonical_segments[index] || 0
      (segment.is_a?(Numeric) ? segment : nil)
    rescue NoMethodError
      abort(<<~MESSAGE)
        Unwrappr requires RubyGems v2.7.0 or newer.

        To upgrade to the latest RubyGems visit https://rubygems.org/pages/download

      MESSAGE
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unwrappr-0.3.1 lib/unwrappr/gem_version.rb
unwrappr-0.3.0 lib/unwrappr/gem_version.rb