Class: SemVer

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/semver.rb

Constant Summary collapse

FILE_NAME =
'.semver'
TAG_FORMAT =
'v%M.%m.%p%s'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major = 0, minor = 0, patch = 0, special = '') ⇒ SemVer

Returns a new instance of SemVer.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/semver.rb', line 35

def initialize major=0, minor=0, patch=0, special=''
  major.kind_of? Integer or raise "invalid major: #{major}"
  minor.kind_of? Integer or raise "invalid minor: #{minor}"
  patch.kind_of? Integer or raise "invalid patch: #{patch}"

  unless special.empty?
    special =~ /^\d+$/ or raise "invalid special: #{special}"
  end

  @major, @minor, @patch, @special = major, minor, patch, special
end

Instance Attribute Details

#majorObject

Returns the value of attribute major.



33
34
35
# File 'lib/semver.rb', line 33

def major
  @major
end

#minorObject

Returns the value of attribute minor.



33
34
35
# File 'lib/semver.rb', line 33

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



33
34
35
# File 'lib/semver.rb', line 33

def patch
  @patch
end

#specialObject

Returns the value of attribute special.



33
34
35
# File 'lib/semver.rb', line 33

def special
  @special
end

Class Method Details

.find(dir = nil) ⇒ Object



9
10
11
12
13
14
# File 'lib/semver.rb', line 9

def SemVer.find dir=nil
  v = SemVer.new
  f = SemVer.find_file dir
  v.load f
  v
end

.find_file(dir = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/semver.rb', line 16

def SemVer.find_file dir=nil
  dir ||= Dir.pwd
  raise "#{dir} is not a directory" unless File.directory? dir
  path = File.join dir, FILE_NAME

  Dir.chdir dir do
    while !File.exists? path do
      raise SemVerMissingError, "#{dir} is not semantic versioned", caller if File.dirname(path).match(/(\w:\/|\/)$/i)
      path = File.join File.dirname(path), ".."
      path = File.expand_path File.join(path, FILE_NAME)
      puts "semver: looking at #{path}"
    end
    return path
  end

end

Instance Method Details

#<=>(other) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/semver.rb', line 86

def <=> other
  maj = major.to_i <=> other.major.to_i
  return maj unless maj == 0

  min = minor.to_i <=> other.minor.to_i
  return min unless min == 0

  pat = patch.to_i <=> other.patch.to_i
  return pat unless pat == 0

  spe = special.to_i <=> other.special.to_i
  return spe unless spe == 0

  0
end

#format(fmt) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/semver.rb', line 70

def format fmt
  fmt = fmt.gsub '%M', @major.to_s
  fmt = fmt.gsub '%m', @minor.to_s
  fmt = fmt.gsub '%p', @patch.to_s
  if @special.nil? or @special.length == 0 then
    fmt = fmt.gsub '%s', ''
  else
    fmt = fmt.gsub '%s', "-" + @special.to_s
  end
  fmt
end

#load(file) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/semver.rb', line 47

def load file
  @file = file
  hash = YAML.load_file(file) || {}
  @major = hash[:major] or raise "invalid semver file: #{file}"
  @minor = hash[:minor] or raise "invalid semver file: #{file}"
  @patch = hash[:patch] or raise "invalid semver file: #{file}"
  @special = hash[:special]  or raise "invalid semver file: #{file}"
end

#save(file = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/semver.rb', line 56

def save file=nil
  file ||= @file

  hash = {
    :major => @major,
    :minor => @minor,
    :patch => @patch,
    :special => @special
  }

  yaml = YAML.dump hash
  open(file, 'w') { |io| io.write yaml }
end

#to_sObject



82
83
84
# File 'lib/semver.rb', line 82

def to_s
  format TAG_FORMAT
end