Class: ContextSpook::Generator

Inherits:
Object
  • Object
show all
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

Methods included from VerbosePuts

#verbose_puts

Constructor Details

#initialize(verbose: false, &block) ⇒ Generator

The initialize method sets up the object by evaluating the provided block in the object’s context.

Parameters:

  • verbose (TrueClass, FalseClass) (defaults to: false)

    flag to enable verbose output during processing, defaults to lfalse.

  • block (Proc)

    a block of code to be evaluated within the object’s context If no block is given, the method does nothing.



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.

Parameters:

  • block (Proc)

    optional block to initialize the context

Returns:



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_sizeObject

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

Parameters:

  • source (String)

    the source code to be parsed and interpreted

Returns:



360
361
362
363
# File 'lib/context_spook/generator.rb', line 360

def parse(source)
  interpret_with_binding source, binding
  self
end