Class: Aidp::PromptOptimization::CodeFragment

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/prompt_optimization/source_code_fragmenter.rb

Overview

Represents a code fragment (class, method, requires, etc.)

Each fragment is a logical unit of code that can be independently included or excluded from prompts based on relevance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, file_path:, type:, name:, content:, line_start:, line_end:) ⇒ CodeFragment

Returns a new instance of CodeFragment.

Parameters:

  • id (String)

    Unique identifier (e.g., “lib/user.rb:User”)

  • file_path (String)

    Source file path

  • type (Symbol)

    Fragment type (:class, :module, :method, :requires)

  • name (String)

    Name of the code unit

  • content (String)

    Code content

  • line_start (Integer)

    Starting line number

  • line_end (Integer)

    Ending line number



236
237
238
239
240
241
242
243
244
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 236

def initialize(id:, file_path:, type:, name:, content:, line_start:, line_end:)
  @id = id
  @file_path = file_path
  @type = type
  @name = name
  @content = content
  @line_start = line_start
  @line_end = line_end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



227
228
229
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 227

def content
  @content
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



227
228
229
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 227

def file_path
  @file_path
end

#idObject (readonly)

Returns the value of attribute id.



227
228
229
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 227

def id
  @id
end

#line_endObject (readonly)

Returns the value of attribute line_end.



227
228
229
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 227

def line_end
  @line_end
end

#line_startObject (readonly)

Returns the value of attribute line_start.



227
228
229
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 227

def line_start
  @line_start
end

#nameObject (readonly)

Returns the value of attribute name.



227
228
229
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 227

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



227
228
229
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 227

def type
  @type
end

Instance Method Details

#estimated_tokensInteger

Estimate token count (rough approximation: 1 token ≈ 4 chars)

Returns:

  • (Integer)

    Estimated token count



256
257
258
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 256

def estimated_tokens
  (size / 4.0).ceil
end

#inspectObject



303
304
305
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 303

def inspect
  "#<CodeFragment id=#{@id} type=#{@type} lines=#{@line_start}-#{@line_end}>"
end

#line_countInteger

Get line count

Returns:

  • (Integer)

    Number of lines



263
264
265
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 263

def line_count
  @line_end - @line_start + 1
end

#relative_path(project_dir) ⇒ String

Get relative file path from project root

Parameters:

  • project_dir (String)

    Project directory

Returns:

  • (String)

    Relative path



271
272
273
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 271

def relative_path(project_dir)
  @file_path.sub(%r{^#{Regexp.escape(project_dir)}/?}, "")
end

#sizeInteger

Get the size of the fragment in characters

Returns:

  • (Integer)

    Character count



249
250
251
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 249

def size
  @content.length
end

#summaryHash

Get a summary of the fragment

Returns:

  • (Hash)

    Fragment summary



285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 285

def summary
  {
    id: @id,
    file_path: @file_path,
    type: @type,
    name: @name,
    lines: "#{@line_start}-#{@line_end}",
    line_count: line_count,
    size: size,
    estimated_tokens: estimated_tokens,
    test_file: test_file?
  }
end

#test_file?Boolean

Check if this is a test file fragment

Returns:

  • (Boolean)

    True if from spec file



278
279
280
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 278

def test_file?
  !!(@file_path =~ /_(spec|test)\.rb$/)
end

#to_sObject



299
300
301
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 299

def to_s
  "CodeFragment<#{@type}:#{@name}>"
end