Sha256: 5f868f21ff27316676e9b30fd966486d0a54541a30773283763b423a232603e6

Contents?: true

Size: 1 KB

Versions: 53

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module Avm
  # A sequence of segments of integer versions (Ex.: 2, 1.0.1, 3.0.0.0.0).
  class Version
    include ::Comparable

    SEGMENT_SEPARATOR = '.'

    class << self
      def segments_from_mixed(segments)
        (segments.is_a?(::Enumerable) ? segments.to_a : segments.to_s.split(SEGMENT_SEPARATOR))
          .map(&:to_i)
      end
    end

    attr_reader :segments

    def initialize(segments)
      @segments = self.class.segments_from_mixed(segments).freeze
    end

    delegate :[], to: :segments

    def <=>(other)
      segments <=> other.segments
    end

    def to_s
      segments.join(SEGMENT_SEPARATOR)
    end

    # @return [Avm::Version]
    def increment_segment(segment_index)
      x = [segments.count, segment_index + 1].max.times.map do |index|
        value = index < segments.count ? segments[index] : 0
        next value if index < segment_index
        next value + 1 if index == segment_index

        0
      end
      self.class.new x
    end
  end
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
avm-tools-0.102.2 lib/avm/version.rb
avm-tools-0.102.1 lib/avm/version.rb
avm-tools-0.102.0 lib/avm/version.rb
avm-tools-0.101.0 lib/avm/version.rb
avm-tools-0.100.0 lib/avm/version.rb
avm-tools-0.99.1 lib/avm/version.rb
avm-tools-0.99.0 lib/avm/version.rb
avm-tools-0.98.0 lib/avm/version.rb
avm-tools-0.97.0 lib/avm/version.rb
avm-tools-0.96.0 lib/avm/version.rb
avm-tools-0.95.0 lib/avm/version.rb
avm-tools-0.94.3 lib/avm/version.rb
avm-tools-0.94.2 lib/avm/version.rb
avm-tools-0.94.1 lib/avm/version.rb
avm-tools-0.94.0 lib/avm/version.rb
avm-tools-0.93.0 lib/avm/version.rb
avm-tools-0.92.0 lib/avm/version.rb
avm-tools-0.91.0 lib/avm/version.rb
avm-tools-0.90.0 lib/avm/version.rb
avm-tools-0.89.0 lib/avm/version.rb