Class: Revision

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/contrib/revision.rb

Constant Summary collapse

DEBUG =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thing) ⇒ Revision

Returns a new instance of Revision.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/contrib/revision.rb', line 71

def initialize(thing)
  if thing.class == Revision
    @contents = thing.contents.dup
    # dup so we get a new copy, not a pointer to it.
  elsif thing.class == String
    result = thing.split(".").collect {|j| j.to_i}
    @contents = result
  elsif thing.class == Float
    @contents = thing.to_s.split(".").collect {|j| j.to_i}
  else
    raise "cannot initialise to #{thing}"
  end
  puts "initialize(#{thing}) => @contents is #{@contents}" if DEBUG
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



18
19
20
# File 'lib/contrib/revision.rb', line 18

def contents
  @contents
end

Instance Method Details

#<=>(x) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/contrib/revision.rb', line 42

def <=>(x)
  puts "Revision(#{self}).<=> called with (#{x})" if DEBUG 
  case x
  when String
    result = self <=> Revision.new(x)
  when Float
    f = self.to_f
    puts "#{f} <=> #{x}" if DEBUG
    result = self.to_f <=> x
  when Revision
    len = [@contents.size, x.contents.size].max 
    puts "len is #{len}" if DEBUG
    result = 0
    (0..(len-1)).each do |index|
      puts "result is #{result}" if DEBUG
      if result ==  0 
        result = ((@contents[index] || 0) <=> (x.contents[index] || 0))
      else
        break
      end
    end
    puts " finally result is #{result}" if DEBUG
    result
  else
    raise RevisionComparisonError.new("self is #{self}, other is #{x}")
  end
  result
end

#[](i) ⇒ Object



38
39
40
# File 'lib/contrib/revision.rb', line 38

def [](i)
  return @contents[i]
end

#to_fObject

Convert the Major.Minor to float. MinorMinor version number is difficult to handle because it depends on the size of Minor to determine how far right to shift it. Bacwards compatibility would necessitate 1 decimal place per revision, but if Ruby reaches 1.10.1 this will break. Therefore float comparisons should be deprecated.



34
35
36
# File 'lib/contrib/revision.rb', line 34

def to_f
  (@contents[0].to_s + '.' + contents[1]..to_s).to_f
end

#to_iObject



20
21
22
# File 'lib/contrib/revision.rb', line 20

def to_i
  @contents.join('').to_i
end

#to_sObject



24
25
26
# File 'lib/contrib/revision.rb', line 24

def to_s
  astring = @contents.collect { |j| j.to_s }.join(".")
end