Class VersionNumber
In: lib/more/facets/version.rb
Parent: Object

VersionNumber

VersionNumber is a simplified form of a Tuple class desgined specifically for dealing with version numbers.

Methods

<=>   =~   []   bump   constraint_lambda   inspect   major   method_missing   minor   new   parse_constraint   teeny   to_s   to_str  

Included Modules

Comparable

Public Class methods

Parses a string constraint returning the operation as a lambda.

[Source]

     # 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

[Source]

    # 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

[Source]

     # 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

Public Instance methods

"Spaceship" comparsion operator.

[Source]

    # 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)

[Source]

    # 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

[Source]

    # File lib/more/facets/version.rb, line 55
55:   def [](i)
56:     @self.fetch(i,0)
57:   end

[Source]

     # 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

[Source]

    # File lib/more/facets/version.rb, line 51
51:   def inspect
52:     @self.to_s
53:   end

Major is the first number in the version series.

[Source]

    # File lib/more/facets/version.rb, line 81
81:   def major ; @self[0] ; end

Delegate to the array.

[Source]

     # 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.

[Source]

    # File lib/more/facets/version.rb, line 85
85:   def minor ; @self[1] || 0 ; end

Teeny is third number in the version series.

[Source]

    # File lib/more/facets/version.rb, line 89
89:   def teeny ; @self[2] || 0 ; end

[Source]

    # File lib/more/facets/version.rb, line 45
45:   def to_s ; @self.join('.') ; end

This is here only becuase File.join calls it instead of to_s.

[Source]

    # File lib/more/facets/version.rb, line 49
49:   def to_str ; @self.join('.') ; end

[Validate]