Class: WolfTrans::Version

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

Overview

Version; represents a major/minor version scheme

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major: nil, minor: nil, flt: nil, str: nil) ⇒ Version

Returns a new instance of Version.



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

def initialize(major: nil, minor: nil, flt: nil, str: nil)
  # See if we need to parse from a string
  if flt
    string = flt.to_s
  elsif str
    string = str
  else
    string = nil
  end

  # Extract major and minor numbers
  if string
    if match = string.match(/(\d+)\.(\d+)/)
      @major, @minor = match.captures.map { |s| s.to_i }
    else
      raise "could not parse version string '#{string}'"
    end
  elsif major && minor
    @major = major
    @minor = minor
  else
    @major = 0
    @minor = 0
  end
end

Instance Attribute Details

#majorObject

Returns the value of attribute major.



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

def major
  @major
end

#minorObject

Returns the value of attribute minor.



39
40
41
# File 'lib/wolftrans.rb', line 39

def minor
  @minor
end

Instance Method Details

#<=>(other) ⇒ Object



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

def <=>(other)
  case other
  when Version
    return 0 if major == other.major && minor == other.minor
    return 1 if major > other.major || (major == other.major && minor >= other.minor)
    return -1
  when String
    return self == Version.new(str = other)
  when Float
    return self == Version.new(flt = other)
  end
  return nil
end

#to_sObject



67
68
69
# File 'lib/wolftrans.rb', line 67

def to_s
  "#{@major}.#{@minor}"
end