-
Notifications
You must be signed in to change notification settings - Fork 0
/
lunchbox.rb
69 lines (57 loc) · 1.21 KB
/
lunchbox.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
#!/usr/bin/env ruby
require 'rubygems'
require 'sequel'
require 'base64'
require 'readline'
class Lunchbox
def initialize(db = "lunchbox.db")
@db = Sequel.sqlite(db)
@files = @db[:files]
end
def schema
@db.create_table :files do # Create a new table
column :name, :text
column :data, :blob
end
end
# alias to Base64
def encode(data)
Base64.encode64(data)
end
# alias to Base64
def decode(data)
Base64.decode64(data)
end
# add file
def add(filename)
b = File.read(filename)
@files << {:name => filename, :data => encode(b)}
puts "Added file: #{filename}"
end
# save file
def save(filename)
pic = Base64.decode64(@files[:name => filename][:data])
File.open("out-#{filename}", "w") do |file|
file << pic
end
puts "Saved file: #{filename}"
end
# list files
def list
puts "File Listing: \n"
@files.each do |r|
puts r[:name]
end
end
def delete(filename)
@files.filter(:name => filename).delete
puts "Deleted file: #{filename}"
end
def run
loop do
line = Readline::readline('lunchbox$ ')
Readline::HISTORY.push(line)
puts "#{line}"
end
end
end