Sha256: 963117d5e6dafe83c4354118d492431bb1f224589deba9c3cebb35d725f11977

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

module Tori
  module Backend
    class FileSystem
      ResourceError = Class.new(StandardError)

      attr_accessor :root
      def initialize(root)
        @root = root
        FileUtils.mkdir_p @root.to_s
      end

      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

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

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

      def read(filename, *args)
        if args.last.kind_of?(Hash)
          opt = args.pop
        else
          opt = {}
        end
        open(filename, {mode: 'rb'}.merge(opt)) do |f|
          f.read(*args)
        end
      end

      def open(filename, *rest, &block)
        ::File.open(path(filename), *rest, &block)
      end

      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

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

      def otherwise(backend)
        Chain.new(self, backend)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tori-0.7.2 lib/tori/backend/filesystem.rb
tori-0.7.1 lib/tori/backend/filesystem.rb
tori-0.7.0 lib/tori/backend/filesystem.rb