Class: Version::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/version/component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component) ⇒ Component

Creates a single Component of a version, consisting of digits and possibly a letter. For example, 1, 3a, 12, or 0.



11
12
13
14
15
16
# File 'lib/version/component.rb', line 11

def initialize(component)
  parts = component.split /(?=\D)/
  
  self.digits = parts[0].to_i
  self.letter = parts[1].to_s.strip
end

Instance Attribute Details

#digitsObject

Returns the value of attribute digits.



4
5
6
# File 'lib/version/component.rb', line 4

def digits
  @digits
end

#letterObject

Returns the value of attribute letter.



5
6
7
# File 'lib/version/component.rb', line 5

def letter
  @letter
end

Instance Method Details

#<=>(other) ⇒ Object



46
47
48
# File 'lib/version/component.rb', line 46

def <=>(other)
  self.to_sortable_a <=> other.to_sortable_a
end

#initialize_copy(other) ⇒ Object



18
19
20
21
# File 'lib/version/component.rb', line 18

def initialize_copy(other)
  self.digits = other.digits
  self.letter = other.letter.dup
end

#inspectObject



66
67
68
# File 'lib/version/component.rb', line 66

def inspect
  self.to_s.inspect
end

#next(pre = false) ⇒ Object



31
32
33
# File 'lib/version/component.rb', line 31

def next(pre = false)
  self.dup.next!(pre)
end

#next!(pre = false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/version/component.rb', line 35

def next!(pre = false)
  case
    when (    pre and     self.prerelease?) then self.letter.next!
    when (    pre and not self.prerelease?) then self.letter = 'a'
    when (not pre and     self.prerelease?) then self.letter = ''
    when (not pre and not self.prerelease?) then self.digits = self.digits.next
  end
  
  self
end

#prerelease?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/version/component.rb', line 23

def prerelease?
  not self.letter.empty?
end

#to_aObject



54
55
56
# File 'lib/version/component.rb', line 54

def to_a
  [ self.digits, self.letter ]
end

#to_iObject



58
59
60
# File 'lib/version/component.rb', line 58

def to_i
  self.digits
end

#to_sObject



62
63
64
# File 'lib/version/component.rb', line 62

def to_s
  self.to_a.join
end

#to_sortable_aObject



50
51
52
# File 'lib/version/component.rb', line 50

def to_sortable_a
  [ self.digits, self.prerelease? ? 0 : 1, self.letter ]
end

#unprerelease!Object



27
28
29
# File 'lib/version/component.rb', line 27

def unprerelease!
  self.next! if self.prerelease?
end