Class: ContextSpook::Generator

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

Instance Method Summary collapse

Methods included from OutputContext

#output_context, #output_context_size

Methods included from VerbosePuts

#verbose_puts

Constructor Details

#initialize(verbose: false, format: nil, &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.



82
83
84
85
86
87
88
89
# File 'lib/context_spook/generator.rb', line 82

def initialize(verbose: false, format: nil, &block)
  @verbose = !!verbose
  @format  = (format || 'JSON').upcase
  %w[ TOON JSON ].include?(@format) or
    raise ArgumentError,
      "format needs to be either JSON or TOON, was #{@format.inspect}"
  block and instance_eval(&block)
end

Instance Attribute Details

#formatString (readonly)

The format method returns the format identifier for the context output.

Returns:

  • (String)

    the format identifier, either ‘JSON’ or ‘TOON’



71
72
73
# File 'lib/context_spook/generator.rb', line 71

def format
  @format
end

#verboseTrueClass, FalseClass (readonly)

The verbose method returns the verbose flag indicating whether verbose output is enabled.

Returns:

  • (TrueClass, FalseClass)

    true if verbose output is enabled, false otherwise



66
67
68
# File 'lib/context_spook/generator.rb', line 66

def verbose
  @verbose
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:



96
97
98
99
100
101
102
103
# File 'lib/context_spook/generator.rb', line 96

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

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