Class: RailsAiBridge::Tools::SearchCode::RubySearch::FileProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/tools/search_code/ruby_search.rb

Overview

Encapsulates file-level search logic.

Constant Summary collapse

SECRET_EXTENSIONS =
%w[.key .pem .p12 .pfx .crt].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regex, results, max_results, root) ⇒ FileProcessor

Returns a new instance of FileProcessor.

Parameters:

  • regex (Regexp)

    compiled search pattern

  • results (Array<Hash>)

    shared result accumulator

  • max_results (Integer)

    maximum number of results

  • root (String)

    application root path



19
20
21
22
23
24
# File 'lib/rails_ai_bridge/tools/search_code/ruby_search.rb', line 19

def initialize(regex, results, max_results, root)
  @regex = regex
  @results = results
  @max_results = max_results
  @root = root
end

Class Method Details

.secret_file?(basename) ⇒ Boolean

Checks if a filename matches secret file patterns (case-insensitive .env and common key extensions).

@param basename [String] file basename @return [Boolean] +true+ if the file looks like a secret file

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/rails_ai_bridge/tools/search_code/ruby_search.rb', line 46

def self.secret_file?(basename)
  normalized = basename.downcase
  return true if normalized.start_with?('.env')

  SECRET_EXTENSIONS.any? { |ext| normalized.end_with?(ext) }
end

Instance Method Details

#process(file) ⇒ :full?

Processes a single file, collecting matching lines into results.

Parameters:

  • file (String)

    absolute path to the file

Returns:

  • (:full, nil)

    +:full+ when the result limit is hit, nil otherwise



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rails_ai_bridge/tools/search_code/ruby_search.rb', line 30

def process(file)
  return if skip_file?(file)

  relative = file.sub("#{@root}/", '')
  File.readlines(file).each_with_index do |line, idx|
    next unless line.match?(@regex)

    @results << { file: relative, line_number: idx + 1, content: line }
    return :full if @results.size >= @max_results
  end
end