Class: Tori::Backend::FileSystem
- Inherits:
-
Object
- Object
- Tori::Backend::FileSystem
- Defined in:
- lib/tori/backend/filesystem.rb
Constant Summary collapse
- ResourceError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
- #copy_to(filename, tori_file, **opts) ⇒ Object
- #delete(filename) ⇒ Object
- #exist?(filename) ⇒ Boolean (also: #exists?)
-
#initialize(root) ⇒ FileSystem
constructor
A new instance of FileSystem.
- #open(filename, **rest, &block) ⇒ Object
- #otherwise(backend) ⇒ Object
- #path(filename) ⇒ Object
- #read(filename, len = nil, **args) ⇒ Object
- #write(filename, resource, opts = nil) ⇒ Object
Constructor Details
#initialize(root) ⇒ FileSystem
Returns a new instance of FileSystem.
9 10 11 12 |
# File 'lib/tori/backend/filesystem.rb', line 9 def initialize(root) @root = root FileUtils.mkdir_p @root.to_s end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
8 9 10 |
# File 'lib/tori/backend/filesystem.rb', line 8 def root @root end |
Instance Method Details
#copy_to(filename, tori_file, **opts) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/tori/backend/filesystem.rb', line 77 def copy_to(filename, tori_file, **opts) FileUtils.mkdir_p tori_file.path.dirname ::File.open(path(filename)) do |from| ::File.open(tori_file.path, 'w+') do |to| IO.copy_stream(from, to) end end end |
#delete(filename) ⇒ Object
41 42 43 |
# File 'lib/tori/backend/filesystem.rb', line 41 def delete(filename) ::File.unlink path(filename) end |
#exist?(filename) ⇒ Boolean Also known as: exists?
45 46 47 |
# File 'lib/tori/backend/filesystem.rb', line 45 def exist?(filename) ::File.exist? path(filename) end |
#open(filename, **rest, &block) ⇒ Object
72 73 74 |
# File 'lib/tori/backend/filesystem.rb', line 72 def open(filename, **rest, &block) ::File.open(path(filename), **rest, &block) end |
#otherwise(backend) ⇒ Object
91 92 93 |
# File 'lib/tori/backend/filesystem.rb', line 91 def otherwise(backend) Chain.new(self, backend) end |
#path(filename) ⇒ Object
87 88 89 |
# File 'lib/tori/backend/filesystem.rb', line 87 def path(filename) @root.join filename.to_s end |
#read(filename, len = nil, **args) ⇒ Object
66 67 68 69 70 |
# File 'lib/tori/backend/filesystem.rb', line 66 def read(filename, len=nil, **args) open(filename, **{mode: 'rb'}.merge(args)) do |f| f.read(len) end end |
#write(filename, resource, opts = nil) ⇒ Object
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 |
# File 'lib/tori/backend/filesystem.rb', line 14 def write(filename, resource, opts = nil) pathname = path(filename) FileUtils.mkdir_p pathname.dirname if resource.nil? && opts && opts[:body] resource = opts[:body] end case resource when String ::File.open(pathname, 'wb'){ |f| f.write resource } when Pathname # see also https://bugs.ruby-lang.org/issues/11199 ::File.open(resource) { |src| ::File.open(pathname, 'wb'){ |dst| ::IO.copy_stream src, dst } } when NilClass raise ResourceError, "null resource" else ::File.open(pathname, 'wb') do |dst| ::IO.copy_stream resource, dst end end end |