forked from SketchUp/rubocop-sketchup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketchup_version.rb
117 lines (101 loc) · 3 KB
/
sketchup_version.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true
module RuboCop
module SketchUp
class SketchUpVersion
include Comparable
attr_reader :version, :maintenance
class InvalidVersion < StandardError; end
# @overload initialize(version_string)
# @param [String] version_string
#
# @overload initialize(version, maintenance)
# @param [Integer, Float] version
# @param [Integer] maintenance
def initialize(*args)
if args.size == 1
@version, @maintenance = parse_version(args.first)
elsif args.size == 2
validate(args)
@version, @maintenance = args
else
raise ArgumentError, "expected 1..2 arguments, got #{args.size}"
end
end
# @return [SketchUpVersion, nil]
def succ
version_parts = [@version, @maintenance]
index = VALID_VERSIONS.index(version_parts)
next_version_parts = VALID_VERSIONS[index + 1]
return nil if next_version_parts.nil?
self.class.new(*next_version_parts)
end
def <=>(other)
if version == other.version
maintenance <=> other.maintenance
else
version <=> other.version
end
end
# @return [String]
def to_s
string_version = version < 2013 ? version.to_f : version.to_i
if maintenance > 0
"SketchUp #{string_version} M#{maintenance}"
else
"SketchUp #{string_version}"
end
end
private
VERSION_NUMBER_REGEX = /^(?:SketchUp )?([0-9.]+)(?: M(\d+))?$/.freeze
# This list is compiled from the list of versions reported by YARD when
# running the `versions` template;
#
# yardoc -t versions -f text
#
# TODO(thomthom): Push the version template to the API stubs repository.
VALID_VERSIONS = [
[2018, 0],
[2017, 0],
[2016, 1],
[2016, 0],
[2015, 0],
[2014, 0],
[2013, 0],
[8.0, 2],
[8.0, 1],
[8.0, 0],
[7.1, 1],
[7.1, 0],
[7.0, 1],
[7.0, 0],
[6.0, 0],
].reverse.freeze
# @param [String] version
# @return [Array(Float, Integer)]
def parse_version(version)
v = 0
m = 0
if version.is_a?(String)
# Treat all LayOut versions as SketchUp versions for now.
normalized_version = version.gsub('LayOut', 'SketchUp')
result = normalized_version.match(VERSION_NUMBER_REGEX)
if result
v = result.captures[0].to_f
m = (result.captures[1] || '0').to_i
end
elsif version.is_a?(Numeric)
v = version
m = 0
end
validate([v, m])
end
# @param [Array(Float, Integer)] version_parts
def validate(version_parts)
unless VALID_VERSIONS.include?(version_parts)
raise InvalidVersion, "#{version} is not a valid SketchUp version"
end
version_parts
end
end
end
end