Class: UserAgent::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/user_agent/version.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Version

Returns a new instance of Version.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/user_agent/version.rb', line 16

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

Class Method Details

.new(obj) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/user_agent/version.rb', line 5

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

Instance Method Details

#<=>(other) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/user_agent/version.rb', line 51

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

#==(other) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/user_agent/version.rb', line 40

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/user_agent/version.rb', line 36

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

#inspectObject



86
87
88
# File 'lib/user_agent/version.rb', line 86

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

#to_aObject



28
29
30
# File 'lib/user_agent/version.rb', line 28

def to_a
  @sequences.dup
end

#to_sObject



82
83
84
# File 'lib/user_agent/version.rb', line 82

def to_s
  to_str
end

#to_strObject



32
33
34
# File 'lib/user_agent/version.rb', line 32

def to_str
  @str.dup
end