Class: Aidp::PromptOptimization::CodeFragment
- Inherits:
-
Object
- Object
- Aidp::PromptOptimization::CodeFragment
- 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
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#line_end ⇒ Object
readonly
Returns the value of attribute line_end.
-
#line_start ⇒ Object
readonly
Returns the value of attribute line_start.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#estimated_tokens ⇒ Integer
Estimate token count (rough approximation: 1 token ≈ 4 chars).
-
#initialize(id:, file_path:, type:, name:, content:, line_start:, line_end:) ⇒ CodeFragment
constructor
A new instance of CodeFragment.
- #inspect ⇒ Object
-
#line_count ⇒ Integer
Get line count.
-
#relative_path(project_dir) ⇒ String
Get relative file path from project root.
-
#size ⇒ Integer
Get the size of the fragment in characters.
-
#summary ⇒ Hash
Get a summary of the fragment.
-
#test_file? ⇒ Boolean
Check if this is a test file fragment.
- #to_s ⇒ Object
Constructor Details
#initialize(id:, file_path:, type:, name:, content:, line_start:, line_end:) ⇒ CodeFragment
Returns a new instance of CodeFragment.
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
#content ⇒ Object (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_path ⇒ Object (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 |
#id ⇒ Object (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_end ⇒ Object (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_start ⇒ Object (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 |
#name ⇒ Object (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 |
#type ⇒ Object (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_tokens ⇒ Integer
Estimate token count (rough approximation: 1 token ≈ 4 chars)
256 257 258 |
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 256 def estimated_tokens (size / 4.0).ceil end |
#inspect ⇒ Object
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_count ⇒ Integer
Get line count
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
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 |
#size ⇒ Integer
Get the size of the fragment in characters
249 250 251 |
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 249 def size @content.length end |
#summary ⇒ Hash
Get a summary of the fragment
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
278 279 280 |
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 278 def test_file? !!(@file_path =~ /_(spec|test)\.rb$/) end |
#to_s ⇒ Object
299 300 301 |
# File 'lib/aidp/prompt_optimization/source_code_fragmenter.rb', line 299 def to_s "CodeFragment<#{@type}:#{@name}>" end |