Class: PromptObjects::CLI::Sandbox
- Inherits:
-
Object
- Object
- PromptObjects::CLI::Sandbox
- Defined in:
- lib/prompt_objects/cli/repl_command.rb
Overview
Sandbox manages an isolated environment for testing
Instance Attribute Summary collapse
-
#objects_dir ⇒ Object
readonly
Returns the value of attribute objects_dir.
-
#original_objects_dir ⇒ Object
readonly
Returns the value of attribute original_objects_dir.
-
#primitives_dir ⇒ Object
readonly
Returns the value of attribute primitives_dir.
-
#sandbox_dir ⇒ Object
readonly
Returns the value of attribute sandbox_dir.
Instance Method Summary collapse
- #cleanup(keep_changes: false) ⇒ Object
- #copy_back_changes ⇒ Object
-
#initialize(original_objects_dir) ⇒ Sandbox
constructor
A new instance of Sandbox.
- #setup ⇒ Object
- #show_new_files ⇒ Object
Constructor Details
#initialize(original_objects_dir) ⇒ Sandbox
Returns a new instance of Sandbox.
12 13 14 15 16 17 18 19 |
# File 'lib/prompt_objects/cli/repl_command.rb', line 12 def initialize(original_objects_dir) @original_objects_dir = File.(original_objects_dir) @sandbox_dir = Dir.mktmpdir("prompt_objects_sandbox_") @objects_dir = File.join(@sandbox_dir, "objects") @primitives_dir = File.join(@sandbox_dir, "primitives") setup end |
Instance Attribute Details
#objects_dir ⇒ Object (readonly)
Returns the value of attribute objects_dir.
10 11 12 |
# File 'lib/prompt_objects/cli/repl_command.rb', line 10 def objects_dir @objects_dir end |
#original_objects_dir ⇒ Object (readonly)
Returns the value of attribute original_objects_dir.
10 11 12 |
# File 'lib/prompt_objects/cli/repl_command.rb', line 10 def original_objects_dir @original_objects_dir end |
#primitives_dir ⇒ Object (readonly)
Returns the value of attribute primitives_dir.
10 11 12 |
# File 'lib/prompt_objects/cli/repl_command.rb', line 10 def primitives_dir @primitives_dir end |
#sandbox_dir ⇒ Object (readonly)
Returns the value of attribute sandbox_dir.
10 11 12 |
# File 'lib/prompt_objects/cli/repl_command.rb', line 10 def sandbox_dir @sandbox_dir end |
Instance Method Details
#cleanup(keep_changes: false) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/prompt_objects/cli/repl_command.rb', line 36 def cleanup(keep_changes: false) if keep_changes copy_back_changes end FileUtils.rm_rf(@sandbox_dir) puts "Sandbox cleaned up" end |
#copy_back_changes ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/prompt_objects/cli/repl_command.rb', line 44 def copy_back_changes # Find new/modified objects new_objects = Dir.glob(File.join(@objects_dir, "*.md")).select do |sandbox_file| original = File.join(@original_objects_dir, File.basename(sandbox_file)) !File.exist?(original) || File.read(sandbox_file) != File.read(original) end # Find new primitives new_primitives = Dir.glob(File.join(@primitives_dir, "*.rb")) return if new_objects.empty? && new_primitives.empty? puts "\nChanges to copy back:" new_objects.each { |f| puts " + objects/#{File.basename(f)}" } new_primitives.each { |f| puts " + primitives/#{File.basename(f)}" } print "\nCopy these to main project? (y/n): " answer = $stdin.gets&.chomp&.downcase return unless answer == "y" new_objects.each do |f| FileUtils.cp(f, @original_objects_dir) end if new_primitives.any? target_primitives = File.join(File.dirname(@original_objects_dir), "primitives") FileUtils.mkdir_p(target_primitives) new_primitives.each do |f| FileUtils.cp(f, target_primitives) end end puts "Changes copied to main project" end |
#setup ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/prompt_objects/cli/repl_command.rb', line 21 def setup FileUtils.mkdir_p(@objects_dir) FileUtils.mkdir_p(@primitives_dir) # Copy existing objects to sandbox if Dir.exist?(@original_objects_dir) Dir.glob(File.join(@original_objects_dir, "*.md")).each do |src| FileUtils.cp(src, @objects_dir) end end puts "Sandbox initialized at: #{@sandbox_dir}" puts " Objects copied: #{Dir.glob(File.join(@objects_dir, '*.md')).count}" end |
#show_new_files ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/prompt_objects/cli/repl_command.rb', line 79 def show_new_files new_objects = Dir.glob(File.join(@objects_dir, "*.md")).reject do |sandbox_file| original = File.join(@original_objects_dir, File.basename(sandbox_file)) File.exist?(original) && File.read(sandbox_file) == File.read(original) end new_primitives = Dir.glob(File.join(@primitives_dir, "*.rb")) { objects: new_objects, primitives: new_primitives } end |