Class: TT::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
TT_Lib2/version.rb

Overview

Allows version comparisons based on a major, minor and revision system.

Since:

  • 2.6.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#new(major, minor, revision) ⇒ Version #new(version_array) ⇒ Version #new(version_string) ⇒ Version #new(float_version) ⇒ Version #new(version) ⇒ Version

Returns a new instance of Version

Overloads:

  • #new(major, minor, revision) ⇒ Version

    Parameters:

    • major (Integer)
    • minor (Integer)
    • revision (Integer)
  • #new(version_array) ⇒ Version

    Parameters:

    • version_array (Array)
  • #new(version_string) ⇒ Version

    Parameters:

    • version_string (String)

      Parse strings such as '1.2.3', '1.2' and '1'

  • #new(float_version) ⇒ Version

    Parameters:

    • float_version (Float)

      Float value of 1.2 become major: 1, minor: 2

  • #new(version) ⇒ Version

    Parameters:

Since:

  • 2.6.0



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'TT_Lib2/version.rb', line 50

def initialize( *args )
  # Validate argument size.
  if args.size == 1
    version = args[0]
  elsif args.size < 4
    version = args
  else
    raise ArgumentError, 'Wrong number of arguments.'
  end
  
  # Extract version into from known types into an array.
  if version.is_a?( String )
    version = version.split('.')
    
  elsif version.is_a?( Integer )
    version = [ version, 0,0 ]
    
  elsif version.is_a?( Float )
    major, minor = version.to_s.split('.')
    version = [ major, minor, 0 ]
    
  elsif version.is_a?( self.class )
    version = version.to_a
    
  end
  
  # Validate the processed version info.
  if version.is_a?( Array )
    # Validate array size.
    unless version.size > 0 && version.size < 4
      raise ArgumentError, 'Invalid version format.'
    end
    # Ensure everything to be an integer value.
    version.map! { |string| string.to_i }
    # All missing version info is set to zero.
    major, minor, revision = version
    major = 0 if major.nil?
    minor = 0 if minor.nil?
    revision = 0 if revision.nil?
  else
    raise ArgumentError, 'Invalid argument type.'
  end
  
  @major = major
  @minor = minor
  @revision = revision
end

Instance Attribute Details

#majorObject

Since:

  • 2.6.0



18
19
20
# File 'TT_Lib2/version.rb', line 18

def major
  @major
end

#minorObject

Since:

  • 2.6.0



18
19
20
# File 'TT_Lib2/version.rb', line 18

def minor
  @minor
end

#revisionObject

Since:

  • 2.6.0



18
19
20
# File 'TT_Lib2/version.rb', line 18

def revision
  @revision
end

Instance Method Details

#<=>(version) ⇒ Object

Since:

  • 2.6.0



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'TT_Lib2/version.rb', line 21

def <=>( version )
  if @major > version.major
    return 1
  end
  if @major == version.major
    if @minor > version.minor
      return 1
    end
    if @minor == version.minor
      return @revision <=> version.revision
    end
  end
  return -1
end

#inspectString

Returns:

  • (String)

Since:

  • 2.6.0



130
131
132
# File 'TT_Lib2/version.rb', line 130

def inspect
  "<#{self.class}::#{@major}.#{@minor}.#{@revision}>"
end

#to_aArray

Returns:

  • (Array)

Since:

  • 2.6.0



136
137
138
# File 'TT_Lib2/version.rb', line 136

def to_a
  [ @major, @minor, @revision ]
end

#to_sString

Returns:

  • (String)

Since:

  • 2.6.0



142
143
144
# File 'TT_Lib2/version.rb', line 142

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