class RubyDetective::AST::Nodes::Query
Attributes
node[R]
Public Class Methods
new(node)
click to toggle source
# File lib/ruby_detective/ast/nodes/query.rb, line 7 def initialize(node) @node = node end
Public Instance Methods
class_declarations()
click to toggle source
# File lib/ruby_detective/ast/nodes/query.rb, line 50 def class_declarations deep_search(node, [:class_declaration_node?]) end
constant_references(where: {})
click to toggle source
TODO: ignore constant definitions, only return constant references
# File lib/ruby_detective/ast/nodes/query.rb, line 23 def constant_references(where: {}) constants = deep_search(node, [:constant_reference_node?]) case when where.key?(:namespace) constants.select { |c| c.namespace.include?(where[:namespace].to_sym) } else constants end end
top_level_constant_references(where: {})
click to toggle source
TODO: ignore constant definitions, only return constant references This finds all constants, ignoring the nested ones. For example: The “Foo::Bar” code contain two constants, but this method will only bring up one (the Bar one), with access to it's full path.
# File lib/ruby_detective/ast/nodes/query.rb, line 39 def top_level_constant_references(where: {}) constants = deep_search(node, [:constant_reference_node?, :top_level_constant?]) case when where.key?(:namespace) constants.select { |c| c.namespace.include?(where[:namespace].to_sym) } else constants end end
where(criteria = {})
click to toggle source
TODO: accept multiple criteria
# File lib/ruby_detective/ast/nodes/query.rb, line 12 def where(criteria = {}) case when criteria.key?(:type) type_validation_function = ->(node) { node.type == criteria[:type] } deep_search(node, [type_validation_function]) else deep_search(node) end end
Private Instance Methods
deep_search(node, validation_methods = [], acc: [])
click to toggle source
# File lib/ruby_detective/ast/nodes/query.rb, line 56 def deep_search(node, validation_methods = [], acc: []) return if node.value_node? validation_result = validation_methods.map do |validation_method| if validation_method.is_a?(Symbol) node.respond_to?(validation_method) && node.public_send(validation_method) elsif validation_method.is_a?(Proc) begin validation_method.call(node) rescue NoMethodError false end else raise ArgumentError, "Unexpected validation method data type" end end # Only appends the node to the results if all validations passed acc << node if validation_result.all? node.children.each { |child| send(__method__, child, validation_methods, acc: acc) } acc.uniq end