Class: PrettyIRB::SnippetManager
- Inherits:
-
Object
- Object
- PrettyIRB::SnippetManager
- Defined in:
- lib/pretty_irb/snippet_manager.rb
Constant Summary collapse
- SNIPPET_DIR =
File.("~/.pretty_irb_snippets")
Instance Method Summary collapse
-
#delete(name) ⇒ Object
Delete a snippet.
-
#initialize ⇒ SnippetManager
constructor
A new instance of SnippetManager.
-
#list ⇒ Object
List all snippets.
-
#load(name) ⇒ Object
Load a snippet.
-
#save(name, code, description = "") ⇒ Object
Save a code snippet.
-
#search(keyword) ⇒ Object
Search snippets by tag or keyword.
-
#show(name) ⇒ Object
Show snippet details.
Constructor Details
#initialize ⇒ SnippetManager
Returns a new instance of SnippetManager.
8 9 10 |
# File 'lib/pretty_irb/snippet_manager.rb', line 8 def initialize ensure_snippet_dir end |
Instance Method Details
#delete(name) ⇒ Object
Delete a snippet
75 76 77 78 79 80 81 |
# File 'lib/pretty_irb/snippet_manager.rb', line 75 def delete(name) filename = snippet_path(name) return "❌ Snippet '#{name}' not found" unless File.exist?(filename) File.delete(filename) "✓ Snippet '#{name}' deleted" end |
#list ⇒ Object
List all snippets
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/pretty_irb/snippet_manager.rb', line 38 def list return "📋 No snippets saved yet" unless Dir.exist?(SNIPPET_DIR) snippets = Dir.glob("#{SNIPPET_DIR}/*.json").map do |file| data = JSON.parse(File.read(file)) " • #{data['name']}: #{data['description']}" end return "📋 No snippets saved yet" if snippets.empty? "📋 Saved Snippets:\n#{snippets.join("\n")}" end |
#load(name) ⇒ Object
Load a snippet
29 30 31 32 33 34 35 |
# File 'lib/pretty_irb/snippet_manager.rb', line 29 def load(name) filename = snippet_path(name) return "❌ Snippet '#{name}' not found" unless File.exist?(filename) snippet = JSON.parse(File.read(filename)) snippet["code"] end |
#save(name, code, description = "") ⇒ Object
Save a code snippet
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/pretty_irb/snippet_manager.rb', line 13 def save(name, code, description = "") filename = snippet_path(name) snippet = { name: name, code: code, description: description, created_at: Time.now.to_s, tags: (code) } File.write(filename, JSON.pretty_generate(snippet)) "✓ Snippet '#{name}' saved" end |
#search(keyword) ⇒ Object
Search snippets by tag or keyword
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pretty_irb/snippet_manager.rb', line 52 def search(keyword) return "📋 No snippets saved yet" unless Dir.exist?(SNIPPET_DIR) results = Dir.glob("#{SNIPPET_DIR}/*.json").select do |file| data = JSON.parse(File.read(file)) data["name"].include?(keyword) || data["code"].include?(keyword) || data["tags"].any? { |tag| tag.include?(keyword) } end return "🔍 No matches found" if results.empty? output = "🔍 Found #{results.length} snippet(s):\n\n" results.each do |file| data = JSON.parse(File.read(file)) output += "📌 #{data['name']}\n" output += " Description: #{data['description']}\n" output += " Tags: #{data['tags'].join(', ')}\n\n" end output end |
#show(name) ⇒ Object
Show snippet details
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/pretty_irb/snippet_manager.rb', line 84 def show(name) filename = snippet_path(name) return "❌ Snippet '#{name}' not found" unless File.exist?(filename) snippet = JSON.parse(File.read(filename)) output = "📌 #{snippet['name']}\n" output += "Description: #{snippet['description']}\n" output += "Created: #{snippet['created_at']}\n" output += "Tags: #{snippet['tags'].join(', ')}\n\n" output += "Code:\n#{snippet['code']}\n" output end |