Class: ContextSpook::Generator
- Inherits:
-
Object
- Object
- ContextSpook::Generator
- Includes:
- OutputContext, 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 Attribute Summary collapse
-
#format ⇒ String
readonly
The format method returns the format identifier for the context output.
-
#manifest ⇒ TrueClass, FalseClass
readonly
The manifest method returns whether the generator is in manifest mode, where file entries omit their content.
-
#token_estimator ⇒ Object
writeonly
The token_estimator attribute allows setting the logic used to estimate the number of tokens in a serialized context.
-
#verbose ⇒ TrueClass, FalseClass
readonly
The verbose method returns the verbose flag indicating whether verbose output is enabled.
Instance Method Summary collapse
-
#context(&block) ⇒ Context
The context method creates or returns a context object.
-
#initialize(verbose: false, format: nil, manifest: false, &block) ⇒ Generator
constructor
The initialize method sets up the object by evaluating the provided block in the object's context.
-
#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 OutputContext
#output_context, #output_context_size
Methods included from VerbosePuts
Constructor Details
#initialize(verbose: false, format: nil, manifest: false, &block) ⇒ Generator
The initialize method sets up the object by evaluating the provided block in the object's context.
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/context_spook/generator.rb', line 100 def initialize(verbose: false, format: nil, manifest: false, &block) @verbose = !!verbose @format = (format || 'JSON').upcase @manifest = !!manifest %w[ TOON JSON ].include?(@format) or raise ArgumentError, "format needs to be either JSON or TOON, was #{@format.inspect}" self.token_estimator = ContextSpook::Utils.method(:estimate_tokens) block and instance_eval(&block) end |
Instance Attribute Details
#format ⇒ String (readonly)
The format method returns the format identifier for the context output.
76 77 78 |
# File 'lib/context_spook/generator.rb', line 76 def format @format end |
#manifest ⇒ TrueClass, FalseClass (readonly)
The manifest method returns whether the generator is in manifest mode, where file entries omit their content.
82 83 84 |
# File 'lib/context_spook/generator.rb', line 82 def manifest @manifest end |
#token_estimator=(value) ⇒ Object (writeonly)
The token_estimator attribute allows setting the logic used to estimate the number of tokens in a serialized context.
It expects an object that responds to #call(text), where text is the serialized context string.
91 92 93 |
# File 'lib/context_spook/generator.rb', line 91 def token_estimator=(value) @token_estimator = value end |
#verbose ⇒ TrueClass, FalseClass (readonly)
The verbose method returns the verbose flag indicating whether verbose output is enabled.
71 72 73 |
# File 'lib/context_spook/generator.rb', line 71 def verbose @verbose end |
Instance Method Details
#context(&block) ⇒ Context
The context method creates or returns a context object.
116 117 118 119 120 121 122 123 |
# File 'lib/context_spook/generator.rb', line 116 def context(&block) if block @context and raise ArgumentError, "only one context allowed" @context = Context.new(generator: self, &block) else @context end 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
398 399 400 401 |
# File 'lib/context_spook/generator.rb', line 398 def parse(source) interpret_with_binding source, binding self end |