From 4d292fc0e75e35e177806b1ea821455fc0bc021c Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Sun, 9 Jul 2017 04:23:21 -0700 Subject: 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 --- lib/active_storage/service/configurator.rb | 37 +++++++++++++--------------- lib/active_storage/service/mirror_service.rb | 14 ++++------- 2 files changed, 22 insertions(+), 29 deletions(-) (limited to 'lib/active_storage/service') 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:) -- cgit v1.2.3