Class: SemVer

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

Constant Summary collapse

FILE_NAME =
'.semver'

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.



63
64
65
# File 'lib/semver.rb', line 63

def initialize major='0', minor='0', patch='0', special=''
  self.major, self.minor, self.patch, self.special = major.to_s, minor.to_s, patch.to_s, special.to_s
end

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



61
62
63
# File 'lib/semver.rb', line 61

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



61
62
63
# File 'lib/semver.rb', line 61

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



61
62
63
# File 'lib/semver.rb', line 61

def patch
  @patch
end

#specialObject (readonly)

Returns the value of attribute special.



61
62
63
# File 'lib/semver.rb', line 61

def special
  @special
end

Class Method Details

.attr_writer_pattern(pattern, symbol) ⇒ Object



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

def attr_writer_pattern pattern, symbol

  define_method("#{symbol}=".to_sym) do |str|

    if str =~ pattern || str.empty?
      instance_variable_set "@#{symbol}".to_sym, str
    else
      raise "#{symbol}: #{str} does not match pattern #{pattern}"
    end

  end

end

.find(dir = nil) ⇒ Object



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

def SemVer.find dir=nil
  v = SemVer.new
  v.load
  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

      if File.dirname(File.expand_path(path)) == '/'
        raise NoSemVerError, "#{dir} is not semantic versioned"
      elsif Dir[FILE_NAME].empty?
        path = File.join '..', path
        next
      else
        path = File.expand_path path
        return path
      end

    end

  end
end

Instance Method Details

#<=>(other) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/semver.rb', line 96

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



88
89
90
91
92
93
94
# File 'lib/semver.rb', line 88

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

#load(file = SemVer.find_file) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/semver.rb', line 67

def load file=SemVer.find_file
  hash = open(file) { |io| YAML::load io.read } || {}
  self.major = hash[:major] || '0'
  self.minor = hash[:minor] || '0'
  self.patch = hash[:patch] || '0'
  self.special = hash[:special] || ''
end

#save(file = SemVer.find_file) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/semver.rb', line 75

def save file=SemVer.find_file

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

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