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.



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

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.



41
42
43
# File 'lib/semver.rb', line 41

def major
  @major
end

#minorObject

Returns the value of attribute minor.



41
42
43
# File 'lib/semver.rb', line 41

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



41
42
43
# File 'lib/semver.rb', line 41

def patch
  @patch
end

#specialObject

Returns the value of attribute special.



41
42
43
# File 'lib/semver.rb', line 41

def special
  @special
end

Class Method Details

.find(dir = nil) ⇒ Object



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

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

.find_file(dir = nil) ⇒ Object



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

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



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

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



78
79
80
81
82
83
84
# File 'lib/semver.rb', line 78

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



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

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



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

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



86
87
88
# File 'lib/semver.rb', line 86

def to_s
  format TAG_FORMAT
end