1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/lib/active_storage/service/mirror_service.rb
Jeremy Daer 4d292fc0e7
Clarify how a service can build other composed services
* Service.build takes the literal YAML config hash for the service and a
  reference to the Configurator that's doing the building.
* Services that compose additional services can use the Configurator to
  look them up and build them by name. See MirrorService for an example.

References #23
2017-07-09 04:46:59 -07:00

40 lines
1 KiB
Ruby

require "active_support/core_ext/module/delegation"
class ActiveStorage::Service::MirrorService < ActiveStorage::Service
attr_reader :primary, :mirrors
delegate :download, :exist?, :url, to: :primary
# Stitch together from named services.
def self.build(primary:, mirrors:, configurator:, **options) #:nodoc:
new \
primary: configurator.build(primary),
mirrors: mirrors.collect { |name| configurator.build name }
end
def initialize(primary:, mirrors:)
@primary, @mirrors = primary, mirrors
end
def upload(key, io, checksum: nil)
each_service.collect do |service|
service.upload key, io.tap(&:rewind), checksum: checksum
end
end
def delete(key)
perform_across_services :delete, key
end
private
def each_service(&block)
[ primary, *mirrors ].each(&block)
end
def perform_across_services(method, *args)
# FIXME: Convert to be threaded
each_service.collect do |service|
service.public_send method, *args
end
end
end