Class: AIA::DirectiveProcessor

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

Constant Summary collapse

DIRECTIVE_PREFIX =
AIA::Directive::DIRECTIVE_PREFIX

Instance Method Summary collapse

Constructor Details

#initializeDirectiveProcessor

Returns a new instance of DirectiveProcessor.



18
19
20
# File 'lib/aia/directive_processor.rb', line 18

def initialize
  @prefix_size = DIRECTIVE_PREFIX.size
end

Instance Method Details

#directive?(string) ⇒ Boolean

Checks whether a string looks like a chat-time directive. Uses PM.directives as the source of truth for known directive names.

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/aia/directive_processor.rb', line 25

def directive?(string)
  content = extract_content(string)
  stripped = content.strip

  return false unless stripped.start_with?(DIRECTIVE_PREFIX)

  # Extract the directive name and check it's registered
  sans_prefix = stripped[@prefix_size..]
  method_name = sans_prefix.split(' ').first&.downcase
  return false if method_name.nil? || method_name.empty?

  PM.directives.key?(method_name.to_sym)
end

#process(string, _context_manager = nil) ⇒ Object

Process a chat-time directive by dispatching through PM.directives. Returns the block’s return value: non-blank string for content directives, nil for operational directives.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/aia/directive_processor.rb', line 43

def process(string, _context_manager = nil)
  return string unless directive?(string)

  content = extract_content(string)
  key = content.strip
  sans_prefix = key[@prefix_size..]
  args = sans_prefix.split(' ')
  method_name = args.shift.downcase

  block = PM.directives[method_name.to_sym]
  return "Error: Unknown directive '#{method_name}'" unless block

  # Provide a RenderContext so PM built-in directives (e.g., :include)
  # work in chat mode with proper path resolution.  AIA directives
  # ignore the context parameter, so this is harmless for them.
  block.call(render_context, *args)
end