Class: UserAgentParser::Version

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

Constant Summary collapse

SEGMENTS_REGEX =

Private: Regex used to split string version string into major, minor, patch, and patch_minor.

/\d+\-\d+|\d+[a-zA-Z]+$|\d+|[A-Za-z][0-9A-Za-z-]*$/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Version

Returns a new instance of Version.



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

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

Instance Attribute Details

#versionObject (readonly) Also known as: to_s

Returns the value of attribute version.



13
14
15
# File 'lib/user_agent_parser/version.rb', line 13

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Object



52
53
54
# File 'lib/user_agent_parser/version.rb', line 52

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/user_agent_parser/version.rb', line 47

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

#inspectObject



43
44
45
# File 'lib/user_agent_parser/version.rb', line 43

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

#majorObject



27
28
29
# File 'lib/user_agent_parser/version.rb', line 27

def major
  segments[0]
end

#minorObject



31
32
33
# File 'lib/user_agent_parser/version.rb', line 31

def minor
  segments[1]
end

#patchObject



35
36
37
# File 'lib/user_agent_parser/version.rb', line 35

def patch
  segments[2]
end

#patch_minorObject



39
40
41
# File 'lib/user_agent_parser/version.rb', line 39

def patch_minor
  segments[3]
end

#segmentsObject



56
57
58
# File 'lib/user_agent_parser/version.rb', line 56

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

#to_hObject



60
61
62
63
64
65
66
67
68
# File 'lib/user_agent_parser/version.rb', line 60

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