Sha256: 561f9c973dd07006c5a8adfa54cb810bb6c8910b84abefcdf67f6ee45c49c616

Contents?: true

Size: 995 Bytes

Versions: 5

Compression:

Stored size: 995 Bytes

Contents

module Kuby
  module Utils
    module SemVer
      class Version
        include Comparable

        attr_reader :major, :minor, :patch

        def self.parse(str, default: 0)
          major, minor, patch = str.split('.')

          new(
            major ? major.to_i : default,
            minor ? minor.to_i : default,
            patch ? patch.to_i : default
          )
        end

        def initialize(major, minor, patch)
          @major = major
          @minor = minor
          @patch = patch
        end

        def to_s
          @str ||= [major, minor, patch].compact.join('.')
        end

        def to_a
          @arr ||= [major, minor, patch]
        end

        def <=>(other)
          other_arr = other.to_a

          to_a.each_with_index do |digit, idx|
            other_digit = other_arr[idx] || 0

            if digit != other_digit
              return digit <=> other_digit
            end
          end

          0
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
kuby-core-0.20.2 lib/kuby/utils/sem_ver/version.rb
kuby-core-0.20.1 lib/kuby/utils/sem_ver/version.rb
kuby-core-0.20.0 lib/kuby/utils/sem_ver/version.rb
kuby-core-0.19.0 lib/kuby/utils/sem_ver/version.rb
kuby-core-0.18.0 lib/kuby/utils/sem_ver/version.rb