Sha256: 8ec091b871e0bf9cedc985b78871025037ddf001bb90ab5588348c14f57e0a5c

Contents?: true

Size: 1.73 KB

Versions: 8

Compression:

Stored size: 1.73 KB

Contents

module Tins
  module StringVersion
    class Version
      include Comparable

      def initialize(string)
        string =~ /\A[\.\d]+\z/ or raise ArgumentError, "#{string.inspect} isn't a version number"
        @version = string.frozen? ? string.dup : string
      end

      def major
        self[0]
      end

      def major=(number)
        self[0] = number
      end

      def minor
        self[1]
      end

      def minor=(number)
        self[1] = number
      end

      def build
        self[2]
      end

      def build=(number)
        self[2] = number
      end

      def revision
        self[3]
      end

      def revision=(number)
        self[3] = number
      end

      def [](index)
        array[index]
      end

      def []=(index, value)
        value = value.to_i
        value >= 0 or raise ArgumentError, "version numbers can't contain negative numbers like #{value}"
        a = array
        @array = nil
        a[index] = value
        a.map! { |x| x.nil? ? 0 : x }
        @version.replace a * '.'
      end

      def succ!
        self[-1] += 1
      end

      def pred!
        self[-1] -= 1
      end

      def <=>(other)
        pairs = array.zip(other.array)
        pairs.map! { |a, b| [ a.nil? ? 0 : a, b.nil? ? 0 : b ] }
        a, b = pairs.transpose
        a <=> b
      end

      def ==(other)
        (self <=> other).zero?
      end

      def array
        @version.split('.').map { |x| x.to_i }
      end

      alias to_a array

      def to_s
        @version
      end

      alias inspect to_s

      def version
        self
      end
    end

    def version
      if frozen?
        Version.new(self)
      else
        @version ||= Version.new(self)
      end
    end
  end
end

require 'tins/alias'

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
tins-1.4.2 lib/tins/string_version.rb
tins-1.4.1 lib/tins/string_version.rb
tins-1.4.0 lib/tins/string_version.rb
tins-1.3.5 lib/tins/string_version.rb
tins-1.3.4 lib/tins/string_version.rb
tins-1.3.3 lib/tins/string_version.rb
tins-1.3.2 lib/tins/string_version.rb
tins-1.3.1 lib/tins/string_version.rb