Sha256: 316047685efea1e5f2c405227584e2e1558569c526f73f51ea9335401a984db9

Contents?: true

Size: 1.13 KB

Versions: 11

Compression:

Stored size: 1.13 KB

Contents

class RenuoVersion
  SEMVER_SCHEMA = /^\d+\.\d+\.\d+(\.[a-z]+\d+)?$/.freeze
  SEGMENTS = %w[major minor patch].freeze

  def self.create(str)
    raise "Invalid segments (#{str} =~ #{SEMVER_SCHEMA})" unless str =~ SEMVER_SCHEMA

    new(*str.split('.'))
  end

  attr_reader :segments

  def initialize(major, minor, patch, prerelease = nil)
    @segments = [major, minor, patch].map(&:to_i)
    @segments.push(prerelease.to_s) if prerelease
  end

  def bump(segment)
    raise "Invalid segment #{segment} (must be one of #{SEGMENTS})" unless SEGMENTS.include?(segment)

    send("bump_#{segment}")
    self
  end

  # Returns true if it's a release candidate (e.g. 3.0.0.rc4)
  def prerelease?
    @segments.length > 3
  end

  def to_s
    @segments.join('.')
  end

  private

  def bump_major
    @segments = [@segments[0] + 1, 0, 0]
  end

  def bump_minor
    @segments = [@segments[0], @segments[1] + 1, 0]
  end

  def bump_patch
    if prerelease?
      old_number = @segments[3][/\d+/]
      @segments[3].gsub!(old_number, (old_number.to_i + 1).to_s)
    else # default patch
      @segments = [*@segments[0..1], @segments[2] + 1]
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
renuo-cli-3.1.7 lib/renuo/cli/app/renuo_version.rb
renuo-cli-3.1.6 lib/renuo/cli/app/renuo_version.rb
renuo-cli-3.1.5 lib/renuo/cli/app/renuo_version.rb
renuo-cli-3.1.4 lib/renuo/cli/app/renuo_version.rb
renuo-cli-3.1.3 lib/renuo/cli/app/renuo_version.rb
renuo-cli-3.1.2 lib/renuo/cli/app/renuo_version.rb
renuo-cli-3.1.1 lib/renuo/cli/app/renuo_version.rb
renuo-cli-3.1.0 lib/renuo/cli/app/renuo_version.rb
renuo-cli-3.0.0 lib/renuo/cli/app/renuo_version.rb
renuo-cli-2.0.0 lib/renuo/cli/app/renuo_version.rb
renuo-cli-1.8.0 lib/renuo/cli/app/renuo_version.rb