Class: KXI::Application::Version

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

Overview

Represents a version of semantic versioning 2.0.0 (semver.org)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor = 0, patch = 0, id = nil, meta = nil) ⇒ Version

Instantiates the KXI::Application::Version class

Parameters:

  • major (integer)

    Major number of version

  • minor (integer) (defaults to: 0)

    Minor number of version

  • patch (integer) (defaults to: 0)

    Patch number of version

  • id (string, nil) (defaults to: nil)

    Identifier of version

  • meta (string, nil) (defaults to: nil)

    Metadata of version



43
44
45
46
47
48
49
# File 'lib/kxi/application/version.rb', line 43

def initialize(major, minor = 0, patch = 0, id = nil, meta = nil)
	@major = major
	@minor = minor
	@patch = patch
	@id    = id
	@meta  = meta
end

Class Method Details

.parse(str) ⇒ KXI::Application::Version

Parses a version from its string representation

Parameters:

  • str (string)

    String representation of the version

Returns:

Raises:



100
101
102
103
104
# File 'lib/kxi/application/version.rb', line 100

def self.parse(str)
	m = /^\s*(?'ma'\d+)\s*(\.\s*(?'mi'\d+))?\s*(\.\s*(?'pt'\d+))?\s*(-\s*(?'id'[0-9A-Z\-.]+))?\s*(\+\s*(?'mt'[0-9A-Z\-.]+))?\s*$/mi.match(str)
	raise(KXI::Exceptions::ParseException.new('version', str)) if m == nil
	return Version.new(m['ma'].to_i, m['mi'] != nil ? m['mi'].to_i : 0, m['pt'] != nil ? m['pt'].to_i : 0, m['id'], m['mt'])
end

Instance Method Details

#<(other) ⇒ bool

Checks whether this version is lower than other version

Returns:

  • (bool)

    True when this version is lower than the other; false otherwise

Raises:



66
67
68
69
70
71
72
# File 'lib/kxi/application/version.rb', line 66

def <(other)
	raise(KXI::Exceptions::InvalidTypeException.new(other.class, KXI::Application::Version)) unless other.is_a?(KXI::Application::Version)
	return true if @major < other.major or @minor < other.minor or @patch < other.patch
	return true if @id != nil and other.identifier == nil
	return true if @id != nil and other.identifier != nil and @id < other.identifier
	return false
end

#<=(other) ⇒ bool

Checks whether this version is lower than or equivalent to other version

Returns:

  • (bool)

    True when this version is lower than or equivalent to the other; false otherwise

Raises:



76
77
78
79
80
81
82
83
# File 'lib/kxi/application/version.rb', line 76

def <=(other)
	raise(KXI::Exceptions::InvalidTypeException.new(other.class, KXI::Application::Version)) unless other.is_a?(KXI::Application::Version)
	return true if self == other
	return true if @major < other.major or @minor < other.minor or @patch < other.patch
	return true if @id != nil and other.identifier == nil
	return true if @id != nil and other.identifier != nil and @id < other.identifier
	return false
end

#==(other) ⇒ bool

Checks whether this version is equivalent to other version

Returns:

  • (bool)

    True when versions are equivalent; false otherwise



59
60
61
62
# File 'lib/kxi/application/version.rb', line 59

def ==(other)
	return false unless other.is_a?(KXI::Application::Version)
	return (@major == other.major and @minor == other.minor and @patch == other.patch and @id == other.identifier)
end

#>(other) ⇒ bool

Checks whether this version is grater than other version

Returns:

  • (bool)

    True when this version is grater than the other; false otherwise



87
88
89
# File 'lib/kxi/application/version.rb', line 87

def >(other)
	return (not self <= other)
end

#>=(other) ⇒ bool

Checks whether this version is grater than or equivalent to other version

Returns:

  • (bool)

    True when this version is grater than or equivalent to the other; false otherwise



93
94
95
# File 'lib/kxi/application/version.rb', line 93

def >=(other)
	return (not self < other)
end

#identifierstring

Gets the identifier of version

Returns:

  • (string)

    Identifier of version



27
28
29
# File 'lib/kxi/application/version.rb', line 27

def identifier
	@id
end

#majorinteger

Gets the major number of version

Returns:

  • (integer)

    Major number of version



9
10
11
# File 'lib/kxi/application/version.rb', line 9

def major
	@major
end

#metadatastring

Gets the metadata of version

Returns:

  • (string)

    Metadata of version



33
34
35
# File 'lib/kxi/application/version.rb', line 33

def 
	@meta
end

#minorinteger

Gets the number minor of version

Returns:

  • (integer)

    Minor number of version



15
16
17
# File 'lib/kxi/application/version.rb', line 15

def minor
	@minor
end

#patchinteger

Gets the number patch of version

Returns:

  • (integer)

    Patch number of version



21
22
23
# File 'lib/kxi/application/version.rb', line 21

def patch
	@patch
end

#to_sstring

Converts class to string representation

Returns:

  • (string)

    String representation of class



53
54
55
# File 'lib/kxi/application/version.rb', line 53

def to_s
	"#{@major}.#{@minor}.#{@patch}#{@id == nil ? '' : "-#{@id}"}#{@meta == nil ? '' : "+#{@meta}"}"
end