Module: OllamaChat::Database::Duplicatable

Included in:
Models::Prompt, Models::Session
Defined in:
lib/ollama_chat/database/duplicatable.rb

Overview

Module to provide duplication functionality for database models.

This module allows an object to create a new instance of its own class with the same attributes, intentionally skipping over the primary key and timestamp fields to ensure a fresh record can be created.

Instance Method Summary collapse

Instance Method Details

#duplicateObject

Creates a new instance of the class with duplicated attributes.

Returns:

  • (Object)

    A new instance of the same class with duplicated attribute values.



11
12
13
14
15
16
17
18
# File 'lib/ollama_chat/database/duplicatable.rb', line 11

def duplicate
  klass = self.class
  attributes = columns.each_with_object({}) do |column, hash|
    [ klass.primary_key, :updated_at, :created_at ].include?(column) and next
    hash[column] = __send__(column).dup
  end
  klass.new(attributes)
end