Class: Etna::Filesystem::Mock
- Inherits:
-
Etna::Filesystem
- Object
- Etna::Filesystem
- Etna::Filesystem::Mock
- Defined in:
- lib/etna/filesystem.rb
Instance Method Summary collapse
- #exist?(src) ⇒ Boolean
-
#initialize(&new_io) ⇒ Mock
constructor
A new instance of Mock.
- #mkdir_p(dest) ⇒ Object
- #mkio(file, opts) ⇒ Object
- #mv(src, dest) ⇒ Object
- #tmpdir ⇒ Object
- #with_readable(src, opts = 'r') {|| ... } ⇒ Object
- #with_writeable(dest, opts = 'w', size_hint: nil) {|(@files[dest] = mkio(dest, opts))| ... } ⇒ Object
Methods inherited from Etna::Filesystem
Constructor Details
#initialize(&new_io) ⇒ Mock
Returns a new instance of Mock.
198 199 200 201 202 |
# File 'lib/etna/filesystem.rb', line 198 def initialize(&new_io) @files = {} @dirs = {} @new_io = new_io end |
Instance Method Details
#exist?(src) ⇒ Boolean
263 264 265 |
# File 'lib/etna/filesystem.rb', line 263 def exist?(src) @files.include?(src) || @dirs.include?(src) end |
#mkdir_p(dest) ⇒ Object
223 224 225 226 227 228 229 |
# File 'lib/etna/filesystem.rb', line 223 def mkdir_p(dest) while !@dirs.include?(dest) @dirs[dest] = Set.new break if dest == "." || dest == "/" dest, _ = File.split(dest) end end |
#mkio(file, opts) ⇒ Object
204 205 206 207 208 209 210 |
# File 'lib/etna/filesystem.rb', line 204 def mkio(file, opts) if @new_io.nil? StringIO.new else @new_io.call(file, opts) end end |
#mv(src, dest) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/etna/filesystem.rb', line 231 def mv(src, dest) if exist?(dest) raise "#{dest} already exists, cannot move" end if @dirs.include?(src) @dirs[dest] = @dirs.delete(src) elsif @files.include?(src) @files[dest] = @files.delete(src) else raise "#{src} does not exist, cannot move" end end |
#tmpdir ⇒ Object
245 246 247 248 |
# File 'lib/etna/filesystem.rb', line 245 def tmpdir require 'securerandom' "/tmp-#{SecureRandom::uuid}" end |
#with_readable(src, opts = 'r') {|| ... } ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/etna/filesystem.rb', line 250 def with_readable(src, opts = 'r', &block) if @dirs.include?(src) raise IOError.new("Path #{src} is a directory") end if !@files.include?(src) raise IOError.new("Path #{src} does not exist") end @files[src].rewind yield @files[src] end |
#with_writeable(dest, opts = 'w', size_hint: nil) {|(@files[dest] = mkio(dest, opts))| ... } ⇒ Object
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/etna/filesystem.rb', line 212 def with_writeable(dest, opts = 'w', size_hint: nil, &block) if @dirs.include?(dest) raise IOError.new("Path #{dest} is a directory") end dir, file = File.split(dest) @dirs[dir] ||= Set.new @dirs[dir].add(file) yield (@files[dest] = mkio(dest, opts)) end |