Class: Crackin::VersionFile

Inherits:
Object
  • Object
show all
Defined in:
lib/crackin/version_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ VersionFile

Returns a new instance of VersionFile.



3
4
5
6
# File 'lib/crackin/version_file.rb', line 3

def initialize(path)
  @path = path
  load
end

Instance Method Details

#alphaObject



81
82
83
84
# File 'lib/crackin/version_file.rb', line 81

def alpha
  tag('alpha')
  name
end

#betaObject



76
77
78
79
# File 'lib/crackin/version_file.rb', line 76

def beta
  tag('beta')
  name
end

#loadObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/crackin/version_file.rb', line 8

def load
  File.read(@path).lines.each do |line|
    @major = $1.to_i if line =~ /MAJOR\s+=\s+(\d+)/
    @minor = $1.to_i if line =~ /MINOR\s+=\s+(\d+)/
    @tiny = $1.to_i if line =~ /TINY\s+=\s+(\d+)/
    if line =~ /TAG\s+=\s+(.*)/
      @tag = $1
      @tag.gsub!(/['"]/, '')
      @tag = nil if @tag == 'nil'
    end
  end
end

#majorObject



50
51
52
53
54
55
56
# File 'lib/crackin/version_file.rb', line 50

def major
  @major += 1
  @minor = 0
  @tiny = 0
  @tag = nil
  name
end

#minorObject



58
59
60
61
62
63
# File 'lib/crackin/version_file.rb', line 58

def minor
  @minor += 1
  @tiny = 0
  @tag = nil
  name
end

#nameObject



34
35
36
# File 'lib/crackin/version_file.rb', line 34

def name
  "v#{number}"
end

#noneObject



96
97
98
# File 'lib/crackin/version_file.rb', line 96

def none

end

#numberObject



46
47
48
# File 'lib/crackin/version_file.rb', line 46

def number
  to_a.join('.')
end

#rcObject



71
72
73
74
# File 'lib/crackin/version_file.rb', line 71

def rc
  tag('rc')
  name
end

#saveObject



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

def save
  out = []
  tag = @tag ? "'#{@tag}'" : "nil"
  File.open(@path).lines.each do |line|; line.chomp!
    line.gsub!(/(\s+)MAJOR\s+=\s+\d+/, "\\1MAJOR = #{@major}")
    line.gsub!(/(\s+)MINOR\s+=\s+\d+/, "\\1MINOR = #{@minor}")
    line.gsub!(/(\s+)TINY\s+=\s+\d+/, "\\1TINY = #{@tiny}")
    line.gsub!(/(\s+)TAG\s+=\s+(nil|['"].*['"])/, "\\1TAG = #{tag}")
    out << line
  end
  File.open(@path, "w+") {|f| f.write(out.join("\n"))}
end

#tag(type) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/crackin/version_file.rb', line 86

def tag(type)
  if @tag =~ /#{type}(\d+)/
    num = $1.to_i + 1
    @tag = "#{type}#{num}"
  else
    @tiny += 1
    @tag = "#{type}0"
  end
end

#tinyObject



65
66
67
68
69
# File 'lib/crackin/version_file.rb', line 65

def tiny
  @tiny += 1 unless @tag
  @tag = nil
  name
end

#to_aObject



42
43
44
# File 'lib/crackin/version_file.rb', line 42

def to_a
  [@major, @minor, @tiny, @tag].compact
end

#to_sObject



38
39
40
# File 'lib/crackin/version_file.rb', line 38

def to_s
  name
end