Sha256: d306f991381679509fada0257483ee69c43217db70f115a4076021e7e71f8d9d

Contents?: true

Size: 1.62 KB

Versions: 11

Compression:

Stored size: 1.62 KB

Contents

class UserAgent
  class Version
    include ::Comparable

    def self.new(obj)
      case obj
      when Version
        obj
      when String
        super
      else
        raise ArgumentError, "invalid value for Version: #{obj.inspect}"
      end
    end

    def initialize(str)
      @str = str

      if str =~ /^\d+$/ || str =~ /^\d+\./
        @sequences  = str.scan(/\d+|[A-Za-z][0-9A-Za-z-]*$/).map { |s| s =~ /^\d+$/ ? s.to_i : s }
        @comparable = true
      else
        @sequences  = [str]
        @comparable = false
      end
    end

    def to_a
      @sequences.dup
    end

    def to_str
      @str.dup
    end

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

    def ==(other)
      case other
      when Version
        eql?(other)
      when String
        eql?(self.class.new(other))
      else
        false
      end
    end

    def <=>(other)
      case other
      when Version
        if @comparable
          ([0]*6).zip(to_a, other.to_a).each do |dump, a, b|
            a ||= 0
            b ||= 0

            if a.is_a?(String) && b.is_a?(Integer)
              return -1
            elsif a.is_a?(Integer) && b.is_a?(String)
              return 1
            elsif a == b
              next
            else
              return a <=> b
            end
          end
          0
        elsif to_s == other.to_s
          return 0
        else
          return -1
        end
      when String
        self <=> self.class.new(other)
      else
        nil
      end
    end

    def to_s
      to_str
    end

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

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
useragent-0.10.0 lib/user_agent/version.rb
useragent-0.9.0 lib/user_agent/version.rb
useragent-0.8.3 lib/user_agent/version.rb
useragent-0.8.2 lib/user_agent/version.rb
useragent-0.8.1 lib/user_agent/version.rb
useragent-0.8.0 lib/user_agent/version.rb
useragent-0.7.0 lib/user_agent/version.rb
useragent-no-extends-0.6.1 lib/user_agent/version.rb
friendlyfashion-useragent-0.6.1.1 lib/user_agent/version.rb
friendlyfashion-useragent-0.6.1 lib/user_agent/version.rb
useragent-0.6.0 lib/user_agent/version.rb