Sha256: 8312ec8c3648a66413bd57121b99ef1ce5ec36fe02d2539bdfac0b7621f54364

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 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)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
unwrappr-0.3.5 lib/unwrappr/gem_version.rb
unwrappr-0.3.4 lib/unwrappr/gem_version.rb
unwrappr-0.3.3 lib/unwrappr/gem_version.rb
unwrappr-0.3.2 lib/unwrappr/gem_version.rb