Class: TT::Version
Overview
Allows version comparisons based on a major, minor and revision system.
Instance Attribute Summary collapse
Instance Method Summary collapse
- #<=>(version) ⇒ Object
-
#initialize(*args) ⇒ Version
constructor
A new instance of Version.
- #inspect ⇒ String
- #to_a ⇒ Array
- #to_s ⇒ String
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
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
#major ⇒ Object
18 19 20 |
# File 'TT_Lib2/version.rb', line 18 def major @major end |
#minor ⇒ Object
18 19 20 |
# File 'TT_Lib2/version.rb', line 18 def minor @minor end |
#revision ⇒ Object
18 19 20 |
# File 'TT_Lib2/version.rb', line 18 def revision @revision end |
Instance Method Details
#<=>(version) ⇒ Object
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 |
#inspect ⇒ String
130 131 132 |
# File 'TT_Lib2/version.rb', line 130 def inspect "<#{self.class}::#{@major}.#{@minor}.#{@revision}>" end |
#to_a ⇒ Array
136 137 138 |
# File 'TT_Lib2/version.rb', line 136 def to_a [ @major, @minor, @revision ] end |
#to_s ⇒ String
142 143 144 |
# File 'TT_Lib2/version.rb', line 142 def to_s "#{@major}.#{@minor}.#{@revision}" end |