Sha256: 3f81d5b639fa96f87d1008a3949651c86d21a707b5f59712d4f0b666609a143b

Contents?: true

Size: 1.76 KB

Versions: 34

Compression:

Stored size: 1.76 KB

Contents

require 'tins/xt/symbol_to_proc'

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(&: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

34 entries across 33 versions & 3 rubygems

Version Path
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/lib/tins/string_version.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/lib/tins/string_version.rb
tdiary-5.0.8 vendor/bundle/gems/tins-0.13.2/lib/tins/string_version.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/tins-0.13.2/lib/tins/string_version.rb
tins-1.3.0 lib/tins/string_version.rb
tins-1.2.0 lib/tins/string_version.rb
tins-1.1.0 lib/tins/string_version.rb
tins-1.0.1 lib/tins/string_version.rb
tins-1.0.0 lib/tins/string_version.rb
tins-0.13.2 lib/tins/string_version.rb
social_url_stats-0.0.1 vendor/ruby/1.9.1/gems/tins-0.13.1/lib/tins/string_version.rb
tins-0.13.1 lib/tins/string_version.rb
tins-0.13.0 lib/tins/string_version.rb
tins-0.12.0 lib/tins/string_version.rb
tins-0.11.0 lib/tins/string_version.rb
tins-0.10.0 lib/tins/string_version.rb
tins-0.9.0 lib/tins/string_version.rb
tins-0.8.4 lib/tins/string_version.rb
tins-0.8.3 lib/tins/string_version.rb
tins-0.8.2 lib/tins/string_version.rb