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, #exist?, #headers_for_direct_upload, #url, #url_for_direct_upload

Constructor Details

#initialize(primary:, mirrors:) ⇒ MirrorService

Returns a new instance of MirrorService.



18
19
20
# File 'lib/active_storage/service/mirror_service.rb', line 18

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

Instance Attribute Details

#mirrorsObject (readonly)

Returns the value of attribute mirrors.



7
8
9
# File 'lib/active_storage/service/mirror_service.rb', line 7

def mirrors
  @mirrors
end

#primaryObject (readonly)

Returns the value of attribute primary.



7
8
9
# File 'lib/active_storage/service/mirror_service.rb', line 7

def primary
  @primary
end

Class Method Details

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

Stitch together from named services.



12
13
14
15
16
# File 'lib/active_storage/service/mirror_service.rb', line 12

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.



31
32
33
# File 'lib/active_storage/service/mirror_service.rb', line 31

def delete(key)
  perform_across_services :delete, key
end

#upload(key, io, checksum: nil) ⇒ 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`.



24
25
26
27
28
# File 'lib/active_storage/service/mirror_service.rb', line 24

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