Class: ProcessFileCollector

Inherits:
BaseProcess show all
Defined in:
lib/process_file_collector.rb

Overview

file: lib/process_file_collector.rb

Instance Attribute Summary

Attributes inherited from BaseProcess

#key

Instance Method Summary collapse

Methods inherited from BaseProcess

#deep_match

Constructor Details

#initialize(key) ⇒ ProcessFileCollector

Returns a new instance of ProcessFileCollector.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/process_file_collector.rb', line 6

def initialize(key)
  super
  @found = nil
  # @matcher = ->(key, value) { key.to_s == 'file_collector' && value.is_a?(Hash) }
  @matcher = lambda do |key, value|
    if key.to_s == 'file_collector'
      if value.is_a?(Array)
        value.any? { |v| v.is_a?(Hash) }
      else
        value.is_a?(Hash)
      end
    else
      false
    end
  end
end

Instance Method Details

#execute(_input) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/process_file_collector.rb', line 28

def execute(_input)
  # Iterate over each `file_collector` found and process individually
  results = {}

  @found.each do |data|
    next unless data.is_a?(Hash)

    # Extract the `as` key if present
    as_key = data['as']

    working_directory = File.expand_path(data['root'])

    options = Appydave::Tools::GptContext::Options.new(
      working_directory: working_directory,
      include_patterns: extract_patterns(data.dig('files', 'include')),
      exclude_patterns: extract_patterns(data.dig('files', 'exclude')),
      format: 'json',
      line_limit: data['line_length']
    )

    collector = Appydave::Tools::GptContext::FileCollector.new(options)
    json = collector.build

    # Structuring the result under `process-data` with `as` as key
    result_data = {
      type: 'file_collector',
      data: {
        working_directory: working_directory,
        files: JSON.parse(json)
      }
    }

    # If `as` key exists, use it to store under process-data with that identifier
    if as_key
      results[as_key] = result_data
    else
      # Generate a unique key if no `as` key is defined
      unique_key = "file_collector_#{results.size + 1}"
      results[unique_key] = result_data
    end
  end

  results
rescue SyntaxError, NameError, NoMethodError => e
  puts "Ruby evaluation error in ProcessFileCollector: #{e.message}"
  puts "Error occurred at: #{e.backtrace.first}"
  {}
rescue StandardError => e
  puts "Unexpected error in ProcessFileCollector: #{e.message}"
  puts e.backtrace.join("\n")
  {}
end

#match?(input) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/process_file_collector.rb', line 23

def match?(input)
  @found = deep_match(input, @matcher)
  !@found.empty?
end