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
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.
-
#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.
9 10 11 |
# File 'lib/valkyrie/storage/memory.rb', line 9 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.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/valkyrie/storage/memory.rb', line 93 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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/valkyrie/storage/memory.rb', line 52 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>
43 44 45 46 |
# File 'lib/valkyrie/storage/memory.rb', line 43 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.
69 70 71 |
# File 'lib/valkyrie/storage/memory.rb', line 69 def handles?(id:) id.to_s.start_with?("memory://") end |
#id_and_version(id) ⇒ Object
86 87 88 89 |
# File 'lib/valkyrie/storage/memory.rb', line 86 def id_and_version(id) id, version = id.to_s.split("#") [Valkyrie::ID.new(id), version] end |
#supports?(feature) ⇒ Boolean
Returns true if the adapter supports the given feature.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/valkyrie/storage/memory.rb', line 75 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
18 19 20 21 22 23 |
# File 'lib/valkyrie/storage/memory.rb', line 18 def upload(file:, original_filename:, resource: nil, **_extra_arguments) identifier = Valkyrie::ID.new("memory://#{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
31 32 33 34 35 36 37 38 39 |
# File 'lib/valkyrie/storage/memory.rb', line 31 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 |