Class: Valkyrie::Storage::Memory
- Inherits:
-
Object
- Object
- Valkyrie::Storage::Memory
- Defined in:
- lib/valkyrie/storage/memory.rb
Overview
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
Constant Summary collapse
- PROTOCOL =
'memory://'
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
Instance Method Summary collapse
-
#delete(id:) ⇒ Object
Delete the file on disk associated with the given identifier.
-
#find_by(id:) ⇒ Valkyrie::StorageAdapter::StreamFile
Return the file associated with the given identifier.
- #find_versions(id:) ⇒ Array<Valkyrie::StorageAdapter::StreamFile>
-
#handles?(id:) ⇒ Boolean
True if this adapter can handle this type of identifer.
- #id_and_version(id) ⇒ Object
-
#initialize ⇒ Memory
constructor
A new instance of Memory.
-
#protocol ⇒ String
Identifier prefix.
-
#supports?(feature) ⇒ Boolean
True if the adapter supports the given feature.
- #upload(file:, original_filename:, resource: nil, **_extra_arguments) ⇒ Valkyrie::StorageAdapter::StreamFile
- #upload_version(id:, file:) ⇒ Valkyrie::StorageAdapter::StreamFile
Constructor Details
#initialize ⇒ Memory
Returns a new instance of Memory.
11 12 13 |
# File 'lib/valkyrie/storage/memory.rb', line 11 def initialize @cache = {} end |
Instance Attribute Details
#cache ⇒ Object (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.
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/valkyrie/storage/memory.rb', line 100 def delete(id:) base_id, version = id_and_version(id) if version && cache[base_id][:current]&.version_id != id cache[base_id][:versions].reject! do |file| file.version_id == id end else cache.delete(base_id) end nil end |
#find_by(id:) ⇒ Valkyrie::StorageAdapter::StreamFile
Return the file associated with the given identifier
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/valkyrie/storage/memory.rb', line 54 def find_by(id:) no_version_id, _version = id_and_version(id) raise Valkyrie::StorageAdapter::FileNotFound unless cache[no_version_id] version = if id == no_version_id cache[id][:current] else find_versions(id: no_version_id).find do |file| file.version_id == id end end raise Valkyrie::StorageAdapter::FileNotFound unless version version end |
#find_versions(id:) ⇒ Array<Valkyrie::StorageAdapter::StreamFile>
45 46 47 48 |
# File 'lib/valkyrie/storage/memory.rb', line 45 def find_versions(id:) return [] if cache[id].nil? [cache[id][:current] || nil].compact + cache[id].fetch(:versions, []) end |
#handles?(id:) ⇒ Boolean
Returns true if this adapter can handle this type of identifer.
71 72 73 |
# File 'lib/valkyrie/storage/memory.rb', line 71 def handles?(id:) id.to_s.start_with?(protocol) end |
#id_and_version(id) ⇒ Object
93 94 95 96 |
# File 'lib/valkyrie/storage/memory.rb', line 93 def id_and_version(id) id, version = id.to_s.split("#") [Valkyrie::ID.new(id), version] end |
#protocol ⇒ String
Returns identifier prefix.
89 90 91 |
# File 'lib/valkyrie/storage/memory.rb', line 89 def protocol PROTOCOL end |
#supports?(feature) ⇒ Boolean
Returns true if the adapter supports the given feature.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/valkyrie/storage/memory.rb', line 77 def supports?(feature) case feature when :versions true when :version_deletion true else false end end |
#upload(file:, original_filename:, resource: nil, **_extra_arguments) ⇒ Valkyrie::StorageAdapter::StreamFile
20 21 22 23 24 25 |
# File 'lib/valkyrie/storage/memory.rb', line 20 def upload(file:, original_filename:, resource: nil, **_extra_arguments) identifier = Valkyrie::ID.new("#{protocol}#{resource.id}") version_id = Valkyrie::ID.new("#{identifier}##{SecureRandom.uuid}") cache[identifier] ||= {} cache[identifier][:current] = Valkyrie::StorageAdapter::StreamFile.new(id: identifier, io: file, version_id: version_id) end |
#upload_version(id:, file:) ⇒ Valkyrie::StorageAdapter::StreamFile
33 34 35 36 37 38 39 40 41 |
# File 'lib/valkyrie/storage/memory.rb', line 33 def upload_version(id:, file:) # Get previous file and add a UUID to the end of it. new_file = Valkyrie::StorageAdapter::StreamFile.new(id: id, io: file, version_id: Valkyrie::ID.new("#{id}##{SecureRandom.uuid}")) current_file = cache[id][:current] cache[id][:current] = new_file cache[id][:versions] ||= [] cache[id][:versions].prepend(current_file) if current_file new_file end |