Class: RakeVersion::Version

Inherits:
Object
  • Object
show all
Defined in:
lib/rake-version/version.rb

Constant Summary collapse

REGEXP =
/^(\d+)\.(\d+)\.(\d+)(?:\.(\d+))?((?:\-[A-Za-z0-9]+)*)$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVersion

Returns a new instance of Version.



13
14
15
16
17
18
19
20
# File 'lib/rake-version/version.rb', line 13

def initialize
  @major = 0
  @minor = 0
  @patch = 0
  @build = nil
  @tags = []
  # TODO: create methods to list, add and remove tags
end

Instance Attribute Details

#buildObject (readonly)

Returns the value of attribute build.



10
11
12
# File 'lib/rake-version/version.rb', line 10

def build
  @build
end

#majorObject (readonly)

Returns the value of attribute major.



7
8
9
# File 'lib/rake-version/version.rb', line 7

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



8
9
10
# File 'lib/rake-version/version.rb', line 8

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



9
10
11
# File 'lib/rake-version/version.rb', line 9

def patch
  @patch
end

#tagsObject (readonly)

Returns the value of attribute tags.



11
12
13
# File 'lib/rake-version/version.rb', line 11

def tags
  @tags
end

Instance Method Details

#bump(type) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rake-version/version.rb', line 22

def bump type
  case type
  when :major
    @major += 1
    @minor = 0
    @patch = 0
  when :minor
    @minor += 1
    @patch = 0
  when :patch
    @patch += 1
  else
    raise BadBumpType, "Unknown version bump type #{type.inspect}. Expecting :major, :minor or :patch."
  end
  self
end

#from_s(s) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rake-version/version.rb', line 39

def from_s s
  s.to_s.match(REGEXP).tap do |m|
    raise BadVersionString, "Version '#{s}' expected to have format MAJOR.MINOR.PATCH(.BUILD)(-TAG)." if m.nil?
    @major = m[1].to_i
    @minor = m[2].to_i
    @patch = m[3].to_i
    @build = m[4] ? m[4].to_i : nil
    @tags = m[5] ? m[5].sub(/^\-/, '').split('-') : []
  end
  self
end

#to_sObject



51
52
53
54
55
56
57
# File 'lib/rake-version/version.rb', line 51

def to_s
  String.new.tap do |s|
    s << "#{@major}.#{@minor}.#{@patch}"
    s << ".#{@build}" if @build
    s << tags.collect{ |tag| "-#{tag}" }.join('') unless tags.empty?
  end
end