Class: Doing::Version

Inherits:
Object show all
Defined in:
lib/doing/changelog/version.rb

Overview

Semantic versioning

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Version

Returns a new instance of Version.



8
9
10
# File 'lib/doing/changelog/version.rb', line 8

def initialize(string)
  @maj, @min, @patch = version_to_a(string)
end

Instance Attribute Details

#majObject (readonly)

Returns the value of attribute maj.



6
7
8
# File 'lib/doing/changelog/version.rb', line 6

def maj
  @maj
end

#minObject (readonly)

Returns the value of attribute min.



6
7
8
# File 'lib/doing/changelog/version.rb', line 6

def min
  @min
end

#patchObject (readonly)

Returns the value of attribute patch.



6
7
8
# File 'lib/doing/changelog/version.rb', line 6

def patch
  @patch
end

Instance Method Details

#compare(other, comp, inclusive: false) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/doing/changelog/version.rb', line 40

def compare(other, comp, inclusive: false)
  case comp
  when :older
    if @maj <= other.maj
      if @maj < other.maj
        true
      elsif @maj == other.maj && (other.min.nil? || @min < other.min)
        true
      elsif @maj == other.maj && @min == other.min
        if other.patch.nil?
          false
        else
          inclusive ? @patch <= other.patch : @patch < other.patch
        end
      else
        false
      end
    else
      false
    end
  when :newer
    if @maj >= other.maj
      if @maj > other.maj
        true
      elsif @maj == other.maj && (other.min.nil? || @min > other.min)
        true
      elsif @maj == other.maj && @min == other.min
        if other.patch.nil?
          false
        else
          inclusive ? @patch >= other.patch : @patch > other.patch
        end
      else
        false
      end
    else
      false
    end
  when :equal
    if @maj == other.maj
      if other.min.nil?
        true
      elsif wild?(other.min)
        @min.to_s =~ /^#{other.min}/ ? true : false
      else
        if @min == other.min
          if other.patch.nil?
            true
          elsif wild?(other.patch)
            @patch.to_s =~ /^#{other.patch}/ ? true : false
          else
            @patch == other.patch
          end
        else
          false
        end
      end
    end
  end
end

#to_sObject



101
102
103
# File 'lib/doing/changelog/version.rb', line 101

def to_s
  "#{@maj}.#{@min || 0}.#{@patch || 0}"
end

#version_to_a(string) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/doing/changelog/version.rb', line 12

def version_to_a(string)
  raise 'Version not a string' unless string.is_a?(String)

  v = string.match(/(?<maj>\d+)(?:\.(?<min>[\d*?]+))?(?:\.(?<patch>[\d*?]+))?/)

  raise 'Error parsing semantic version string' if v.nil?

  maj = v['maj'].to_i
  min = case v['min']
        when /[*?]/
          v['min'].sub(/(\d+)?[^\d]/, '\1\d+')
        when /^[0-9]+$/
          v['min'].to_i
        end
  pat = case v['patch']
        when /[*?]/
          v['patch'].sub(/(\d+)?[^\d]/, '\1\d+')
        when /^[0-9]+$/
          v['patch'].to_i
        end
  [maj, min, pat]
end

#wild?(val) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/doing/changelog/version.rb', line 35

def wild?(val)
  val.is_a?(String)
end