Class: RubyLLM::Registry::Adapters::ActiveRecord

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_llm/registry/adapters/active_record.rb

Overview

ActiveRecord-backed prompt repository.

Instance Method Summary collapse

Methods inherited from Base

#export, #import

Constructor Details

#initialize(model: nil, table_name: "ruby_llm_registry_prompts") ⇒ ActiveRecord

Returns a new instance of ActiveRecord.



10
11
12
13
14
# File 'lib/ruby_llm/registry/adapters/active_record.rb', line 10

def initialize(model: nil, table_name: "ruby_llm_registry_prompts")
  require_active_record!
  @model = model || build_model(table_name)
  ensure_schema!
end

Instance Method Details

#available_versions(path) ⇒ Object



34
35
36
# File 'lib/ruby_llm/registry/adapters/active_record.rb', line 34

def available_versions(path)
  model.where(path: path).map { |row| Version.parse(row.version) }.sort
end

#get(path, version: nil, label: nil) ⇒ Object



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

def get(path, version: nil, label: nil)
  scope = model.where(path: path)
  raise PromptNotFoundError, "Prompt path not found: #{path}" if scope.none?

  record = if version
             scope.find_by(version: Version.parse(version).to_s)
           elsif label
             label = label.to_sym
             scope.to_a.find { |row| labels_for(row).include?(label) } || scope.find_by(version: label.to_s)
           else
             scope.to_a.max_by { |row| Version.parse(row.version) }
           end

  raise PromptNotFoundError, "Prompt not found: #{path}" unless record

  prompt_from_record(record)
end

#store(prompt, overwrite: false) ⇒ Object

Raises:



38
39
40
41
42
43
44
45
# File 'lib/ruby_llm/registry/adapters/active_record.rb', line 38

def store(prompt, overwrite: false)
  existing = model.find_by(path: prompt.path, version: prompt.version.to_s)
  raise Error, "Prompt #{prompt.path}@#{prompt.version} already exists" if existing && !overwrite

  attrs = serialize_prompt(prompt)
  existing ? existing.update!(attrs) : model.create!(attrs)
  prompt
end