Class: ContextSpook::Generator::Context

Inherits:
Object
  • Object
show all
Includes:
VerbosePuts, Term::ANSIColor, Tins::DSLAccessor, Tins::Scope
Defined in:
lib/context_spook/generator.rb

Overview

The Context class represents and manages project context data, providing structured storage for file contents, command outputs, variables, and metadata that can be serialized to JSON for AI assistance.

Instance Method Summary collapse

Methods included from VerbosePuts

#verbose_puts

Constructor Details

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

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 false.

  • block (Proc)

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



137
138
139
140
# File 'lib/context_spook/generator.rb', line 137

def initialize(verbose: false, &block)
  @verbose = !!verbose
  block and instance_eval(&block)
end

Instance Method Details

#as_json(*ignored) ⇒ Hash

The as_json method converts the context’s files, commands, and metadata into a hash representation.

Parameters:

  • ignored (Array)

    ignored arguments

Returns:

  • (Hash)

    a hash containing the files, commands, and metadata



327
328
329
330
331
332
333
334
# File 'lib/context_spook/generator.rb', line 327

def as_json(*ignored)
  {
    files:,
    commands:,
    metadata:,
    variables:
  }
end

#command(shell_command, tags: nil) ⇒ Hash

The command method executes a shell command and stores its result.

This method runs a given shell command and records the output, exit code, working directory, and optional tags in the commands hash.

with the command

and metadata

Parameters:

  • shell_command (String)

    the shell command to execute

  • tags (Array<String>, nil) (defaults to: nil)

    optional array of tags to associate

Returns:

  • (Hash)

    the stored command result including output, exit code,



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/context_spook/generator.rb', line 290

def command(shell_command, tags: nil)
  output = `#{shell_command}`
  exit_code = $?&.exitstatus.to_i
  if exit_code != 0
    verbose_puts color(208) {
      "Executing #{shell_command.inspect} resulted in exit code #{exit_code}."
    }
  end
  commands[shell_command] = {
    namespace: scope_top,
    output:,
    exit_code:,
    working_directory: Dir.pwd,
    tags: (Array(tags) if tags),
  }.compact
  output_size = Tins::Unit.format(
    output.size, format: '%.2f %U', unit: ?b, prefix: 1024
  )
  verbose_puts "Executed #{shell_command.inspect} with output (%s) for context." % output_size
  nil
end

#file(filename, tags: nil) ⇒ Hash

The file method associates a file with the current scope and stores its content.

It reads the specified file and creates an entry in the files hash with the file’s content, along with its namespace and optional tags.

Parameters:

  • filename (String)

    the path to the file to be read and stored

  • tags (Array<String>, nil) (defaults to: nil)

    optional array of tags to associate with the file

Returns:

  • (Hash)

    the created file entry with content, namespace, and tags



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/context_spook/generator.rb', line 255

def file(filename, tags: nil)
  content = File.read(filename)
  files[filename] = {
    namespace: scope_top,
    content:,
    size: content.size,
    lines: content.lines.size,
    content_types: MIME::Types.type_for(filename).map(&:content_type).full?,
    tags: (Array(tags) if tags),
  }.compact
  file_size = Tins::Unit.format(
    content.size, format: '%.2f %U', unit: ?b, prefix: 1024
  )
  verbose_puts "Read #{filename.inspect} (%s) for context." % file_size
  nil
rescue Errno::ENOENT => e
  verbose_puts color(208) { "Reading #{filename.inspect} caused #{e.class}: #{e}" }
end

#json(filename) ⇒ Object?

The json method reads and parses a JSON file, returning the parsed data structure.

This method attempts to load a JSON file from the specified path and returns the resulting Ruby data structure. It provides verbose output about the file size when successfully reading the file. In case of file not found errors, it outputs a colored warning message to standard error and returns nil.

Parameters:

  • filename (String)

    the path to the JSON file to be read and parsed

Returns:

  • (Object, nil)

    the parsed JSON data structure or nil if the file cannot be read



203
204
205
206
207
208
209
210
211
212
# File 'lib/context_spook/generator.rb', line 203

def json(filename)
  file_size = Tins::Unit.format(
    File.size(filename), format: '%.2f %U', unit: ?b, prefix: 1024
  )
  verbose_puts "Read #{filename.inspect} as JSON (%s) for context." % file_size
  JSON.load_file(filename)
rescue Errno::ENOENT, JSON::ParserError => e
  verbose_puts color(208) { "Reading #{filename.inspect} as JSON caused #{e.class}: #{e}" }
  nil
end

#meta(**m) ⇒ Object

The meta method assigns metadata key-value pairs to the metadata hash.

Parameters:

  • m (Hash)

    a hash of metadata key-value pairs to be added



184
185
186
187
188
189
# File 'lib/context_spook/generator.rb', line 184

def meta(**m)
  m.each do |name, value|
    [name.to_sym] = value
  end
  nil
end

#namespace(name) {|block| ... } ⇒ Object

The namespace method creates a scoped block with a given name.

Parameters:

  • name (Object)

    the name to scope the block with

Yields:

  • (block)

    executes the block within the created scope



147
148
149
150
151
152
153
# File 'lib/context_spook/generator.rb', line 147

def namespace(name, &block)
  name = name.to_sym
  scope_block(name) do
    instance_eval(&block)
  end
  nil
end

#sizeInteger

The size method calculates and returns the byte size of the JSON representation of the context.

This method determines the size in bytes of the JSON-serialized version of the context object, which is useful for understanding the total data payload being sent to an AI assistant.

context

Returns:

  • (Integer)

    the size in bytes of the JSON representation of the



345
346
347
# File 'lib/context_spook/generator.rb', line 345

def size
  to_json.size
end

#variable(**v) ⇒ Object

The variable method assigns key-value pairs to the variables hash.

values

Parameters:

  • v (Hash)

    a hash containing variable names as keys and their



167
168
169
170
171
172
# File 'lib/context_spook/generator.rb', line 167

def variable(**v)
  v.each do |name, value|
    variables[name.to_sym] = value
  end
  nil
end

#yaml(filename) ⇒ Object?

The yaml method reads and parses a YAML file, returning the resulting data structure.

This method attempts to load a YAML file from the specified path and returns the resulting Ruby data structure. It provides verbose output about the file size when successfully reading the file. In case of file not found errors or YAML syntax errors, it outputs a colored warning message to standard error and returns nil.

Parameters:

  • filename (String)

    the path to the YAML file to be read and parsed

Returns:

  • (Object, nil)

    the parsed YAML data structure or nil if the file cannot be read



227
228
229
230
231
232
233
234
235
236
# File 'lib/context_spook/generator.rb', line 227

def yaml(filename)
  file_size = Tins::Unit.format(
    File.size(filename), format: '%.2f %U', unit: ?b, prefix: 1024
  )
  verbose_puts "Read #{filename.inspect} as YAML (%s) for context." % file_size
  YAML.load_file(filename)
rescue Errno::ENOENT, Psych::SyntaxError => e
  verbose_puts color(208) { "Reading #{filename.inspect} as YAML caused #{e.class}: #{e}" }
  nil
end