Class: BucketStore::Disk

Inherits:
Object
  • Object
show all
Defined in:
lib/bucket_store/disk.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_dir) ⇒ Disk

Returns a new instance of Disk.



12
13
14
# File 'lib/bucket_store/disk.rb', line 12

def initialize(base_dir)
  @base_dir = File.expand_path(base_dir)
end

Class Method Details

.build(base_dir = ENV["DISK_ADAPTER_BASE_DIR"]) ⇒ Object



7
8
9
10
# File 'lib/bucket_store/disk.rb', line 7

def self.build(base_dir = ENV["DISK_ADAPTER_BASE_DIR"])
  base_dir ||= Dir.tmpdir
  Disk.new(base_dir)
end

Instance Method Details

#delete!(bucket:, key:) ⇒ Object



51
52
53
54
55
# File 'lib/bucket_store/disk.rb', line 51

def delete!(bucket:, key:)
  File.unlink(key_path(bucket, key))

  true
end

#download(bucket:, key:, file:) ⇒ Object



28
29
30
31
32
33
# File 'lib/bucket_store/disk.rb', line 28

def download(bucket:, key:, file:)
  File.open(key_path(bucket, key), "r") do |saved_file|
    file.write(saved_file.read)
    file.rewind
  end
end

#list(bucket:, key:, page_size:) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bucket_store/disk.rb', line 35

def list(bucket:, key:, page_size:)
  root = Pathname.new(bucket_root(bucket))

  Dir["#{root}/**/*"].
    reject { |absolute_path| File.directory?(absolute_path) }.
    map { |full_path| Pathname.new(full_path).relative_path_from(root).to_s }.
    select { |f| f.start_with?(key) }.
    each_slice(page_size).
    map do |keys|
    {
      bucket: bucket,
      keys: keys,
    }
  end.to_enum
end

#upload!(bucket:, key:, file:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bucket_store/disk.rb', line 16

def upload!(bucket:, key:, file:)
  File.open(key_path(bucket, key), "w") do |output_file|
    output_file.write(file.read)
    output_file.rewind
  end

  {
    bucket: bucket,
    key: key,
  }
end