Class: Bake::Gem::Version

Inherits:
Object
  • Object
show all
Defined in:
lib/bake/gem/helper.rb

Overview

Represents a gem version with support for parsing and incrementing version numbers.

Constant Summary collapse

LINE_PATTERN =
/VERSION = ['"](?<version>(?<parts>\d+\.\d+\.\d+)(-(?<suffix>.*?))?)['"]/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts, suffix) ⇒ Version

Initialize a new version with the given parts and optional suffix.



37
38
39
40
# File 'lib/bake/gem/helper.rb', line 37

def initialize(parts, suffix)
	@parts = parts
	@suffix = suffix
end

Class Method Details

.update_version(line) ⇒ Object

If the line contains a version constant, update it using the provided block.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bake/gem/helper.rb', line 21

def self.update_version(line)
	if match = line.match(LINE_PATTERN)
		parts = match[:parts].split(/\./).map(&:to_i)
		suffix = match[:suffix]
		
		version = self.new(parts, suffix)
		
		yield version
		
		line.sub!(match[:version], version.join)
	end
end

Instance Method Details

#increment(bump) ⇒ Object

Increment the version according to the provided bump specification.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bake/gem/helper.rb', line 65

def increment(bump)
	bump.each_with_index do |increment, index|
		if index > @parts.size
			@suffix = bump[index..].join(".")
			break
		end
		
		if increment == 1
			@parts[index] += 1
		elsif increment == 0
			@parts[index] = 0
		end
		# If increment is nil, we don't change that part of the version
	end
	
	return self
end

#joinObject

Join all parts together to form a version string.



49
50
51
52
53
54
55
# File 'lib/bake/gem/helper.rb', line 49

def join
	if @suffix
		return "#{@parts.join('.')}-#{@suffix}"
	else
		return @parts.join(".")
	end
end

#release?Boolean

Check if this version represents a release version.

Returns:

  • (Boolean)


44
45
46
# File 'lib/bake/gem/helper.rb', line 44

def release?
	@suffix.nil?
end

#to_sObject

The version string with a ā€œvā€ prefix.



58
59
60
# File 'lib/bake/gem/helper.rb', line 58

def to_s
	"v#{join}"
end