Class: VersionCheck
- Inherits:
-
Object
- Object
- VersionCheck
- Defined in:
- lib/versioncheck.rb
Overview
VersionCheck is a ruby class provides tests against a packages version so we can determine if our code is running in the version we want or is running on a version later than the version we need.
Naming convention used: RUBY_VERSION 1.8.7 patch level 249 is Rubyvesion.major = 1, VersionCheck.minor = 8, VersionCheck.update = 7 VersionCheck.build = 249 or VersionCheck.patchlevel = 249
Special initializer, VersionCheck.rubyversion, sets up VersionCheck instance we can use to check against current Ruby major,minor,update and patch level. Methods can take: a single float argument. e.g. VersionCheck.have_version?(1.8) Tests for version 1.8 with any update or build version a single string argument. e.g. VersionCheck.have_version?(“1”) Tests for version 1 with any minor, update or build version e.g. VersionCheck.have_version?(“1.8”) Tests for version 1.8 with any update or build version VersionCheck.have_version?(“1.8.7”) Tests for version 1.8.7 with and build version Integer arguments major, minor, update and build e.g. VersionCheck.have_version?(1) Tests for version 1 with any minor, update or build version VersionCheck.have_version?(1,8) Tests for version 1.8 with any update or build version VersionCheck.have_version?(1,8,7) Tests for version 1.8.7 with and build version VersionCheck.have_version?(1,8,7,249) Test for 1.8.7 build 249
Constant Summary collapse
- VERSION =
version of class VersionCheck
'1.0.0'
Instance Attribute Summary collapse
-
#build ⇒ Object
(also: #patchlevel)
Returns the value of attribute build.
-
#major ⇒ Object
Returns the value of attribute major.
-
#minor ⇒ Object
Returns the value of attribute minor.
-
#update ⇒ Object
Returns the value of attribute update.
Class Method Summary collapse
-
.rubyversion ⇒ Array
Short cut to setting up VersionCheck against current RUBY_VERSION and RUBY_PATCHLEVEL.
Instance Method Summary collapse
-
#have_at_least_version?(major, minor = nil, update = nil, build = nil) ⇒ Boolean
have_at_least_version? tests that the versien of RUBY is newer than the one we want.
-
#have_version?(major, minor = nil, update = nil, build = nil) ⇒ Boolean
have_version? tests that the versien of RUBY is the one we want.
-
#initialize(major, minor = 0, update = 0, build = 0) ⇒ VersionCheck
constructor
Set up the version we are going to check against.
-
#to_s ⇒ String
to_s returns the current Ruby version and build as a string.
Constructor Details
#initialize(major, minor = 0, update = 0, build = 0) ⇒ VersionCheck
Set up the version we are going to check against.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/versioncheck.rb', line 37 def initialize(major,minor=0,update=0,build=0) if major.class == Float major = major.to_s end if major.class == String major,minor,update = major.split('.').collect { |x| x.to_i } end @major = major != nil ? major.to_i : 0 @minor = minor != nil ? minor.to_i : 0 @update = update != nil ? update.to_i : 0 @build = build != nil ? build.to_i : 0 end |
Instance Attribute Details
#build ⇒ Object Also known as: patchlevel
Returns the value of attribute build.
28 29 30 |
# File 'lib/versioncheck.rb', line 28 def build @build end |
#major ⇒ Object
Returns the value of attribute major.
28 29 30 |
# File 'lib/versioncheck.rb', line 28 def major @major end |
#minor ⇒ Object
Returns the value of attribute minor.
28 29 30 |
# File 'lib/versioncheck.rb', line 28 def minor @minor end |
#update ⇒ Object
Returns the value of attribute update.
28 29 30 |
# File 'lib/versioncheck.rb', line 28 def update @update end |
Class Method Details
.rubyversion ⇒ Array
Short cut to setting up VersionCheck against current RUBY_VERSION and RUBY_PATCHLEVEL
53 54 55 56 |
# File 'lib/versioncheck.rb', line 53 def self.rubyversion major,minor,update = RUBY_VERSION.split('.').collect { |i| i.to_i } self.new(major,minor,update, RUBY_PATCHLEVEL.to_i) end |
Instance Method Details
#have_at_least_version?(major, minor = nil, update = nil, build = nil) ⇒ Boolean
have_at_least_version? tests that the versien of RUBY is newer than the one we want. If update and or build are missing, then any update or build version will return true, as long as the major and minor numbers match, or the running Ruby verion major.minor numbers are for a newer version.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/versioncheck.rb', line 92 def have_at_least_version?(major, minor = nil, update = nil, build = nil) if major.class == Float major = major.to_s end if major.class == String major,minor,update = major.split('.').collect { |x| x.to_i } end if major == @major #Could true if minor != nil #Being asked to test minor level if minor == @minor #Could be true if update != nil #Being asked to test update level if update == @update #Could be true if build != nil #Being asked to test the build version return build <= @build #have at least the required patch level. end return true #update was equal end return update < @update #current version is newer end return true #major and minor was equal. end return minor < @minor #true if current version is newer end return true #major was equal. end return major < @major #true, if current version is newer end |
#have_version?(major, minor = nil, update = nil, build = nil) ⇒ Boolean
have_version? tests that the versien of RUBY is the one we want. If update and build are not specified, they are not tested for.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/versioncheck.rb', line 66 def have_version?(major, minor = nil, update = nil, build = nil) if major.class == Float major = major.to_s end if major.class == String major,minor,update = major.split('.').collect { |x| x.to_i } end if major == @major return false if minor != nil && minor != @minor return false if update != nil && update != @update return false if build != nil && build != @build return true else return false end end |
#to_s ⇒ String
to_s returns the current Ruby version and build as a string.
123 124 125 |
# File 'lib/versioncheck.rb', line 123 def to_s "#{@major}.#{@minor}.#{@update} Build #{@build}" end |