debian-mirror-gitlab/lib/gitlab/search/parsed_query.rb

61 lines
1.4 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
module Gitlab
module Search
class ParsedQuery
2020-10-24 23:57:45 +05:30
include Gitlab::Utils::StrongMemoize
2018-11-08 19:23:39 +05:30
attr_reader :term, :filters
def initialize(term, filters)
@term = term
@filters = filters
end
def filter_results(results)
2020-10-24 23:57:45 +05:30
with_matcher = ->(filter) { filter[:matcher].present? }
excluding = excluding_filters.select(&with_matcher)
including = including_filters.select(&with_matcher)
return unless excluding.any? || including.any?
results.select! do |result|
including.all? { |filter| filter[:matcher].call(filter, result) }
end
results.reject! do |result|
excluding.any? { |filter| filter[:matcher].call(filter, result) }
end
results
end
private
def including_filters
processed_filters(:including)
end
def excluding_filters
processed_filters(:excluding)
end
def processed_filters(type)
excluding, including = strong_memoize(:processed_filters) do
filters.partition { |filter| filter[:negated] }
end
2018-11-08 19:23:39 +05:30
2020-10-24 23:57:45 +05:30
case type
when :including then including
when :excluding then excluding
else
raise ArgumentError.new(type)
2018-11-08 19:23:39 +05:30
end
end
end
end
end
2020-05-24 23:13:21 +05:30
Gitlab::Search::ParsedQuery.prepend_if_ee('EE::Gitlab::Search::ParsedQuery')