Skip to content

Commit

Permalink
Implement first version of FileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
mjacobus committed Apr 2, 2020
1 parent d193d7d commit d4ad9c7
Show file tree
Hide file tree
Showing 6 changed files with 367 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/koine/file_system.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# frozen_string_literal: true

require 'koine/file_system/version'
require 'koine/file_system/file_system'
require 'koine/file_system/adapters/base'
require 'koine/file_system/adapters/local'

module Koine
module FileSystem
class Error < StandardError; end
# Your code goes here...
end
end
79 changes: 79 additions & 0 deletions lib/koine/file_system/adapters/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

module Koine
module FileSystem
module Adapters
# rubocop:disable Lint/UnusedMethodArgument
class Base
def read(_path)
raise NotImplementedError
end

def read_stream(_path)
raise NotImplementedError
end

def write(_path, _contents, options: {})
raise NotImplementedError
end

def write_stream(_path, _contents, options: {})
raise NotImplementedError
end

def update(_path, _contents, options: {})
raise NotImplementedError
end

def update_stream(_path, _contents, options: {})
raise NotImplementedError
end

def has?(_path)
raise NotImplementedError
end

def delete(_path)
raise NotImplementedError
end

def read_and_delete(_path)
raise NotImplementedError
end

def rename(_from, _to)
raise NotImplementedError
end

def copy(_from, _to)
raise NotImplementedError
end

def mime_type(_path)
raise NotImplementedError
end

def size(_path)
raise NotImplementedError
end

def create_dir(_path)
raise NotImplementedError
end

def delete_dir(_path)
raise NotImplementedError
end

def list_contents(_path)
raise NotImplementedError
end

def list(_path, recursive: false)
raise NotImplementedError
end
end
# rubocop:enable Lint/UnusedMethodArgument
end
end
end
10 changes: 10 additions & 0 deletions lib/koine/file_system/adapters/local.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Koine
module FileSystem
module Adapters
class Local < Base
end
end
end
end
77 changes: 77 additions & 0 deletions lib/koine/file_system/file_system.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

module Koine
module FileSystem
# Inspired on
# https://flysystem.thephpleague.com/v1/docs/usage/filesystem-api/
class FileSystem
def initialize(adapter)
@adapter = adapter
end

def read(path, &block)
@adapter.read(path, &block)
end

def read_stream(path, &block)
@adapter.read_stream(path, &block)
end

def write(path, contents, options: {})
@adapter.write(path, contents, options: options)
end

def write_stream(path, contents, options: {})
@adapter.write_stream(path, contents, options: options)
end

def update(path, contents, options: {})
@adapter.update(path, contents, options: options)
end

def update_stream(path, contents, options: {})
@adapter.update_stream(path, contents, options: options)
end

def has?(path)
@adapter.has?(path)
end

def delete(path)
@adapter.delete(path)
end

def read_and_delete(path)
@adapter.read_and_delete(path)
end

def rename(from, to)
@adapter.rename(from, to)
end

def copy(from, to)
@adapter.copy(from, to)
end

def mime_type(path)
@adapter.mime_type(path)
end

def size(path)
@adapter.size(path)
end

def create_dir(path)
@adapter.create_dir(path)
end

def delete_dir(path)
@adapter.delete_dir(path)
end

def list(path, recursive: false)
@adapter.list(path, recursive: recursive)
end
end
end
end
85 changes: 85 additions & 0 deletions spec/koine/file_system/adapters/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Koine::FileSystem::Adapters::Base do
subject(:adapter) { described_class.new }

let(:path) { 'foo' }
let(:contents) { 'bar' }
let(:options) { {} }
let(:from) { 'from' }
let(:to) { 'to' }

# rubocop:disable RSpec/ExampleLength
it 'raises on every method' do
expect do
adapter.read(path)
end.to raise_error(NotImplementedError)

expect do
adapter.read_stream(path)
end.to raise_error(NotImplementedError)

expect do
adapter.write(path, contents, options: {})
end.to raise_error(NotImplementedError)

expect do
adapter.write_stream(path, contents, options: {})
end.to raise_error(NotImplementedError)

expect do
adapter.update(path, contents, options: {})
end.to raise_error(NotImplementedError)

