Class: Service::MemoryService
- Inherits:
-
ActiveStorage::Service
- Object
- ActiveStorage::Service
- Service::MemoryService
- Defined in:
- lib/active_storage/service/memory_service.rb
Overview
ActiveStorage in-memory service
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #delete_prefixed(prefix) ⇒ Object
- #download(key, &block) ⇒ Object
- #exist?(key) ⇒ Boolean
- #headers_for_direct_upload(_key, content_type:) ⇒ Object
-
#initialize(**config) ⇒ MemoryService
constructor
A new instance of MemoryService.
- #upload(key, io, checksum: nil) ⇒ Object
- #url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
Constructor Details
#initialize(**config) ⇒ MemoryService
Returns a new instance of MemoryService.
8 9 10 11 12 |
# File 'lib/active_storage/service/memory_service.rb', line 8 def initialize(**config) super @store = {} @config = config end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
6 7 8 |
# File 'lib/active_storage/service/memory_service.rb', line 6 def store @store end |
Instance Method Details
#delete(key) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/active_storage/service/memory_service.rb', line 37 def delete(key) instrument(:delete, key: key) do store.delete(key) rescue KeyError # Ignore key errors end end |
#delete_prefixed(prefix) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/active_storage/service/memory_service.rb', line 45 def delete_prefixed(prefix) instrument :delete_prefixed, prefix: prefix do store.each_key do |key| store.delete(key) if key.start_with?(prefix) end end end |
#download(key, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/active_storage/service/memory_service.rb', line 21 def download(key, &block) if block_given? instrument(:streaming_download, key: key) do stream key, &block end else instrument(:download, key: key) do io = StringIO.new(store.fetch(key)) io.set_encoding(io.string.encoding) io rescue KeyError raise ActiveStorage::FileNotFoundError end end end |
#exist?(key) ⇒ Boolean
53 54 55 56 57 58 59 |
# File 'lib/active_storage/service/memory_service.rb', line 53 def exist?(key) instrument(:exist, key: key) do |payload| answer = store.key?(key) payload[:exist] = answer answer end end |
#headers_for_direct_upload(_key, content_type:) ⇒ Object
79 80 81 |
# File 'lib/active_storage/service/memory_service.rb', line 79 def headers_for_direct_upload(_key, content_type:, **) { 'Content-Type' => content_type } end |
#upload(key, io, checksum: nil) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/active_storage/service/memory_service.rb', line 14 def upload(key, io, checksum: nil, **) instrument(:upload, key: key) do store[key] = io.read ensure_integrity_of(key, checksum) if checksum end end |
#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/active_storage/service/memory_service.rb', line 61 def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, **) instrument :url, key: key do |payload| verified_token_with_expiration = generate_verified_token( key, expires_in: expires_in, content_type: content_type, content_length: content_length, checksum: checksum ) url_helpers.update_rails_memory_service_url( verified_token_with_expiration, ).tap do |generated_url| payload[:url] = generated_url end end end |