Class: PromptObjects::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_objects/loader.rb

Overview

Loads and parses prompt object markdown files. Extracts YAML frontmatter (config) and markdown body (soul).

Class Method Summary collapse

Class Method Details

.load(path) ⇒ Hash

Load a prompt object from a markdown file.

Parameters:

  • path (String)

    Path to the .md file

Returns:

  • (Hash)

    Parsed data with :config, :body, and :path

Raises:



10
11
12
13
14
15
# File 'lib/prompt_objects/loader.rb', line 10

def self.load(path)
  raise Error, "File not found: #{path}" unless File.exist?(path)

  content = File.read(path, encoding: "UTF-8")
  parse(content, path: path)
end

.load_all(dir) ⇒ Array<Hash>

Load all prompt objects from a directory.

Parameters:

  • dir (String)

    Directory path

Returns:

  • (Array<Hash>)

    Array of parsed prompt objects

Raises:



53
54
55
56
57
# File 'lib/prompt_objects/loader.rb', line 53

def self.load_all(dir)
  raise Error, "Directory not found: #{dir}" unless Dir.exist?(dir)

  Dir.glob(File.join(dir, "*.md")).map { |path| load(path) }
end

.merge_capabilities(config) ⇒ Object

Merge delegates_to + tools into the capabilities array. If both new-style and old-style keys are present, they are combined.



41
42
43
44
45
46
47
48
# File 'lib/prompt_objects/loader.rb', line 41

def self.merge_capabilities(config)
  delegates = config.delete("delegates_to") || []
  tools = config.delete("tools") || []
  return if delegates.empty? && tools.empty?

  existing = config["capabilities"] || []
  config["capabilities"] = (existing + delegates + tools).uniq
end

.parse(content, path: nil) ⇒ Hash

Parse prompt object markdown from a raw string (frontmatter + body). Raises if the YAML frontmatter is malformed, so callers can validate before persisting (see PromptObject#update_source).

Parameters:

  • content (String)

    Raw .md content

  • path (String, nil) (defaults to: nil)

    Source path to record in the result

Returns:

  • (Hash)

    Parsed data with :config, :body, and :path



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/prompt_objects/loader.rb', line 23

def self.parse(content, path: nil)
  parsed = FrontMatterParser::Parser.new(:md).call(content)
  config = parsed.front_matter || {}

  # Merge simplified frontmatter (delegates_to + tools) into capabilities.
  # This lets PO authors distinguish PO references from primitive references
  # while keeping the internal model uniform.
  merge_capabilities(config)

  {
    config: config,
    body: parsed.content.strip,
    path: path
  }
end