Class: Version

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/carat/version.rb

Constant Summary collapse

VERSION_EXP =
/\d+(?:\.\d+)*/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg, *nums) ⇒ Version



49
50
51
# File 'lib/carat/version.rb', line 49

def initialize( arg, *nums )
  set( arg, *nums )
end

Class Method Details

.[](*args) ⇒ Object



43
44
45
# File 'lib/carat/version.rb', line 43

def self.[](*args)
  Version.new(*args)
end

Instance Method Details

#<=>(other) ⇒ Object



130
131
132
133
134
135
# File 'lib/carat/version.rb', line 130

def <=>( other )
  return false unless self.class === other
  n = @num
  ret = nil; other.instance_eval { ret = (n <=> @num) }
  ret
end

#==(other) ⇒ Object Also known as: eql?



69
70
71
# File 'lib/carat/version.rb', line 69

def ==( other )
  @num == other.to_a
end

#dupObject



65
66
67
# File 'lib/carat/version.rb', line 65

def dup
  self.class.new( *@nums )
end

#hashObject



74
# File 'lib/carat/version.rb', line 74

def hash ; @num.hash ; end

#inspectObject



79
80
81
# File 'lib/carat/version.rb', line 79

def inspect
  "\#<#{self.class} #{to_s}>"
end

#majorObject



83
# File 'lib/carat/version.rb', line 83

def major       ; @num[0]     ; end

#major=(i) ⇒ Object



84
# File 'lib/carat/version.rb', line 84

def major=( i ) ; @num[0] = i ; end

#minorObject



86
# File 'lib/carat/version.rb', line 86

def minor       ; @num[1]     ; end

#minor=(i) ⇒ Object



87
# File 'lib/carat/version.rb', line 87

def minor=( i ) ; @num[1] = i ; end

#next_major!Object



95
96
97
98
# File 'lib/carat/version.rb', line 95

def next_major!    
  (1..last_index).each{ |i| @num[i] = 0 } if last_index > 0
  @num[0] += 1
end

#next_minor!Object



100
101
102
103
104
105
# File 'lib/carat/version.rb', line 100

def next_minor!
  (2..last_index).each{ |i| @num[i] = 0 } if last_index > 1
  if @num[1]
    @num[1] += 1
  end
end

#next_teeny!Object



107
108
109
110
111
112
# File 'lib/carat/version.rb', line 107

def next_teeny!
  (3..last_index).each{ |i| @num[i] = 0 } if last_index > 2
  if @num[2]
    @num[2] += 1
  end
end

#next_weeny!Object



114
115
116
117
118
119
# File 'lib/carat/version.rb', line 114

def next_weeny!
  #(4..last_index).each{ |i| @num[i] = 0 } if last_index > 3
  if @num[3]
    @num[3] += 1
  end
end

#set(arg, *nums) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/carat/version.rb', line 53

def set( arg, *nums )
  if String === arg then
    unless m = VERSION_EXP.match( arg ) then
      raise ArgumentError, "wrong version string format '#{arg}'"
    end
    @num = m[0].split('.').collect {|i| i.to_i }
  else
    @num = [ arg, *(nums || []) ]
    #@num.unshift arg
  end
end

#teenyObject



89
# File 'lib/carat/version.rb', line 89

def teeny       ; @num[2]     ; end

#teeny=(i) ⇒ Object



90
# File 'lib/carat/version.rb', line 90

def teeny=( i ) ; @num[2] = i ; end

#to_aObject



77
# File 'lib/carat/version.rb', line 77

def to_a ; @num.dup ; end

#to_sObject



76
# File 'lib/carat/version.rb', line 76

def to_s ; @num.join '.' ; end

#typeObject



121
122
123
124
125
126
127
128
# File 'lib/carat/version.rb', line 121

def type
  case last_index
  when 0 then :major
  when 1 then :minor
  when 2 then :teeny
  when 3 then :weeny
  end
end

#weenyObject



92
# File 'lib/carat/version.rb', line 92

def weeny       ; @num[3]     ; end

#weeny=(i) ⇒ Object



93
# File 'lib/carat/version.rb', line 93

def weeny=( i ) ; @num[3] = i ; end