Class: Version

Inherits:
Object
  • Object
show all
Defined in:
lib/atk/version.rb

Overview

create a variable for the current ruby version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_as_string) ⇒ Version

Returns a new instance of Version.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/atk/version.rb', line 13

def initialize(version_as_string)
    @levels = version_as_string.split('.')
    @comparable = @levels[0] =~ /\A\d+\z/
    # convert values into integers where possible
    index = -1
    for each in @levels.dup
        index += 1
        if each =~ /\A\d+\z/
            @levels[index] = each.to_i
        end
    end
    @major, @minor, @patch, *_ = @levels
end

Instance Attribute Details

#codenameObject

Returns the value of attribute codename.



4
5
6
# File 'lib/atk/version.rb', line 4

def codename
  @codename
end

#levelsObject

Returns the value of attribute levels.



4
5
6
# File 'lib/atk/version.rb', line 4

def levels
  @levels
end

Class Method Details

.extract_from(string) ⇒ Object



6
7
8
9
10
11
# File 'lib/atk/version.rb', line 6

def self.extract_from(string)
    match = string.match(/\d+\.\d+(\.\d+)*/)
    if match != nil
        return Version.new(match[0])
    end
end

Instance Method Details

#<(other_version) ⇒ Object



80
81
82
83
# File 'lib/atk/version.rb', line 80

def <(other_version)
    value = (self <=> other_version)
    return value && value == -1
end

#<=>(other_version) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/atk/version.rb', line 44

def <=>(other_version)
    if not other_version.is_a?(Version)
        raise "When doing version comparision, both sides must be a version object"
    end
    if other_version.to_s == self.to_s
        return 0
    end
    
    if other_version.comparable? && self.comparable?
        self_levels = @levels.dup
        other_levels = other_version.levels.dup
        loop do
            if self_levels.size == 0 || other_levels.size == 0
                if self_levels.size > other_levels.size
                    return 1
                elsif self_levels.size < other_levels.size
                    return -1
                else
                    return 0
                end
            end
            comparision = self_levels.shift() <=> other_levels.shift()
            if comparision != 0
                return comparision
            end
        end
    else
        return nil
    end
end

#==(other_version) ⇒ Object



85
86
87
88
# File 'lib/atk/version.rb', line 85

def ==(other_version)
    value = (self <=> other_version)
    return value && value == 0
end

#>(other_version) ⇒ Object



75
76
77
78
# File 'lib/atk/version.rb', line 75

def >(other_version)
    value = (self <=> other_version)
    return value && value == 1
end

#comparable?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/atk/version.rb', line 40

def comparable?
    return @comparable
end

#majorObject



35
# File 'lib/atk/version.rb', line 35

def major() @major end

#major=(new_value) ⇒ Object



36
37
38
# File 'lib/atk/version.rb', line 36

def major=(new_value)
    @levels[0] = new_value
end

#minorObject



31
# File 'lib/atk/version.rb', line 31

def minor() @minor end

#minor=(new_value) ⇒ Object



32
33
34
# File 'lib/atk/version.rb', line 32

def minor=(new_value)
    @levels[1] = new_value
end

#patchObject



27
# File 'lib/atk/version.rb', line 27

def patch() @patch end

#patch=(new_value) ⇒ Object



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

def patch=(new_value)
    @levels[2] = new_value
end

#to_sObject



90
91
92
# File 'lib/atk/version.rb', line 90

def to_s
    return @levels.map(&:to_s).join('.')
end