Class: ContextSpook::Generator
- Inherits:
-
Object
- Object
- ContextSpook::Generator
- Includes:
- VerbosePuts
- Defined in:
- lib/context_spook/generator.rb
Overview
The Generator class provides a DSL parser that interprets context definition files and constructs structured context objects containing project metadata, file contents, command outputs, and variables for AI assistance.
Defined Under Namespace
Classes: Context
Instance Method Summary collapse
-
#context(&block) ⇒ Context
The context method creates or returns a context object.
-
#initialize(verbose: false, &block) ⇒ Generator
constructor
The initialize method sets up the object by evaluating the provided block in the object’s context.
-
#output_context_size ⇒ Object
The output_context_size method prints the total size of the generated context JSON representation.
-
#parse(source) ⇒ ContextSpook::Generator
private
The parse method processes the given source code by interpreting it within the current binding context, allowing for dynamic evaluation and configuration setup.
Methods included from VerbosePuts
Constructor Details
#initialize(verbose: false, &block) ⇒ Generator
The initialize method sets up the object by evaluating the provided block in the object’s context.
88 89 90 91 |
# File 'lib/context_spook/generator.rb', line 88 def initialize(verbose: false, &block) @verbose = !!verbose block and instance_eval(&block) end |
Instance Method Details
#context(&block) ⇒ Context
The context method creates or returns a context object.
98 99 100 101 102 103 104 105 |
# File 'lib/context_spook/generator.rb', line 98 def context(&block) if block @context and raise ArgumentError, "only one context allowed" @context = Context.new(verbose: @verbose, &block) else @context end end |
#output_context_size ⇒ Object
The output_context_size method prints the total size of the generated context JSON representation.
This method calculates the size of the context object when serialized to JSON, formats it using binary units (KiB, MiB, etc.), and outputs the result to standard error.
113 114 115 116 117 118 119 |
# File 'lib/context_spook/generator.rb', line 113 def output_context_size context_size = @context&.size.to_i json_content_size = Tins::Unit.format( context_size, format: '%.2f %U', unit: ?b, prefix: 1024 ) verbose_puts "Built #{json_content_size} of JSON context in total." end |
#parse(source) ⇒ ContextSpook::Generator (private)
The parse method processes the given source code by interpreting it within the current binding context, allowing for dynamic evaluation and configuration setup.
chaining after parsing
360 361 362 363 |
# File 'lib/context_spook/generator.rb', line 360 def parse(source) interpret_with_binding source, binding self end |