Sha256: b224ee1175fa5ead037693309706cbf25492f126b12065970afab48cc10ae1aa

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

module Tori
  module Backend
    class FileSystem
      attr_accessor :root
      def initialize(root)
        @root = root
        FileUtils.mkdir_p @root.to_s
      end

      def write(filename, resource, opts = nil)
        case resource
        when String
          ::File.open(path(filename), 'wb'){ |f| f.write resource }
        when Pathname
          # see also https://bugs.ruby-lang.org/issues/11199
          ::File.open(resource) { |src|
            FileUtils.mkdir_p path(filename).dirname
            ::File.open(path(filename), 'wb'){ |dst|
              ::IO.copy_stream src, dst
            }
          }
        else
          ::File.open(path(filename), 'wb') do |dst|
            ::IO.copy_stream resource, dst
          end
        end
      end

      def delete(filename)
        ::File.unlink path(filename)
      end

      def exist?(filename)
        ::File.exist? path(filename)
      end
      alias exists? exist?

      def read(filename)
        ::File.read(path(filename), mode: 'rb')
      end

      def path(filename)
        @root.join filename.to_s
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tori-0.5.0 lib/tori/backend/filesystem.rb
tori-0.4.1 lib/tori/backend/filesystem.rb
tori-0.4.0 lib/tori/backend/filesystem.rb
tori-0.3.0 lib/tori/backend/filesystem.rb