File: version_lookup.rb

package info (click to toggle)
ruby-regexp-parser 2.11.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,092 kB
  • sloc: ruby: 6,891; makefile: 6; sh: 3
file content (67 lines) | stat: -rw-r--r-- 2,018 bytes parent folder | download
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
# frozen_string_literal: true

module Regexp::Syntax
  VERSION_FORMAT = '\Aruby/\d+\.\d+(\.\d+)?\z'
  VERSION_REGEXP = /#{VERSION_FORMAT}/.freeze
  VERSION_CONST_REGEXP = /\AV\d+_\d+(?:_\d+)?\z/.freeze

  class InvalidVersionNameError < Regexp::Syntax::SyntaxError
    def initialize(name)
      super "Invalid version name '#{name}'. Expected format is '#{VERSION_FORMAT}'"
    end
  end

  class UnknownSyntaxNameError < Regexp::Syntax::SyntaxError
    def initialize(name)
      super "Unknown syntax name '#{name}'."
    end
  end

  module_function

  # Returns the syntax specification class for the given syntax
  # version name. The special names 'any' and '*' return Syntax::Any.
  def for(name)
    (@alias_map ||= {})[name] ||= version_class(name)
  end

  def new(name)
    warn 'Regexp::Syntax.new is deprecated in favor of Regexp::Syntax.for. '\
         'It does not return distinct instances and will be removed in v3.0.0.'
    self.for(name)
  end

  def supported?(name)
    name =~ VERSION_REGEXP && comparable(name) >= comparable('1.8.6')
  end

  def version_class(version)
    return Regexp::Syntax::Any if ['*', 'any'].include?(version.to_s)

    version =~ VERSION_REGEXP || raise(InvalidVersionNameError, version)
    version_const_name = "V#{version.to_s.scan(/\d+/).join('_')}"
    const_get(version_const_name) || raise(UnknownSyntaxNameError, version)
  end

  def const_missing(const_name)
    if const_name =~ VERSION_CONST_REGEXP
      return fallback_version_class(const_name)
    end
    super
  end

  def fallback_version_class(version)
    sorted = (specified_versions + [version]).sort_by { |ver| comparable(ver) }
    index = sorted.index(version)
    index > 0 && const_get(sorted[index - 1])
  end

  def specified_versions
    constants.select { |const_name| const_name =~ VERSION_CONST_REGEXP }
  end

  def comparable(name)
    # add .99 to treat versions without a patch value as latest patch version
    Gem::Version.new((name.to_s.scan(/\d+/) << 99).join('.'))
  end
end