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

Returns a new instance of Memory.


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.

Parameters:


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

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

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

Return the file associated with the given identifier

Parameters:

Returns:

Raises:

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


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

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

#handles?(id:) ⇒ Boolean

Returns true if this adapter can handle this type of identifer.

Parameters:

Returns:

  • (Boolean)

    true if this adapter can handle this type of identifer


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

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

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

Parameters:

  • file (IO)
  • original_filename (String)
  • resource (Valkyrie::Resource) (defaults to: nil)
  • _extra_arguments (Hash)

    additional arguments which may be passed to other adapters

Returns:


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

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