Class: RubyDetective::AST::Nodes::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_detective/ast/nodes/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Query

Returns a new instance of Query.



7
8
9
# File 'lib/ruby_detective/ast/nodes/query.rb', line 7

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



5
6
7
# File 'lib/ruby_detective/ast/nodes/query.rb', line 5

def node
  @node
end

Instance Method Details

#class_declarationsObject



50
51
52
# File 'lib/ruby_detective/ast/nodes/query.rb', line 50

def class_declarations
  deep_search(node, [:class_declaration_node?])
end

#constant_references(where: {}) ⇒ Object

TODO: ignore constant definitions, only return constant references



23
24
25
26
27
28
29
30
31
32
# 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: {}) ⇒ Object

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.



39
40
41
42
43
44
45
46
47
48
# 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 = {}) ⇒ Object

TODO: accept multiple criteria



12
13
14
15
16
17
18
19
20
# 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