Class: MetasploitDataModels::Search::Visitor::Relation

Inherits:
Metasploit::Model::Base
  • Object
show all
Defined in:
app/models/metasploit_data_models/search/visitor/relation.rb

Overview

Generates a ActiveRecord::Relation from an Metasploit::Model::Search::Query#tree

Constant Summary collapse

RELATION_METHODS =

ActiveRecord::Relation methods that can compute their argument with a visitor under the MetasploitDataModels::Search::Visitor namespace.

[
    :joins,
    :includes,
    :where
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#queryMetasploit::Model::Search::Query

The query to visit. Query supplies Class with #scope upon which to build ActiveRecord::Relation.

Returns:

  • (Metasploit::Model::Search::Query)


23
24
25
# File 'app/models/metasploit_data_models/search/visitor/relation.rb', line 23

def query
  @query
end

Class Method Details

.visitor_class_by_relation_methodHash{Symbol => Class}

Maps method on ActiveRecord::Relation to the Class under MetasploitDataModels::Search::Visitor whose #visit method can produce the arguments to the ActiveRecord::Relation method.

Returns:

  • (Hash{Symbol => Class})


68
69
70
71
72
73
74
75
# File 'app/models/metasploit_data_models/search/visitor/relation.rb', line 68

def self.visitor_class_by_relation_method
  @relation_method_by_visitor_class ||= RELATION_METHODS.each_with_object({}) { |relation_method, relation_method_by_visitor_class|
    visitor_class_name = "#{parent.name}::#{relation_method.to_s.camelize}"
    visitor_class = visitor_class_name.constantize

    relation_method_by_visitor_class[relation_method] = visitor_class
  }
end

Instance Method Details

#visitActiveRecord::Relation

Visits #query tree to produce an ActiveRecord::Relation on the Metasploit::Model::Search::Query#klass.

Returns:

  • (ActiveRecord::Relation)


41
42
43
44
45
46
47
48
49
50
# File 'app/models/metasploit_data_models/search/visitor/relation.rb', line 41

def visit
  tree = query.tree

  # Enumerable#inject does not support 3 arity for Hashes so need to unpack pair
  visitor_by_relation_method.inject(query.klass.all) do |relation, pair|
    relation_method, visitor = pair
    visited = visitor.visit(tree)
    relation.send(relation_method, visited)
  end
end

#visitor_by_relation_methodHash{Symbol => #visit}

Map method on ActiveRecord::Relation to visitor that can visit Metasploit::Model::Search::Query#tree to produce the arguments to the method on ActiveRecord::Relation.

Returns:

  • (Hash{Symbol => #visit})


56
57
58
59
60
61
62
# File 'app/models/metasploit_data_models/search/visitor/relation.rb', line 56

def visitor_by_relation_method
  # Enumerable#each_with_object does not support 3 arity for Hashes so need to unpack pair
  @visitor_by_relation_method ||= self.class.visitor_class_by_relation_method.each_with_object({}) { |pair, visitor_by_relation_method|
    relation_method, visitor_class = pair
    visitor_by_relation_method[relation_method] = visitor_class.new
  }
end