Class: VersionNumber

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

Overview

VersionNumber

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Comparable

#at_least, #at_most, #cap, #clip, #cmp

Constructor Details

#initialize(*args) ⇒ VersionNumber

Returns a new instance of VersionNumber.



45
46
47
48
# File 'lib/more/facets/version.rb', line 45

def initialize( *args )
  args = args.join('.').split(/\W+/)
  @self = args.collect { |i| i.to_i }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &blk) ⇒ Object

Delegate to the array.



98
99
100
# File 'lib/more/facets/version.rb', line 98

def method_missing( sym, *args, &blk )
  @self.send(sym, *args, &blk ) rescue super
end

Class Method Details

.constraint_lambda(constraint) ⇒ Object

Parses a string constraint returning the operation as a lambda.



104
105
106
107
# File 'lib/more/facets/version.rb', line 104

def self.constraint_lambda( constraint )
  op, val = *parse_constraint( constraint )
  lambda { |t| t.send(op, val) }
end

.parse_constraint(constraint) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/more/facets/version.rb', line 109

def self.parse_constraint( constraint )
  constraint = constraint.strip
  re = %r{^(=~|~>|<=|>=|==|=|<|>)?\s*(\d+(:?[-.]\d+)*)$}
  if md = re.match( constraint )
    if op = md[1]
      op = '=~' if op == '~>'
      op = '==' if op == '='
      val = new( *md[2].split(/\W+/) )
    else
      op = '=='
      val = new( *constraint.split(/\W+/) )
    end
  else
    raise ArgumentError, "invalid constraint"
  end
  return op, val
end

Instance Method Details

#<=>(other) ⇒ Object

“Spaceship” comparsion operator.



66
67
68
69
70
71
72
73
# File 'lib/more/facets/version.rb', line 66

def <=>( other )
  #other = other.to_t
  [@self.size, other.size].max.times do |i|
    c = @self[i] <=> other[i]
    return c if c != 0
  end
  0
end

#=~(other) ⇒ Object

For pessimistic constraint (like ‘~>’ in gems)



77
78
79
80
81
82
# File 'lib/more/facets/version.rb', line 77

def =~( other )
  #other = other.to_t
  upver = other.dup
  upver[0] += 1
  @self >= other and @self < upver
end

#[](i) ⇒ Object



60
61
62
# File 'lib/more/facets/version.rb', line 60

def [](i)
  @self.fetch(i,0)
end

#inspectObject



56
57
58
# File 'lib/more/facets/version.rb', line 56

def inspect
  @self.to_s
end

#majorObject

Major is the first number in the version series.



86
# File 'lib/more/facets/version.rb', line 86

def major ; @self[0] ; end

#minorObject

Minor is the second number in the version series.



90
# File 'lib/more/facets/version.rb', line 90

def minor ; @self[1] || 0 ; end

#teenyObject

Teeny is third number in the version series.



94
# File 'lib/more/facets/version.rb', line 94

def teeny ; @self[2] || 0 ; end

#to_sObject



50
# File 'lib/more/facets/version.rb', line 50

def to_s ; @self.join('.') ; end

#to_strObject

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



54
# File 'lib/more/facets/version.rb', line 54

def to_str ; @self.join('.') ; end