Class: Kurosawa::Filesystems::Local
- Defined in:
- lib/kurosawa/filesystems/local.rb
Instance Method Summary collapse
- #cleanup! ⇒ Object
- #del(key) ⇒ Object
- #exists(key) ⇒ Object
- #get(key) ⇒ Object
-
#initialize(root_path) ⇒ Local
constructor
A new instance of Local.
- #ls(prefix) ⇒ Object
- #path(key) ⇒ Object
- #put(key, value) ⇒ Object
- #root_path ⇒ Object
Constructor Details
#initialize(root_path) ⇒ Local
Returns a new instance of Local.
7 8 9 10 |
# File 'lib/kurosawa/filesystems/local.rb', line 7 def initialize(root_path) @root_path = File.(root_path) FileUtils.mkdir_p(@root_path) end |
Instance Method Details
#cleanup! ⇒ Object
51 52 53 54 55 56 |
# File 'lib/kurosawa/filesystems/local.rb', line 51 def cleanup! Dir['#{@root_path}**/*']. select { |d| File.directory? d }. select { |d| (Dir.entries(d) - %w[ . .. ]).empty? }. each { |d| Dir.rmdir d } end |
#del(key) ⇒ Object
41 42 43 44 |
# File 'lib/kurosawa/filesystems/local.rb', line 41 def del(key) puts "fs: delete #{path(key)}" File.delete(path(key)) if File.file?(path(key)) end |
#exists(key) ⇒ Object
46 47 48 49 |
# File 'lib/kurosawa/filesystems/local.rb', line 46 def exists(key) puts "fs: exists #{path(key)}" File.exists?(path(key)) and File.file?(path(key)) end |
#get(key) ⇒ Object
30 31 32 33 |
# File 'lib/kurosawa/filesystems/local.rb', line 30 def get(key) puts "fs: get #{path(key)}" File.read(path(key)) end |
#ls(prefix) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/kurosawa/filesystems/local.rb', line 20 def ls(prefix) puts "fs: ls #{prefix}" (Dir["#{@root_path}/#{prefix}*"] + Dir["#{@root_path}/#{prefix}/**/*"]). select{|x| File.file?(x)}. map{|x| x.sub(/^#{root_path}/, ""). gsub(/\/+/, "/")}. uniq end |
#path(key) ⇒ Object
16 17 18 |
# File 'lib/kurosawa/filesystems/local.rb', line 16 def path(key) File.join(@root_path, key) end |
#put(key, value) ⇒ Object
35 36 37 38 39 |
# File 'lib/kurosawa/filesystems/local.rb', line 35 def put(key, value) puts "fs: put #{path(key)}" FileUtils.mkdir_p(File.dirname(path(key))) File.write(path(key), value) end |
#root_path ⇒ Object
12 13 14 |
# File 'lib/kurosawa/filesystems/local.rb', line 12 def root_path @root_path end |