Class: Tins::StringVersion::Version

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

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Version

Returns a new instance of Version.



11
12
13
14
15
# File 'lib/tins/string_version.rb', line 11

def initialize(string)
  string =~ /\A\d+(\.\d+)*\z/ or
    raise ArgumentError, "#{string.inspect} isn't a version number"
  @version = string.frozen? ? string.dup : string
end

Instance Method Details

#<=>(other) ⇒ Object



69
70
71
72
73
74
# File 'lib/tins/string_version.rb', line 69

def <=>(other)
  pairs = array.zip(other.array)
  pairs.map! { |a, b| [ a.to_i, b.to_i ] }
  a, b = pairs.transpose
  a <=> b
end

#==(other) ⇒ Object



76
77
78
# File 'lib/tins/string_version.rb', line 76

def ==(other)
  (self <=> other).zero?
end

#[](level) ⇒ Object



44
45
46
# File 'lib/tins/string_version.rb', line 44

def [](level)
  array[level_of(level)]
end

#[]=(level, value) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/tins/string_version.rb', line 48

def []=(level, value)
  level = level_of(level)
  value = value.to_i
  value >= 0 or raise ArgumentError,
    "version numbers can't contain negative numbers like #{value}"
  a = array
  a[level] = value
  a.map!(&:to_i)
  @version.replace a * ?.
end

#arrayObject Also known as: to_a



80
81
82
# File 'lib/tins/string_version.rb', line 80

def array
  @version.split(?.).map(&:to_i)
end

#bump(level = array.size - 1) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/tins/string_version.rb', line 27

def bump(level = array.size - 1)
  level = level_of(level)
  self[level] += 1
  for l in level.succ..3
    self[l] &&= 0
  end
  self
end

#initialize_copy(source) ⇒ Object



90
91
92
93
# File 'lib/tins/string_version.rb', line 90

def initialize_copy(source)
  super
  @version = source.instance_variable_get(:@version).dup
end

#level_of(specifier) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/tins/string_version.rb', line 36

def level_of(specifier)
  if specifier.respond_to?(:to_sym)
    LEVELS.fetch(specifier)
  else
    specifier
  end
end

#pred!Object



64
65
66
67
# File 'lib/tins/string_version.rb', line 64

def pred!
  self[-1] -= 1
  self
end

#succ!Object



59
60
61
62
# File 'lib/tins/string_version.rb', line 59

def succ!
  self[-1] += 1
  self
end

#to_sObject Also known as: inspect



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

def to_s
  @version
end