class DataMetaDom::SemVer

Semantic Version implementation.

See {semver.org this page} for details

Constants

BUILD_INDEX

Build part index in the @items array

DIGITS

Any Integral part of the Version - just digits

DOTS_SPLIT

Split by dots pattern

EQ

Equality for the saucer operator <=>

GT

Strictly “greater than” for the saucer operator <=>

ITEMS_MAX_SIZE

Max size of the @items array

ITEMS_MIN_SIZE

Minimal size of the @items array

LT

Strictly “lesser than” for the saucer operator <=>

MAJOR_INDEX

Major part index in the @items array

MINOR_INDEX

Minor part index in the @items array

UPDATE_INDEX

Update part index in the @items array

Attributes

items[R]
semanticPartsOnly[R]
source[R]

Public Class Methods

fromSpecs(specs) click to toggle source

Builds an instance of SemVer from the given specs whatever they are

# File lib/dataMetaDom/ver.rb, line 153
def fromSpecs(specs)
    case specs
        when SemVer
            specs
        when String
            SemVer.new(specs)
        else
            raise ArgumentError, %<Unsupported SemVer specs type #{specs}==#{specs.inspect}>
    end
end
new(src) click to toggle source

Parsing constructor

# File lib/dataMetaDom/ver.rb, line 52
def initialize(src)
    raise ArgumentError, "Attempted to create an instance of #{self.class.name} from a nil" if src.nil?
    @source = src
    # put everything in an array -- this provides free eql? and hash() methods
    @items = []
    src.split(DOTS_SPLIT).each { |i|
        if i =~ DIGITS
            @items << i.to_i
        else
            break
        end
    }
    raise ArgumentError, %<Invalid semantic version format: #{src}> if items.size < ITEMS_MIN_SIZE ||
            items.size > ITEMS_MAX_SIZE

    raise ArgumentError,
          %<Invalid semantic version format: "#{src}": build version can not be zero.> unless build.nil? ||
            build != 0

    @semanticPartsOnly  = @items.map{|i| i.to_s}.join('.')

end

Public Instance Methods

<(other) click to toggle source

Overload the lesser-than operator

# File lib/dataMetaDom/ver.rb, line 145
def <(other); self.<=>(other) == LT end
<=(other) click to toggle source

Overload the lesser-than-or-equals operator

# File lib/dataMetaDom/ver.rb, line 149
def <=(other); self.<=>(other) <= EQ end
<=>(o) click to toggle source

The Saucer Operator, Ruby equivalent of Java's compareTo(…)

# File lib/dataMetaDom/ver.rb, line 109
def <=>(o)
    raise ArgumentError, %<Attempt to compare #{self.class.name} "#{self}" to a nil> if o.nil?

    0.upto(UPDATE_INDEX) { |x|
        cmp = items[x] <=> o.items[x]
        return cmp unless cmp == EQ #  not equal: end of the story, that's the comparison result
    }
    # if we are here, the Minor, Major and the Update are equal. See what's up with the build if any:

    # this object is newer (version bigger) because it has a build number but the other does not
    return GT if items.size > o.items.size

    # this object is older (version lesser) because it does not have a build number but the other does
    return LT if items.size < o.items.size

    # We got build part in self and the other, return the build part comparison:
    build <=> o.build
end
==(other) click to toggle source

Overload the equals operator

# File lib/dataMetaDom/ver.rb, line 141
def ==(other); self.<=>(other) == EQ end
>(other) click to toggle source

Overload the greater-than operator

# File lib/dataMetaDom/ver.rb, line 143
def >(other); self.<=>(other) == GT end
>=(other) click to toggle source

Overload the greater-than-or-equals operator

# File lib/dataMetaDom/ver.rb, line 147
def >=(other); self.<=>(other) >= EQ end
build() click to toggle source

Build part of the version or nil

# File lib/dataMetaDom/ver.rb, line 85
def build; items.size > BUILD_INDEX ? @items[BUILD_INDEX] : nil end
diffLevel(other) click to toggle source

Difference Level, computes one of the DiffLevel values

# File lib/dataMetaDom/ver.rb, line 88
def diffLevel(other)
    return DiffLevel::MAJOR if major != other.major
    return DiffLevel::MINOR if minor != other.minor
    return DiffLevel::UPDATE if update != other.update
    if (
       !build.nil? && !other.build.nil? && (build <=> other.build) != EQ
    ) || (
       build.nil? && !other.build.nil?
    ) || ( !build.nil? && other.build.nil? )
        return DiffLevel::BUILD
    end
    DiffLevel::NONE
end
eql?(o) click to toggle source

Override the eql? for the == operator to work

# File lib/dataMetaDom/ver.rb, line 103
def eql?(o); @items == o.items end
hash() click to toggle source

Override the hash() method for the sets and maps to work

# File lib/dataMetaDom/ver.rb, line 106
def hash; @items.hash end
major() click to toggle source

Major part of the version

# File lib/dataMetaDom/ver.rb, line 76
def major; @items[MAJOR_INDEX] end
minor() click to toggle source

Minor part of the version

# File lib/dataMetaDom/ver.rb, line 79
def minor; @items[MINOR_INDEX] end
toVarName() click to toggle source

Consistently and reproducibly convert the version specs to the text suitable for making it a part of a class name or a variable name

# File lib/dataMetaDom/ver.rb, line 138
def toVarName; @items.join('_') end
to_long_s() click to toggle source

Long string for debugging and detailed logging - shows semantic parts as parsed

# File lib/dataMetaDom/ver.rb, line 132
def to_long_s; "#{self.class.name}{#{@source}(#{@semanticPartsOnly})}" end
to_s() click to toggle source

For a simple string representation, just show the source

# File lib/dataMetaDom/ver.rb, line 129
def to_s; @source end
update() click to toggle source

Update part of the version

# File lib/dataMetaDom/ver.rb, line 82
def update; @items[UPDATE_INDEX] end