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.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/semver.rb', line 41

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 =~ /[A-Za-z][0-9A-Za-z-]+/ 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.



39
40
41
# File 'lib/semver.rb', line 39

def major
  @major
end

#minorObject

Returns the value of attribute minor.



39
40
41
# File 'lib/semver.rb', line 39

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



39
40
41
# File 'lib/semver.rb', line 39

def patch
  @patch
end

#specialObject

Returns the value of attribute special.



39
40
41
# File 'lib/semver.rb', line 39

def special
  @special
end

Class Method Details

.find(dir = nil) ⇒ Object



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

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

.find_file(dir = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/semver.rb', line 15

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

  Dir.chdir dir do

    loop do
      raise "#{dir} is not semantic versioned" if File.dirname(path) == '/'

      if Dir[path].empty?
        path = File.join '..', path
        path = File.expand_path path
        next
      else
        return path
      end

    end

  end

end

Instance Method Details

#<=>(other) ⇒ Object



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

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 <=> other.special
  return spec unless spe == 0

  0
end

#format(fmt) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/semver.rb', line 76

def format fmt
  fmt.gsub! '%M', @major.to_s
  fmt.gsub! '%m', @minor.to_s
  fmt.gsub! '%p', @patch.to_s
  fmt.gsub! '%s', @special.to_s
  fmt
end

#load(file) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/semver.rb', line 53

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



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/semver.rb', line 62

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



84
85
86
# File 'lib/semver.rb', line 84

def to_s
  format TAG_FORMAT
end