Class: SemanticRange::PreRelease

Inherits:
Object
  • Object
show all
Defined in:
lib/semantic_range/pre_release.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ PreRelease

Returns a new instance of PreRelease.



5
6
7
# File 'lib/semantic_range/pre_release.rb', line 5

def initialize(input)
  @parts = parse(input)
end

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



3
4
5
# File 'lib/semantic_range/pre_release.rb', line 3

def parts
  @parts
end

Instance Method Details

#<=>(other) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/semantic_range/pre_release.rb', line 37

def <=>(other)
  return unless other.is_a?(self.class)

  return -1 if parts.any? && !other.parts.any?
  return 1 if !parts.any? && other.parts.any?
  return 0 if !parts.any? && !other.parts.any?

  i = 0
  while true
    a = parts[i]
    b = other.parts[i]

    if a.nil? && b.nil?
      return 0
    elsif b.nil?
      return 1
    elsif a.nil?
      return -1
    elsif a == b

    else
      return Version.compare_identifiers(a, b)
    end
    i += 1
  end
end

#clear!Object



29
30
31
# File 'lib/semantic_range/pre_release.rb', line 29

def clear!
  @parts = []
end

#convert(str) ⇒ Object



13
14
15
# File 'lib/semantic_range/pre_release.rb', line 13

def convert(str)
  str.match(/^[0-9]+$/) ? str.to_i : str
end

#empty?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/semantic_range/pre_release.rb', line 21

def empty?
  parts.empty?
end

#increment!(identifier = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/semantic_range/pre_release.rb', line 68

def increment!(identifier = nil)
  if empty?
    zero!
  else
    if last_number_index
      @parts[last_number_index] += 1
    else
      @parts << 0
    end
  end

  if identifier
    # 1.2.0-beta.1 bumps to 1.2.0-beta.2,
    # 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
    if parts[0] == identifier
      unless parts[1].kind_of?(Integer)
        @parts = [identifier, 0]
      end
    else
      @parts = [identifier, 0]
    end
  end
end

#last_number_indexObject



64
65
66
# File 'lib/semantic_range/pre_release.rb', line 64

def last_number_index
  parts.rindex { |e| e.is_a? Integer }
end

#lengthObject



17
18
19
# File 'lib/semantic_range/pre_release.rb', line 17

def length
  parts.length
end

#parse(str) ⇒ Object



9
10
11
# File 'lib/semantic_range/pre_release.rb', line 9

def parse(str)
  str.to_s.split('.').map { |id| convert(id) }
end

#to_sObject



25
26
27
# File 'lib/semantic_range/pre_release.rb', line 25

def to_s
  parts.join '.'
end

#zero!Object



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

def zero!
  @parts = [0]
end