Class: Valkyrie::Storage::Memory

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

Overview

Note:

this adapter is used primarily for testing, and is not recommended in cases where you want to preserve real data

Implements the DataMapper Pattern to store binary data in memory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemory



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

def initialize
  @cache = {}
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



8
9
10
# File 'lib/valkyrie/storage/memory.rb', line 8

def cache
  @cache
end

Instance Method Details

#delete(id:) ⇒ Object

Delete the file on disk associated with the given identifier.



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

def delete(id:)
  cache.delete(id)
  nil
end

#find_by(id:) ⇒ Valkyrie::StorageAdapter::StreamFile

Return the file associated with the given identifier

Raises:

  • Valkyrie::StorageAdapter::FileNotFound if nothing is found



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

def find_by(id:)
  raise Valkyrie::StorageAdapter::FileNotFound unless cache[id]
  cache[id]
end

#handles?(id:) ⇒ Boolean



33
34
35
# File 'lib/valkyrie/storage/memory.rb', line 33

def handles?(id:)
  id.to_s.start_with?("memory://")
end

#upload(file:, original_filename:, resource: nil) ⇒ Valkyrie::StorageAdapter::StreamFile



17
18
19
20
# File 'lib/valkyrie/storage/memory.rb', line 17

def upload(file:, original_filename:, resource: nil)
  identifier = Valkyrie::ID.new("memory://#{resource.id}")
  cache[identifier] = Valkyrie::StorageAdapter::StreamFile.new(id: identifier, io: file)
end