Class: Bookbinder::FileSystem::Memory
- Inherits:
-
Object
- Object
- Bookbinder::FileSystem::Memory
- Defined in:
- lib/bookbinder/file_system/memory.rb
Instance Method Summary collapse
- #each(&blk) ⇒ Object
- #exists?(path) ⇒ Boolean
-
#get_file(path, mode = 'r', &blk) ⇒ Object
Creates a tempfile so you can do memory-efficient stuff.
- #get_io(path, &blk) ⇒ Object
-
#initialize ⇒ Memory
constructor
A new instance of Memory.
- #read(path) ⇒ Object
- #set_file(path, file) ⇒ Object
- #write(path, data) ⇒ Object
Constructor Details
#initialize ⇒ Memory
Returns a new instance of Memory.
3 4 5 |
# File 'lib/bookbinder/file_system/memory.rb', line 3 def initialize @index = {} end |
Instance Method Details
#each(&blk) ⇒ Object
58 59 60 |
# File 'lib/bookbinder/file_system/memory.rb', line 58 def each(&blk) @index.each_key(&blk) end |
#exists?(path) ⇒ Boolean
8 9 10 |
# File 'lib/bookbinder/file_system/memory.rb', line 8 def exists?(path) @index.key?(path) end |
#get_file(path, mode = 'r', &blk) ⇒ Object
Creates a tempfile so you can do memory-efficient stuff.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/bookbinder/file_system/memory.rb', line 32 def get_file(path, mode = 'r', &blk) read_before = mode[0] != 'w' write_after = mode[0] != 'r' begin tmpfile = Tempfile.new(File.basename(path)) if read_before tmpfile.write(read(path)) tmpfile.rewind end blk.call(tmpfile) if write_after tmpfile.rewind write(path, tmpfile.read) end ensure tmpfile.close tmpfile.unlink end end |
#get_io(path, &blk) ⇒ Object
25 26 27 |
# File 'lib/bookbinder/file_system/memory.rb', line 25 def get_io(path, &blk) blk.call(StringIO.new(read(path))) end |
#read(path) ⇒ Object
13 14 15 16 |
# File 'lib/bookbinder/file_system/memory.rb', line 13 def read(path) raise(Bookbinder::FileSystem::UnknownPath, path) unless exists?(path) @index[path] end |
#set_file(path, file) ⇒ Object
53 54 55 |
# File 'lib/bookbinder/file_system/memory.rb', line 53 def set_file(path, file) write(path, file.read) end |
#write(path, data) ⇒ Object
19 20 21 22 |
# File 'lib/bookbinder/file_system/memory.rb', line 19 def write(path, data) @index[path] = data nil end |