Module: RubynCode::Learning::Porter

Defined in:
lib/rubyn_code/learning/porter.rb

Overview

Exports and imports learned instincts so a user can carry their accumulated learnings to another machine. Instincts live in SQLite under ~/.rubyn-code; this serializes them to a portable JSON file and loads them back, regenerating ids and de-duplicating by (project_path, pattern).

Defined Under Namespace

Classes: Error

Constant Summary collapse

FORMAT_VERSION =
1
COLUMNS =

Columns carried across machines (id is regenerated on import).

%w[
  project_path pattern context_tags confidence decay_rate
  times_applied times_helpful created_at updated_at
].freeze

Class Method Summary collapse

Class Method Details

.export(db:, path:, project_path: nil) ⇒ Integer

Export instincts to a JSON file.

Parameters:

  • db (DB::Connection)
  • path (String)

    destination file

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

    limit to one project, or nil for all

Returns:

  • (Integer)

    number of instincts exported



29
30
31
32
33
34
# File 'lib/rubyn_code/learning/porter.rb', line 29

def export(db:, path:, project_path: nil)
  rows = fetch(db, project_path)
  payload = { 'version' => FORMAT_VERSION, 'instincts' => rows }
  File.write(path, "#{JSON.pretty_generate(payload)}\n")
  rows.size
end

.import(db:, path:, remap_project: nil) ⇒ Hash

Import instincts from a JSON file.

Parameters:

  • db (DB::Connection)
  • path (String)

    source file

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

    override every row’s project_path (use the current project so imported learnings apply here)

Returns:

  • (Hash)

    { imported:, skipped:, total: }

Raises:



43
44
45
46
47
48
49
50
51
# File 'lib/rubyn_code/learning/porter.rb', line 43

def import(db:, path:, remap_project: nil)
  raise Error, "File not found: #{path}" unless File.file?(path)

  payload = parse(path)
  instincts = Array(payload['instincts'])
  imported = instincts.count { |row| import_row(db, row, remap_project) }

  { imported: imported, skipped: instincts.size - imported, total: instincts.size }
end

.stats(db, project_path: nil) ⇒ Hash

Returns { count:, projects: } summary for display.

Returns:

  • (Hash)

    { count:, projects: } summary for display



54
55
56
57
# File 'lib/rubyn_code/learning/porter.rb', line 54

def stats(db, project_path: nil)
  rows = fetch(db, project_path)
  { count: rows.size, projects: rows.map { |r| r['project_path'] }.uniq.size }
end