Sha256: d9d24aa7f77bde10304441b9852a838327366e13c40a4bb276ca4addfca6bb6b

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require 'rubygems/version'

module UserAgentParser
  class Version
    include Comparable

    # Private: Regex used to split string version string into major, minor,
    # patch, and patch_minor.
    SEGMENTS_REGEX = /\d+\-\d+|\d+[a-zA-Z]+$|\d+|[A-Za-z][0-9A-Za-z-]*$/.freeze

    attr_reader :version
    alias to_s version

    def initialize(*args)
      # If only one string argument is given, assume a complete version string
      # and attempt to parse it
      if args.length == 1 && args.first.is_a?(String)
        @version = args.first.to_s.strip
      else
        @segments = args.compact.map(&:to_s).map(&:strip)
        @version = segments.join('.')
      end
    end

    def major
      segments[0]
    end

    def minor
      segments[1]
    end

    def patch
      segments[2]
    end

    def patch_minor
      segments[3]
    end

    def inspect
      "#<#{self.class} #{self}>"
    end

    def eql?(other)
      self.class.eql?(other.class) &&
        version == other.version
    end

    def <=>(other)
      Gem::Version.new(version).<=>(Gem::Version.new(other.to_s))
    end

    def segments
      @segments ||= version.scan(SEGMENTS_REGEX)
    end

    def to_h
      {
        version: version,
        major: major,
        minor: minor,
        patch: patch,
        patch_minor: patch_minor
      }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
user_agent_parser-2.18.0 lib/user_agent_parser/version.rb
user_agent_parser-2.17.0 lib/user_agent_parser/version.rb
user_agent_parser-2.16.0 lib/user_agent_parser/version.rb
user_agent_parser-2.15.0 lib/user_agent_parser/version.rb
user_agent_parser-2.14.0 lib/user_agent_parser/version.rb
user_agent_parser-2.13.0 lib/user_agent_parser/version.rb
user_agent_parser-2.12.0 lib/user_agent_parser/version.rb
user_agent_parser-2.11.0 lib/user_agent_parser/version.rb