Class: VersionCheck

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor = 0, update = 0, build = 0) ⇒ VersionCheck

Set up the version we are going to check against.

Parameters:

  • major (Integer|Float|String)

    Major version number or a partial or full string version e.g. “1.8.7”

  • minor (Integer) (defaults to: 0)

    Minor version number

  • update (Integer) (defaults to: 0)

    Update, within a minor version

  • build (Integer) (defaults to: 0)

    Patch or build, within an Update



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

#buildObject Also known as: patchlevel

Returns the value of attribute build.



28
29
30
# File 'lib/versioncheck.rb', line 28

def build
  @build
end

#majorObject

Returns the value of attribute major.



28
29
30
# File 'lib/versioncheck.rb', line 28

def major
  @major
end

#minorObject

Returns the value of attribute minor.



28
29
30
# File 'lib/versioncheck.rb', line 28

def minor
  @minor
end

#updateObject

Returns the value of attribute update.



28
29
30
# File 'lib/versioncheck.rb', line 28

def update
  @update
end

Class Method Details

.rubyversionArray

Short cut to setting up VersionCheck against current RUBY_VERSION and RUBY_PATCHLEVEL

Returns:

  • (Array)

    Major, minor, update, patch level, of the Ruby interpretor



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.

Parameters:

  • major (Integer|Float|String)

    Major version number or a partial or full string version e.g. “1.8.7”

  • minor (Integer) (defaults to: nil)

    Minor version number

  • update (Integer) (defaults to: nil)

    Update, within a minor version

  • build (Integer) (defaults to: nil)

    Patch or build, within an Update

Returns:

  • (Boolean)


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.

Parameters:

  • major (Integer|Float|String)

    Major version number or a partial or full string version e.g. “1.8.7”

  • minor (Integer) (defaults to: nil)

    Minor version number

  • update (Integer) (defaults to: nil)

    Update, within a minor version

  • build (Integer) (defaults to: nil)

    Patch or build, within an Update

Returns:

  • (Boolean)

    true if match to arguments is exact, otherwise false.



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_sString

to_s returns the current Ruby version and build as a string.

Returns:

  • (String)

    Version as a string



123
124
125
# File 'lib/versioncheck.rb', line 123

def to_s
  "#{@major}.#{@minor}.#{@update} Build #{@build}"
end