Class: ContextSpook::Generator::Context

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

Constructor Details

#initialize(&block) ⇒ Context

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

Parameters:

  • block (Proc)

    A block to be evaluated within the object’s context.



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

def initialize(&block)
  block and instance_eval(&block)
end

Instance Method Details

#as_jsonHash

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

Returns:

  • (Hash)

    a hash containing the files, commands, and metadata



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

def as_json(*)
  {
    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,



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/context_spook/generator.rb', line 171

def command(shell_command, tags: nil)
  output = `#{shell_command}`
  exit_code = $?&.exitstatus.to_i
  if exit_code != 0
    STDERR.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
  )
  STDERR.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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/context_spook/generator.rb', line 137

def file(filename, tags: nil)
  content = File.read(filename)
  files[filename] = {
    namespace: scope_top,
    content:,
    size: content.size,
    lines: content.lines.size,
    tags: (Array(tags) if tags),
  }.compact
  file_size = Tins::Unit.format(
    content.size, format: '%.2f %U', unit: ?b, prefix: 1024
  )
  STDERR.puts "Read #{filename.inspect} (%s) for context." % file_size
  nil
rescue Errno::ENOENT => e
  STDERR.puts color(208) { "Reading #{filename.inspect} caused #{e.class}: #{e}" }
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



113
114
115
116
117
118
# File 'lib/context_spook/generator.rb', line 113

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



76
77
78
79
80
81
82
# File 'lib/context_spook/generator.rb', line 76

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

#to_jsonObject

The to_json method converts the object to a JSON representation by first generating its hash form and then serializing that hash into JSON format.



194
195
196
# File 'lib/context_spook/generator.rb', line 194

def to_json(*)
  as_json.to_json(*)
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



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

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