class Minitest::RubyGolfMetrics::MethodParser

Public Class Methods

new(source) click to toggle source
# File lib/minitest/ruby_golf_metrics/method_parser.rb, line 10
def initialize(source)
  @ast = Ripper.sexp(source)
end

Public Instance Methods

is_method_called?(method_name) click to toggle source
# File lib/minitest/ruby_golf_metrics/method_parser.rb, line 14
def is_method_called?(method_name)
  search_ast_for_method(@ast, method_name)
end

Private Instance Methods

is_top_level_method_call(ast, method_name) click to toggle source
# File lib/minitest/ruby_golf_metrics/method_parser.rb, line 20
def is_top_level_method_call(ast, method_name)
  # firstly check if possible command block
  unless ast.is_a?(Array) && ast.length > 1 && ast[1].is_a?(Array)
    return false
  end
  # now check if it is a function call or command, and check the method name
  if [:command, :fcall].include? ast[0]
    ast[1].include?(method_name.to_s)
  elsif ast[0] == :command_call
    ast[1][0] == :var_ref && ast[1][1].include?("RubyGolf")
    ast[3].include?(method_name.to_s)
  else
    false
  end
end
search_ast_for_method(ast, method_name) click to toggle source
# File lib/minitest/ruby_golf_metrics/method_parser.rb, line 36
def search_ast_for_method(ast, method_name)
  return true if is_top_level_method_call(ast, method_name)
  return false unless ast.is_a? Array
  ast.any? { |e| search_ast_for_method(e, method_name) }
end