Class: BucketStore::InMemory

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInMemory

Returns a new instance of InMemory.



19
20
21
# File 'lib/bucket_store/in_memory.rb', line 19

def initialize
  reset!
end

Class Method Details

.buildObject



5
6
7
# File 'lib/bucket_store/in_memory.rb', line 5

def self.build
  InMemory.instance
end

.instanceObject



9
10
11
12
13
# File 'lib/bucket_store/in_memory.rb', line 9

def self.instance
  # rubocop:disable Style/ClassVars
  @@instance ||= new
  # rubocop:enable Style/ClassVars
end

.reset!Object



15
16
17
# File 'lib/bucket_store/in_memory.rb', line 15

def self.reset!
  instance.reset!
end

Instance Method Details

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



57
58
59
60
61
# File 'lib/bucket_store/in_memory.rb', line 57

def delete!(bucket:, key:)
  @buckets[bucket].delete(key)

  true
end

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



38
39
40
41
42
43
# File 'lib/bucket_store/in_memory.rb', line 38

def download(bucket:, key:, file:)
  file.tap do |f|
    f.write(@buckets[bucket].fetch(key))
    f.rewind
  end
end

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



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bucket_store/in_memory.rb', line 45

def list(bucket:, key:, page_size:)
  @buckets[bucket].keys.
    select { |k| k.start_with?(key) }.
    each_slice(page_size).
    map do |keys|
      {
        bucket: bucket,
        keys: keys,
      }
    end.to_enum
end

#reset!Object



23
24
25
# File 'lib/bucket_store/in_memory.rb', line 23

def reset!
  @buckets = Hash.new { |hash, key| hash[key] = {} }
end

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



27
28
29
30
31
32
33
34
35
36
# File 'lib/bucket_store/in_memory.rb', line 27

def upload!(bucket:, key:, file:)
  file.tap do |f|
    @buckets[bucket][key] = f.read
  end

  {
    bucket: bucket,
    key: key,
  }
end