<commit_before>module ActiveAdmin
  module Mongoid
    module Adaptor
      class Search
        attr_reader :base, :query, :query_hash, :search_params

        def initialize(object, search_params = {})
          @base = object
          @search_params = search_params
          @query_hash = get_query_hash(search_params)
          @query = @base.where(@query_hash)
        end

        def respond_to?(method_id)
          @query.send(:respond_to?, method_id)
        end

        def method_missing(method_id, *args, &block)
          if is_query(method_id)
            @search_params[method_id.to_s]
          else
            @query.send(method_id, *args, &block)
          end
        end

        private

        def is_query(method_id)
          method_id.to_s =~ /_contains$/  ||
          method_id.to_s =~ /_eq$/        ||
          method_id.to_s =~ /_(gt|lt)/
        end

        def get_query_hash(search_params)
          searches = search_params.map do|k, v|
            mongoidify_search(k,v)
          end
          Hash[searches]
        end

        def mongoidify_search(k, v)
          case k
          when /_contains$/
            [get_attribute(k, '_contains'), Regexp.new(Regexp.escape("#{v}"), Regexp::IGNORECASE)]
          when /_eq$/
            [get_attribute(k, '_eq'), v]
          when /_gt$/
            [get_attribute(k, "_gt").to_sym.gt, v]
          when /_lt$/
            [get_attribute(k, "_lt").to_sym.lt, v]
          else
            [k, v]
          end
        end

        def get_attribute(k, postfix)
          k.match(/^(.*)#{postfix}$/)[1]
        end
      end
    end
  end
end
<commit_msg>Marge regular expressions into one for is_query<commit_after>module ActiveAdmin
  module Mongoid
    module Adaptor
      class Search
        attr_reader :base, :query, :query_hash, :search_params

        def initialize(object, search_params = {})
          @base = object
          @search_params = search_params
          @query_hash = get_query_hash(search_params)
          @query = @base.where(@query_hash)
        end

        def respond_to?(method_id)
          @query.send(:respond_to?, method_id)
        end

        def method_missing(method_id, *args, &block)
          if is_query(method_id)
            @search_params[method_id.to_s]
          else
            @query.send(method_id, *args, &block)
          end
        end

        private

        def is_query(method_id)
          method_id.to_s =~ /_(contains|eq|gt|lt)$/
        end

        def get_query_hash(search_params)
          searches = search_params.map do|k, v|
            mongoidify_search(k,v)
          end
          Hash[searches]
        end

        def mongoidify_search(k, v)
          case k
          when /_contains$/
            [get_attribute(k, '_contains'), Regexp.new(Regexp.escape("#{v}"), Regexp::IGNORECASE)]
          when /_eq$/
            [get_attribute(k, '_eq'), v]
          when /_gt$/
            [get_attribute(k, "_gt").to_sym.gt, v]
          when /_lt$/
            [get_attribute(k, "_lt").to_sym.lt, v]
          else
            [k, v]
          end
        end

        def get_attribute(k, postfix)
          k.match(/^(.*)#{postfix}$/)[1]
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11049553708833487, 'fraction_numerical': 0.0006155740227762388, 'mean_word_length': 1.6487367563162185, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '110344', 'n_tokens_mistral': 1057, 'n_tokens_neox': 1034, 'n_words': 208}