| Class | VersionNumber |
| In: |
lib/more/facets/version.rb
|
| Parent: | Object |
VersionNumber is a simplified form of a Tuple class desgined specifically for dealing with version numbers.
Parses a string constraint returning the operation as a lambda.
# File lib/more/facets/version.rb, line 113
113: def self.constraint_lambda( constraint )
114: op, val = *parse_constraint( constraint )
115: lambda { |t| t.send(op, val) }
116: end
# File lib/more/facets/version.rb, line 40
40: def initialize( *args )
41: args = args.join('.').split(/\W+/)
42: @self = args.collect { |i| i.to_i }
43: end
# File lib/more/facets/version.rb, line 118
118: def self.parse_constraint( constraint )
119: constraint = constraint.strip
120: re = %r{^(=~|~>|<=|>=|==|=|<|>)?\s*(\d+(:?[-.]\d+)*)$}
121: if md = re.match( constraint )
122: if op = md[1]
123: op = '=~' if op == '~>'
124: op = '==' if op == '='
125: val = new( *md[2].split(/\W+/) )
126: else
127: op = '=='
128: val = new( *constraint.split(/\W+/) )
129: end
130: else
131: raise ArgumentError, "invalid constraint"
132: end
133: return op, val
134: end
"Spaceship" comparsion operator.
# File lib/more/facets/version.rb, line 61
61: def <=>( other )
62: #other = other.to_t
63: [@self.size, other.size].max.times do |i|
64: c = @self[i] <=> other[i]
65: return c if c != 0
66: end
67: 0
68: end
For pessimistic constraint (like ’~>’ in gems)
# File lib/more/facets/version.rb, line 72
72: def =~( other )
73: #other = other.to_t
74: upver = other.dup
75: upver[0] += 1
76: @self >= other and @self < upver
77: end
# File lib/more/facets/version.rb, line 92
92: def bump(which=:teeny)
93: case which
94: when :major
95: self.class.new(major+1)
96: when :minor
97: self.class.new(major, minor+1)
98: when :teeny
99: self.class.new(major, minor, teeny+1)
100: else
101: # ???
102: end
103: end
Major is the first number in the version series.
# File lib/more/facets/version.rb, line 81
81: def major ; @self[0] ; end
Delegate to the array.
# File lib/more/facets/version.rb, line 107
107: def method_missing( sym, *args, &blk )
108: @self.send(sym, *args, &blk ) rescue super
109: end
Minor is the second number in the version series.
# File lib/more/facets/version.rb, line 85
85: def minor ; @self[1] || 0 ; end