class KXI::Application::VersionExpression

Represents a comparison of two semantic versions

Constants

COMP_AT

Minimal requirement comparison

COMP_EQ

Equivalence comparison

COMP_GEQ

Grater than or equivalent to comparison

COMP_GT

Grater than comparison

COMP_LEQ

Less than or equivalent to comparison

COMP_LT

Less than comparison

Public Class Methods

new(comp, major, minor = nil, patch = nil) click to toggle source

Instantiates the {KXI::Application::VersionExpression} class @param [string] comp Comparison rule of expression @param [int] major Major number of left-side version @param [int,nil] minor Minor number of left-side version @param [int,nil] patch Patch number of left-side version

# 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
parse(str) click to toggle source

Parses a version expression from its string form @param [string] str String representation of version expression @return [KXI::Application::VersionExpression] Version expression equivalent to given string

# 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

Public Instance Methods

comparison() click to toggle source

Get the comparison of versions @return [string] Comparison of versions

# File lib/kxi/application/version_expression.rb, line 22
def comparison
        @comp
end
major() click to toggle source

Gets the major number of left-side version @return [int] Major number version

# File lib/kxi/application/version_expression.rb, line 28
def major
        @major
end
matches?(ver) click to toggle source

Checks whether expression is true for given version @param ver [KXI::Application::Version] Right-side version of expression @return [bool] True if expression is valid; otherwise false

# 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
minor() click to toggle source

Gets the minor number of left-side version @return [int,nil] Minor number version

# File lib/kxi/application/version_expression.rb, line 34
def minor
        @minor
end
patch() click to toggle source

Gets the patch number of left-side version @return [int,nil] Patch number version

# File lib/kxi/application/version_expression.rb, line 40
def patch
        @patch
end