Class: Kward::Prompts::Templates

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/prompts/templates.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_dir:, template_class:, markdown_parser:) ⇒ Templates

Returns a new instance of Templates.



4
5
6
7
8
# File 'lib/kward/prompts/templates.rb', line 4

def initialize(config_dir:, template_class:, markdown_parser:)
  @config_dir = config_dir
  @template_class = template_class
  @markdown_parser = markdown_parser
end

Instance Method Details

#prompt_templates(reserved_commands: []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kward/prompts/templates.rb', line 10

def prompt_templates(reserved_commands: [])
  prompts_root = File.join(@config_dir, "prompts")
  return [] unless Dir.exist?(prompts_root)

  reserved = reserved_commands.map(&:to_s)
  seen = {}
  Dir.glob(File.join(prompts_root, "*.md")).sort.filter_map do |path|
    template = parse_prompt_template(path)
    next unless template

    if reserved.include?(template.command)
      warn "Warning: skipping Kward prompt command /#{template.command}: reserved command"
      next
    end
    if seen[template.command]
      warn "Warning: skipping duplicate Kward prompt command /#{template.command}: #{path}"
      next
    end

    seen[template.command] = true
    template
  end
rescue StandardError => e
  warn "Warning: skipping Kward prompt templates in #{prompts_root}: #{e.message}"
  []
end