Class: MetasploitDataModels::Search::Visitor::Relation
- Inherits:
-
Metasploit::Model::Base
- Object
- Metasploit::Model::Base
- MetasploitDataModels::Search::Visitor::Relation
- 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
-
#query ⇒ Metasploit::Model::Search::Query
The query to visit.
Class Method Summary collapse
-
.visitor_class_by_relation_method ⇒ Hash{Symbol => Class}
Maps method on
ActiveRecord::Relation
to theClass
under MetasploitDataModels::Search::Visitor whose#visit
method can produce the arguments to theActiveRecord::Relation
method.
Instance Method Summary collapse
-
#visit ⇒ ActiveRecord::Relation
Visits #query tree to produce an
ActiveRecord::Relation
on theMetasploit::Model::Search::Query#klass
. -
#visitor_by_relation_method ⇒ Hash{Symbol => #visit}
Map method on
ActiveRecord::Relation
to visitor that can visitMetasploit::Model::Search::Query#tree
to produce the arguments to the method onActiveRecord::Relation
.
Instance Attribute Details
#query ⇒ Metasploit::Model::Search::Query
The query to visit. Query supplies Class with #scope upon which to build ActiveRecord::Relation
.
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_method ⇒ Hash{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.
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
#visit ⇒ ActiveRecord::Relation
Visits #query tree to produce an ActiveRecord::Relation
on the Metasploit::Model::Search::Query#klass
.
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.scoped) do |relation, pair| relation_method, visitor = pair visited = visitor.visit(tree) relation.send(relation_method, visited) end end |
#visitor_by_relation_method ⇒ Hash{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
.
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 |