Class: ContextSpook::Generator::Context
- Inherits:
-
Object
- Object
- ContextSpook::Generator::Context
- 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
-
#as_json ⇒ Hash
The as_json method converts the context’s files, commands, and metadata into a hash representation.
-
#command(shell_command, tags: nil) ⇒ Hash
The command method executes a shell command and stores its result.
-
#file(filename, tags: nil) ⇒ Hash
The file method associates a file with the current scope and stores its content.
-
#initialize(&block) ⇒ Context
constructor
The initialize method sets up the object by evaluating a block in the object’s context.
-
#meta(**m) ⇒ Object
The meta method assigns metadata key-value pairs to the metadata hash.
-
#namespace(name) {|block| ... } ⇒ Object
The namespace method creates a scoped block with a given name.
-
#to_json ⇒ Object
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.
-
#variable(**v) ⇒ Object
The variable method assigns key-value pairs to the variables hash.
Constructor Details
#initialize(&block) ⇒ Context
The initialize method sets up the object by evaluating a block in 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_json ⇒ Hash
The as_json method converts the context’s files, commands, and metadata into a hash representation.
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
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() if ), }.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.
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() if ), }.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.
113 114 115 116 117 118 |
# File 'lib/context_spook/generator.rb', line 113 def (**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.
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_json ⇒ Object
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
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 |