Class: PromptManager::DirectiveProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_manager/directive_processor.rb

Constant Summary collapse

EXCLUDED_METHODS =
%w[ run initialize ]

Instance Method Summary collapse

Constructor Details

#initializeDirectiveProcessor

Returns a new instance of DirectiveProcessor.



11
12
13
14
# File 'lib/prompt_manager/directive_processor.rb', line 11

def initialize
  @prefix_size    = PromptManager::Prompt::DIRECTIVE_SIGNAL.size
  @included_files = []
end

Instance Method Details

#include(file_path) ⇒ Object Also known as: import



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/prompt_manager/directive_processor.rb', line 34

def include(file_path)
  if  File.exist?(file_path) &&
      File.readable?(file_path) &&
      !@included_files.include?(file_path)
    content = File.read(file_path)
    @included_files << file_path
    content
  else
    "Error: File '#{file_path}' not accessible"
  end
end

#run(directives) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/prompt_manager/directive_processor.rb', line 16

def run(directives)
  return {} if directives.nil? || directives.empty?
  directives.each do |key, _|
    sans_prefix = key[@prefix_size..]
    args        = sans_prefix.split(' ')
    method_name = args.shift

    if EXCLUDED_METHODS.include?(method_name)
      directives[key] = "Error: #{method_name} is not a valid directive: #{key}"
    elsif respond_to?(method_name)
      directives[key] =  send(method_name, *args)
    else
      directives[key] = "Error: Unknown directive '#{key}'"
    end
  end
  directives
end