aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_storage
diff options
context:
space:
mode:
authorJeremy Daer <jeremydaer@gmail.com>2017-07-09 04:23:21 -0700
committerJeremy Daer <jeremydaer@gmail.com>2017-07-09 04:46:59 -0700
commit4d292fc0e75e35e177806b1ea821455fc0bc021c (patch)
tree6d81c06d71b8a6428a3b6edceb334fdb9ea6cabb /lib/active_storage
parent1a17cfb9d9719c8458fb1259371c173627b96d8f (diff)
downloadrails-4d292fc0e75e35e177806b1ea821455fc0bc021c.tar.gz
rails-4d292fc0e75e35e177806b1ea821455fc0bc021c.tar.bz2
rails-4d292fc0e75e35e177806b1ea821455fc0bc021c.zip
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
Diffstat (limited to 'lib/active_storage')
-rw-r--r--lib/active_storage/service.rb19
-rw-r--r--lib/active_storage/service/configurator.rb37
-rw-r--r--lib/active_storage/service/mirror_service.rb14
3 files changed, 36 insertions, 34 deletions
diff --git a/lib/active_storage/service.rb b/lib/active_storage/service.rb
index 6978ce6429..021c695a07 100644
--- a/lib/active_storage/service.rb
+++ b/lib/active_storage/service.rb
@@ -32,15 +32,24 @@
class ActiveStorage::Service
class ActiveStorage::IntegrityError < StandardError; end
+ extend ActiveSupport::Autoload
+ autoload :Configurator
+
+ # Configure an Active Storage service by name from a set of configurations,
+ # typically loaded from a YAML file. The Active Storage engine uses this
+ # to set the global Active Storage service when the app boots.
def self.configure(service_name, configurations)
- require 'active_storage/service/configurator'
- Configurator.new(service_name, configurations).build
+ Configurator.build(service_name, configurations)
end
# Override in subclasses that stitch together multiple services and hence
- # need to do additional lookups from configurations. See MirrorService.
- def self.build(service_config, all_configurations) #:nodoc:
- new(service_config)
+ # need to build additional services using the configurator.
+ #
+ # Passes the configurator and all of the service's config as keyword args.
+ #
+ # See MirrorService for an example.
+ def self.build(configurator:, service: nil, **service_config) #:nodoc:
+ new(**service_config)
end
def upload(key, io, checksum: nil)
diff --git a/lib/active_storage/service/configurator.rb b/lib/active_storage/service/configurator.rb
index 5054e07ec7..2159c80df9 100644
--- a/lib/active_storage/service/configurator.rb
+++ b/lib/active_storage/service/configurator.rb
@@ -1,31 +1,28 @@
class ActiveStorage::Service::Configurator #:nodoc:
- def initialize(service_name, configurations)
- @service_name, @configurations = service_name.to_sym, configurations.symbolize_keys
- end
+ attr_reader :configurations
- def build
- service_class.build(service_config.except(:service), @configurations)
+ def self.build(service_name, configurations)
+ new(configurations).build(service_name)
end
- private
- def service_class
- resolve service_class_name
- end
+ def initialize(configurations)
+ @configurations = configurations.symbolize_keys
+ end
- def service_class_name
- service_config.fetch :service do
- raise "Missing Active Storage `service: …` configuration for #{service_config.inspect}"
- end
- end
+ def build(service_name)
+ config = config_for(service_name.to_sym)
+ resolve(config.fetch(:service)).build(**config, configurator: self)
+ end
- def service_config
- @configurations.fetch @service_name do
- raise "Missing configuration for the #{@service_name.inspect} Active Storage service. Configurations available for #{@configurations.keys.inspect}"
+ private
+ def config_for(name)
+ configurations.fetch name do
+ raise "Missing configuration for the #{name.inspect} Active Storage service. Configurations available for #{configurations.keys.inspect}"
end
end
- def resolve(service_class_name)
- require "active_storage/service/#{service_class_name.to_s.downcase}_service"
- ActiveStorage::Service.const_get(:"#{service_class_name}Service")
+ def resolve(class_name)
+ require "active_storage/service/#{class_name.to_s.downcase}_service"
+ ActiveStorage::Service.const_get(:"#{class_name}Service")
end
end
diff --git a/lib/active_storage/service/mirror_service.rb b/lib/active_storage/service/mirror_service.rb
index 8a51a75684..54465cad05 100644
--- a/lib/active_storage/service/mirror_service.rb
+++ b/lib/active_storage/service/mirror_service.rb
@@ -5,15 +5,11 @@ class ActiveStorage::Service::MirrorService < ActiveStorage::Service
delegate :download, :exist?, :url, to: :primary
- # Stitch together from named configuration.
- def self.build(service_config, all_configurations) #:nodoc:
- primary = ActiveStorage::Service.configure(service_config.fetch(:primary), all_configurations)
-
- mirrors = service_config.fetch(:mirrors).collect do |service_name|
- ActiveStorage::Service.configure(service_name.to_sym, all_configurations)
- end
-
- new primary: primary, mirrors: mirrors
+ # 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:)