Class: ActiveStorage::Service::MirrorService

Inherits:
ActiveStorage::Service show all
Defined in:
lib/active_storage/service/mirror_service.rb

Overview

Wraps a set of mirror services and provides a single ActiveStorage::Service object that will all have the files uploaded to them. A primary service is designated to answer calls to download, exists?, and url.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveStorage::Service

configure, #download, #download_chunk, #exist?, #headers_for_direct_upload, #update_metadata, #url, #url_for_direct_upload

Constructor Details

#initialize(primary:, mirrors:) ⇒ MirrorService

Returns a new instance of MirrorService.



21
22
23
# File 'lib/active_storage/service/mirror_service.rb', line 21

def initialize(primary:, mirrors:)
  @primary, @mirrors = primary, mirrors
end

Instance Attribute Details

#mirrorsObject (readonly)

Returns the value of attribute mirrors.



10
11
12
# File 'lib/active_storage/service/mirror_service.rb', line 10

def mirrors
  @mirrors
end

#primaryObject (readonly)

Returns the value of attribute primary.



10
11
12
# File 'lib/active_storage/service/mirror_service.rb', line 10

def primary
  @primary
end

Class Method Details

.build(primary:, mirrors:, configurator:, **options) ⇒ Object

Stitch together from named services.



15
16
17
18
19
# File 'lib/active_storage/service/mirror_service.rb', line 15

def self.build(primary:, mirrors:, configurator:, **options) #:nodoc:
  new \
    primary: configurator.build(primary),
    mirrors: mirrors.collect { |name| configurator.build name }
end

Instance Method Details

#delete(key) ⇒ Object

Delete the file at the key on all services.



34
35
36
# File 'lib/active_storage/service/mirror_service.rb', line 34

def delete(key)
  perform_across_services :delete, key
end

#delete_prefixed(prefix) ⇒ Object

Delete files at keys starting with the prefix on all services.



39
40
41
# File 'lib/active_storage/service/mirror_service.rb', line 39

def delete_prefixed(prefix)
  perform_across_services :delete_prefixed, prefix
end

#upload(key, io, checksum: nil, **options) ⇒ Object

Upload the io to the key specified to all services. If a checksum is provided, all services will ensure a match when the upload has completed or raise an ActiveStorage::IntegrityError.



27
28
29
30
31
# File 'lib/active_storage/service/mirror_service.rb', line 27

def upload(key, io, checksum: nil, **options)
  each_service.collect do |service|
    service.upload key, io.tap(&:rewind), checksum: checksum, **options
  end
end