Class: Shrine::Storage::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/shrine/storage/memory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store = {}) ⇒ Memory

Returns a new instance of Memory.


11
12
13
# File 'lib/shrine/storage/memory.rb', line 11

def initialize(store = {})
  @store = store
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.


9
10
11
# File 'lib/shrine/storage/memory.rb', line 9

def store
  @store
end

Instance Method Details

#clear!Object


44
45
46
# File 'lib/shrine/storage/memory.rb', line 44

def clear!
  store.clear
end

#delete(id) ⇒ Object


35
36
37
# File 'lib/shrine/storage/memory.rb', line 35

def delete(id)
  store.delete(id)
end

#delete_prefixed(delete_prefix) ⇒ Object


39
40
41
42
# File 'lib/shrine/storage/memory.rb', line 39

def delete_prefixed(delete_prefix)
  delete_prefix = delete_prefix.chomp("/") + "/"
  store.delete_if { |key, _value| key.start_with?(delete_prefix) }
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)

27
28
29
# File 'lib/shrine/storage/memory.rb', line 27

def exists?(id)
  store.key?(id)
end

#open(id) ⇒ Object


19
20
21
22
23
24
25
# File 'lib/shrine/storage/memory.rb', line 19

def open(id, **)
  io = StringIO.new(store.fetch(id))
  io.set_encoding(io.string.encoding) # Ruby 2.7.0 – https://bugs.ruby-lang.org/issues/16497
  io
rescue KeyError
  raise Shrine::FileNotFound, "file #{id.inspect} not found on storage"
end

#upload(io, id) ⇒ Object


15
16
17
# File 'lib/shrine/storage/memory.rb', line 15

def upload(io, id, **)
  store[id] = io.read
end

#url(id) ⇒ Object


31
32
33
# File 'lib/shrine/storage/memory.rb', line 31

def url(id, *)
  "memory://#{id}"
end