Class: PromptManager::Storage::ActiveRecordAdapter

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

Overview

This class acts as an adapter for interacting with an ActiveRecord model to manage storage operations for PromptManager::Prompt instances. It defines methods that allow for saving, searching, retrieving by ID, and deleting prompts.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActiveRecordAdapter

Returns a new instance of ActiveRecordAdapter.



75
76
77
78
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 75

def initialize
  self.class.send(:validate_configuration) # send gets around private designations of a method
  @record = model.first
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



133
134
135
136
137
138
139
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 133

def method_missing(method_name, *args, &block)
  if @record.respond_to?(method_name)
    model.send(method_name, args.first, &block)
  else
    super
  end
end

Class Attribute Details

.id_columnObject

Returns the value of attribute id_column.



11
12
13
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 11

def id_column
  @id_column
end

.modelObject

Returns the value of attribute model.



11
12
13
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 11

def model
  @model
end

.parameters_columnObject

Returns the value of attribute parameters_column.



11
12
13
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 11

def parameters_column
  @parameters_column
end

.text_columnObject

Returns the value of attribute text_column.



11
12
13
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 11

def text_column
  @text_column
end

Instance Attribute Details

#recordObject

Returns the value of attribute record.



65
66
67
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 65

def record
  @record
end

Class Method Details

.configObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 16

def config
  if block_given?
    yield self
    validate_configuration
  else
    raise ArgumentError, "No block given to config"
  end
  
  self
end

.method_missing(method_name, *args, &block) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 49

def method_missing(method_name, *args, &block)
  if model.respond_to?(method_name)
    model.send(method_name, *args, &block)
  else
    super
  end
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 58

def respond_to_missing?(method_name, include_private = false)
  model.respond_to?(method_name, include_private) || super
end

.validate_columnsObject



40
41
42
43
44
45
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 40

def validate_columns
  columns = model.column_names # Array of Strings
  [id_column, text_column, parameters_column].each do |column|
    raise ArgumentError, "#{column} is not a valid column for model #{model}" unless columns.include?(column.to_s)
  end
end

.validate_configurationObject



28
29
30
31
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 28

def validate_configuration
  validate_model
  validate_columns
end

.validate_modelObject

Raises:

  • (ArgumentError)


34
35
36
37
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 34

def validate_model
  raise ArgumentError, "AR Model not set" unless model
  raise ArgumentError, "AR Model is not an ActiveRecord model" unless model < ActiveRecord::Base
end

Instance Method Details

#delete(id:) ⇒ Object



112
113
114
115
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 112

def delete(id:)
  @record = model.find_by(id_column => id)
  @record&.destroy
end

#get(id:) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 81

def get(id:)
  @record = model.find_by(id_column => id)
  raise ArgumentError, "Prompt not found with id: #{id}" unless @record

  # kludge? testing showed that parameters was being
  # returned as a String.  Did serialization fail or is
  # there something else going on?
  # FIXME: expected the parameters_column to be a HAsh after de-serialization
  parameters = @record[parameters_column]

  if parameters.is_a? String
    parameters = JSON.parse parameters
  end

  {
    id:         id, # same as the id_column
    text:       @record[text_column],
    parameters: parameters
  }
end

#id_columnObject



70
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 70

def id_column    = self.class.id_column

#listObject



119
120
121
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 119

def list(*)
  model.all.pluck(id_column)
end

#modelObject

Avoid code littered with self.class prefixes …



69
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 69

def model               = self.class.model

#parameters_columnObject



72
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 72

def parameters_column   = self.class.parameters_column

#save(id:, text: "", parameters: {}) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 103

def save(id:, text: "", parameters: {})
  @record = model.find_or_initialize_by(id_column => id) 

  @record[text_column] = text
  @record[parameters_column]  = parameters
  @record.save!
end

#search(for_what) ⇒ Object



124
125
126
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 124

def search(for_what)
  model.where("#{text_column} LIKE ?", "%#{for_what}%").pluck(id_column)
end

#text_columnObject



71
# File 'lib/prompt_manager/storage/active_record_adapter.rb', line 71

def text_column  = self.class.text_column