Sha256: 6e1631aa372490b3eeb59d0d21e7f69f2b502ad73a1d951c4c23000e8ae0ca9f

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

# Gathers all the association names to pass to `ActiveRecord::Relation#joins` from a `Metasploit::Model::Search::Query`
class MetasploitDataModels::Search::Visitor::Joins
  include Metasploit::Model::Visitation::Visit

  #
  # Visitors
  #

  visit 'Metasploit::Model::Search::Group::Intersection',
        'Metasploit::Model::Search::Operation::Group::Intersection' do |parent|
    parent.children.flat_map { |child|
      visit child
    }
  end

  visit 'Metasploit::Model::Search::Group::Union',
        'Metasploit::Model::Search::Operation::Group::Union' do |parent|
    # A Set<Set> because if all children have multiple joins, but those multiple joins contain the same elements for
    # all children, then all joins can be counted as common:
    #
    #  (a.b:1 && c.d:2) || (a.b:3 && c.d:4) should return [:a, :c] since its common to both
    #  (a.b:1 && c.d:2 && e.f:3) || (a.b:3 && c.d:4) should return [:a, :c] since its the common _subset_
    join_set_set = parent.children.each_with_object(Set.new) { |child, set|
      child_joins = visit child
      child_join_set = Set.new child_joins

      set.add child_join_set
    }

    common_join_set = join_set_set.reduce { |common_subset, set|
      common_subset & set
    }

    common_join_set.to_a
  end

  visit 'Metasploit::Model::Search::Operation::Base' do |operation|
    visit operation.operator
  end

  visit 'Metasploit::Model::Search::Operator::Association' do |operator|
    [operator.association]
  end

  visit 'Metasploit::Model::Search::Operator::Attribute',
        'MetasploitDataModels::Search::Operator::Port::List' do |_|
    []
  end

  Metasploit::Concern.run(self)
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
metasploit_data_models-0.18.1-java app/models/metasploit_data_models/search/visitor/joins.rb
metasploit_data_models-0.18.1 app/models/metasploit_data_models/search/visitor/joins.rb
metasploit_data_models-0.18.0 app/models/metasploit_data_models/search/visitor/joins.rb
metasploit_data_models-0.18.0-java app/models/metasploit_data_models/search/visitor/joins.rb
metasploit_data_models-0.18.0.pre.compatibility app/models/metasploit_data_models/search/visitor/joins.rb