class TheSchemaIs::Cops::Patterns

This module mitigates usage of RuboCop's NodePattern in a more flexible manner. NodePattern (targeting RuboCop's goals) was only available as a metaprogramming macro, requiring to `def_node_search :some_method, pattern` before the pattern can be used, while we wanted to just do `ast.ast_search(some_pattern)`; so this method defines a method for each used pattern on the fly and hides this discrepancy. Used by NodeRefinements (mixed into parser's node) to provide `Node#ast_search` and `Node#ast_match`.

Public Class Methods

match(pattern, node) click to toggle source
# File lib/the_schema_is/cops/node_util.rb, line 24
def match(pattern, node)
  match_methods[pattern].then { |m| instance.send(m, node) }
end

Private Class Methods

match_methods() click to toggle source
# File lib/the_schema_is/cops/node_util.rb, line 38
        def match_methods
  Hash.new { |h, pattern|
    method_name = "match_#{h.size}"
    def_node_search method_name, pattern
    h[pattern] = method_name
  }
end
search_methods() click to toggle source
# File lib/the_schema_is/cops/node_util.rb, line 30
        def search_methods
  Hash.new { |h, pattern|
    method_name = "search_#{h.size}"
    def_node_search method_name, pattern
    h[pattern] = method_name
  }
end