Sha256: 8ca6ddd8108af1e489037c4c8945a28b650db471398858788b32af83c4cf4c69

Contents?: true

Size: 1.1 KB

Versions: 9

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module Groundskeeper
  # Encapsulates semantic version manipulation and comparison.
  class SemanticVersion
    MAJOR = "major"
    MINOR = "minor"
    PATCH = "patch"
    SEMANTIC_RELEASE_TYPE = { M: MAJOR, m: MINOR, p: PATCH }.freeze

    attr_reader :major, :minor, :patch

    def self.build(version)
      return version if version.is_a?(SemanticVersion)

      new(version)
    end

    def initialize(version)
      @major, @minor, @patch = version.split(".").map(&:to_i)
    end

    def bump(release_type)
      case SEMANTIC_RELEASE_TYPE[release_type.to_sym]
      when MAJOR
        "#{major + 1}.0.0"
      when MINOR
        "#{major}.#{minor + 1}.0"
      when PATCH
        "#{major}.#{minor}.#{patch + 1}"
      end
    end

    def >(other)
      other_version = SemanticVersion.build(other)
      other_major = other_version.major

      return true if major > other_major

      return false unless major == other_major

      other_minor = other_version.minor

      return true if minor > other_minor

      minor == other_minor && patch > other_version.patch
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
groundskeeper-bitcore-0.3.1 lib/groundskeeper/semantic_version.rb
groundskeeper-bitcore-0.3.0 lib/groundskeeper/semantic_version.rb
groundskeeper-bitcore-0.2.7 lib/groundskeeper/semantic_version.rb
groundskeeper-bitcore-0.2.6 lib/groundskeeper/semantic_version.rb
groundskeeper-bitcore-0.2.5 lib/groundskeeper/semantic_version.rb
groundskeeper-bitcore-0.2.4 lib/groundskeeper/semantic_version.rb
groundskeeper-bitcore-0.2.3 lib/groundskeeper/semantic_version.rb
groundskeeper-bitcore-0.2.2 lib/groundskeeper/semantic_version.rb
groundskeeper-bitcore-0.2.1 lib/groundskeeper/semantic_version.rb