Module: RuboCop::AST::NodePattern::Sets

Defined in:
lib/rubocop/ast/node_pattern/sets.rb

Overview

Utility to assign a set of values to a constant

Constant Summary collapse

REGISTRY =
Hash.new do |h, set|
  name = Sets.name(set).freeze
  Sets.const_set(name, set)
  h[set] = "::RuboCop::AST::NodePattern::Sets::#{name}"
end
MAX =
4

Class Method Summary collapse

Class Method Details

.[](set) ⇒ Object



31
32
33
# File 'lib/rubocop/ast/node_pattern/sets.rb', line 31

def self.[](set)
  REGISTRY[set]
end

.name(set) ⇒ Object



15
16
17
18
19
20
# File 'lib/rubocop/ast/node_pattern/sets.rb', line 15

def self.name(set)
  elements = set
  elements = set.first(MAX - 1) << :etc if set.size > MAX
  name = elements.to_a.join('_').upcase.gsub(/[^A-Z0-9_]/, '')
  uniq("SET_#{name}")
end

.uniq(name) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/rubocop/ast/node_pattern/sets.rb', line 22

def self.uniq(name)
  return name unless Sets.const_defined?(name)

  (2..Float::INFINITY).each do |i|
    uniq = "#{name}_#{i}"
    return uniq unless Sets.const_defined?(uniq)
  end
end