expect do
adapter.update_stream(path, contents, options: {})
end.to raise_error(NotImplementedError)

expect do
adapter.has?(path)
end.to raise_error(NotImplementedError)

expect do
adapter.delete(path)
end.to raise_error(NotImplementedError)

expect do
adapter.read_and_delete(path)
end.to raise_error(NotImplementedError)

expect do
adapter.rename(from, to)
end.to raise_error(NotImplementedError)

expect do
adapter.copy(from, to)
end.to raise_error(NotImplementedError)

expect do
adapter.mime_type(path)
end.to raise_error(NotImplementedError)

expect do
adapter.size(path)
end.to raise_error(NotImplementedError)

expect do
adapter.create_dir(path)
end.to raise_error(NotImplementedError)

expect do
adapter.delete_dir(path)
end.to raise_error(NotImplementedError)

expect do
adapter.list_contents(path)
end.to raise_error(NotImplementedError)

expect do
adapter.list(path, recursive: false)
end.to raise_error(NotImplementedError)
end
# rubocop:enable RSpec/ExampleLength
end
113 changes: 113 additions & 0 deletions spec/koine/file_system/file_system_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Koine::FileSystem::FileSystem do
subject(:fs) { described_class.new(adapter) }

let(:adapter) { instance_double(described_class) }

it 'delegates #read to adapter' do
stub(:read, 'path').and_return('foo')

expect(fs.read('path')).to eq('foo')
end

it 'delegates #read_stream to adapter' do
stub(:read_stream, 'path').and_return('foo')

expect(fs.read_stream('path')).to eq('foo')
end

it 'delegates #write to adapter' do
stub(:write, 'path', 'content', options: 'the-options').and_return('foo')

expect(fs.write('path', 'content', options: 'the-options')).to eq('foo')
end

it 'delegates #write_stream to adapter' do
stub(:write_stream, 'path', 'content', options: 'the-options').and_return('foo')

expect(fs.write_stream('path', 'content', options: 'the-options')).to eq('foo')
end

it 'delegates #update to adapter' do
stub(:update, 'path', 'content', options: 'the-options').and_return('foo')

expect(fs.update('path', 'content', options: 'the-options')).to eq('foo')
end

it 'delegates #update_stream to adapter' do
stub(:update_stream, 'path', 'content', options: 'the-options').and_return('foo')

expect(fs.update_stream('path', 'content', options: 'the-options')).to eq('foo')
end

it 'delegates #has to adapter' do
stub(:has?, 'path').and_return('foo')

expect(fs.has?('path')).to eq('foo')
end

it 'delegates #delete to adapter' do
stub(:delete, 'path').and_return('foo')

expect(fs.delete('path')).to eq('foo')
end

it 'delegates #read_and_delete to adapter' do
stub(:read_and_delete, 'path').and_return('foo')

expect(fs.read_and_delete('path')).to eq('foo')
end

it 'delegates #rename to adapter' do
stub(:rename, 'foo', 'bar').and_return('baz')

expect(fs.rename('foo', 'bar')).to eq('baz')
end

it 'delegates #copy to adapter' do
stub(:copy, 'foo', 'bar').and_return('baz')

expect(fs.copy('foo', 'bar')).to eq('baz')
end

it 'delegates #mime_type to adapter' do
stub(:mime_type, 'path').and_return('foo')

expect(fs.mime_type('path')).to eq('foo')
end

it 'delegates #size to adapter' do
stub(:size, 'path').and_return('foo')

expect(fs.size('path')).to eq('foo')
end

it 'delegates #create_dir to adapter' do
stub(:create_dir, 'path').and_return('foo')

expect(fs.create_dir('path')).to eq('foo')
end

it 'delegates #delete_dir to adapter' do
stub(:delete_dir, 'path').and_return('foo')

expect(fs.delete_dir('path')).to eq('foo')
end

it 'delegates #list to adapter' do
stub(:list, 'path', recursive: 'r').and_return('foo')

expect(fs.list('path', recursive: 'r')).to eq('foo')
end

private

def stub(*args)
method = args.shift

allow(adapter).to receive(method).with(*args)
end
end

0 comments on commit d4ad9c7

Please sign in to comment.