Sha256: 8475eba1d5e17a13ef1108ee809fb082862b4b50126dc2be19f3d46cae31cedd

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

module Solve
  # @author Jamie Winsor <jamie@vialstudios.com>
  class Version
    class << self
      # @param [#to_s] version_string
      #
      # @raise [InvalidVersionFormat]
      #
      # @return [Array]
      def split(version_string)
        major, minor, patch = case version_string.to_s
        when /^(\d+)\.(\d+)\.(\d+)$/
          [ $1.to_i, $2.to_i, $3.to_i ]
        when /^(\d+)\.(\d+)$/
          [ $1.to_i, $2.to_i, nil ]
        else
          raise Errors::InvalidVersionFormat.new(version_string)
        end
      end
    end

    include Comparable

    attr_reader :major
    attr_reader :minor
    attr_reader :patch

    # @overload initialize(version_array)
    #   @param [Array] version_array
    #
    #   @example
    #     Version.new([1, 2, 3]) => #<Version: @major=1, @minor=2, @patch=3>
    #
    # @overload initialize(version_string)
    #   @param [#to_s] version_string
    #
    #   @example
    #     Version.new("1.2.3") => #<Version: @major=1, @minor=2, @patch=3>
    #
    # @overload initialize(version)
    #   @param [Solve::Version] version
    #
    #   @example
    #     Version.new(Version.new("1.2.3")) => #<Version: @major=1, @minor=2, @patch=3>
    #
    def initialize(*args)
      if args.first.is_a?(Array)
        @major, @minor, @patch = args.first
      else
        @major, @minor, @patch = self.class.split(args.first.to_s)
      end

      @major ||= 0
      @minor ||= 0
      @patch ||= 0
    end

    # @param [Solve::Version] other
    #
    # @return [Integer]
    def <=>(other)
      [:major, :minor, :patch].each do |method|
        ans = (self.send(method) <=> other.send(method))
        return ans if ans != 0
      end
      0
    end

    # @param [Solve::Version] other
    #
    # @return [Boolean]
    def eql?(other)
      other.is_a?(Version) && self == other
    end

    def inspect
      to_s
    end

    def to_s
      "#{major}.#{minor}.#{patch}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
solve-0.3.1 lib/solve/version.rb
solve-0.3.0 lib/solve/version.rb