Class: KXI::Application::VersionExpression

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

Overview

Represents a comparison of two semantic versions

Constant Summary collapse

COMP_EQ =

Equivalence comparison

'='
COMP_LEQ =

Less than or equivalent to comparison

'<='
COMP_LT =

Less than comparison

'<'
COMP_GEQ =

Grater than or equivalent to comparison

'>='
COMP_GT =

Grater than comparison

'>'
COMP_AT =

Minimal requirement comparison

'~>'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comp, major, minor = nil, patch = nil) ⇒ VersionExpression

Instantiates the KXI::Application::VersionExpression class

Parameters:

  • comp (string)

    Comparison rule of expression

  • major (int)

    Major number of left-side version

  • minor (int, nil) (defaults to: nil)

    Minor number of left-side version

  • patch (int, nil) (defaults to: nil)

    Patch number of left-side version



49
50
51
52
53
54
55
# File 'lib/kxi/application/version_expression.rb', line 49

def initialize(comp, major, minor = nil, patch = nil)
	@comp   = comp
	@major  = major
	@minor  = minor
	@patch  = patch
	@target = KXI::Application::Version.new(major, minor == nil ? 0 : minor, patch == nil ? 0 : patch)
end

Class Method Details

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

Parses a version expression from its string form

Parameters:

  • str (string)

    String representation of version expression

Returns:

Raises:



88
89
90
91
92
# File 'lib/kxi/application/version_expression.rb', line 88

def self.parse(str)
	m = /^\s*(?'cm'>|<|>=|<=|=|~>)\s*(?'mj'\d+)\s*(\.\s*(?'mi'\d+))?\s*(\.\s*(?'pt'\d+))?\s*?$/m.match(str)
	raise(KXI::Exceptions::ParseException.new('version expression', str)) if m == nil
	return VersionExpression.new(m['cm'], m['mj'].to_i, m['mi']&.to_i, m['pt']&.to_i)
end

Instance Method Details

#comparisonstring

Get the comparison of versions

Returns:

  • (string)

    Comparison of versions



22
23
24
# File 'lib/kxi/application/version_expression.rb', line 22

def comparison
	@comp
end

#majorint

Gets the major number of left-side version

Returns:

  • (int)

    Major number version



28
29
30
# File 'lib/kxi/application/version_expression.rb', line 28

def major
	@major
end

#matches?(ver) ⇒ bool

Checks whether expression is true for given version

Parameters:

Returns:

  • (bool)

    True if expression is valid; otherwise false



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kxi/application/version_expression.rb', line 60

def matches?(ver)
	case @comp
		when COMP_EQ
			return (
			ver.major == @major and
				ver.minor == (@minor == nil ? 0 : @minor) and
				ver.patch == (@patch == nil ? 0 : @patch)
			)
		when COMP_AT
			return (
			ver.major == @major and
				(@minor == nil or ver.minor == @minor) and
				(@patch == nil or ver.patch == @patch)
			)
		when COMP_GEQ
			return ver >= @target
		when COMP_LEQ
			return ver <= @target
		when COMP_GT
			return ver > @target
		when COMP_LT
			return ver < @target
	end
end

#minorint?

Gets the minor number of left-side version

Returns:

  • (int, nil)

    Minor number version



34
35
36
# File 'lib/kxi/application/version_expression.rb', line 34

def minor
	@minor
end

#patchint?

Gets the patch number of left-side version

Returns:

  • (int, nil)

    Patch number version



40
41
42
# File 'lib/kxi/application/version_expression.rb', line 40

def patch
	@patch
end