diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-09 14:40:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-09 14:40:19 +0200 |
commit | 41afdb62f12e2464bfbda4abccad17e6db053687 (patch) | |
tree | 5eb110adff0dfb0446042825cb6580a6a92af923 /lib/active_storage | |
parent | f1489c22202858fe003968e93cc4dc435859483e (diff) | |
parent | 4d292fc0e75e35e177806b1ea821455fc0bc021c (diff) | |
download | rails-41afdb62f12e2464bfbda4abccad17e6db053687.tar.gz rails-41afdb62f12e2464bfbda4abccad17e6db053687.tar.bz2 rails-41afdb62f12e2464bfbda4abccad17e6db053687.zip |
Merge pull request #26 from jeremy/service-configurator
Clarify how a service can build other composed services
Diffstat (limited to 'lib/active_storage')
-rw-r--r-- | lib/active_storage/service.rb | 19 | ||||
-rw-r--r-- | lib/active_storage/service/configurator.rb | 37 | ||||
-rw-r--r-- | lib/active_storage/service/mirror_service.rb | 14 |
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:) |