From e5503399c0bb992ec1fd47b4bc371b8aef679e37 Mon Sep 17 00:00:00 2001 From: Jeremy Daer Date: Sat, 8 Jul 2017 16:55:50 -0700 Subject: Configure services that reference other services * Move service configuration from the Engine to Service * Delegate configuration mechanics to internal Service::Configurator * Delegate service building to the concrete Service classes, allowing them to configure composed services. * Implement for the Mirror service. --- lib/active_storage/service/configurator.rb | 31 ++++++++++++++++++++++++++++ lib/active_storage/service/mirror_service.rb | 11 ++++++++++ 2 files changed, 42 insertions(+) create mode 100644 lib/active_storage/service/configurator.rb (limited to 'lib/active_storage/service') diff --git a/lib/active_storage/service/configurator.rb b/lib/active_storage/service/configurator.rb new file mode 100644 index 0000000000..5054e07ec7 --- /dev/null +++ b/lib/active_storage/service/configurator.rb @@ -0,0 +1,31 @@ +class ActiveStorage::Service::Configurator #:nodoc: + def initialize(service_name, configurations) + @service_name, @configurations = service_name.to_sym, configurations.symbolize_keys + end + + def build + service_class.build(service_config.except(:service), @configurations) + end + + private + def service_class + resolve service_class_name + end + + def service_class_name + service_config.fetch :service do + raise "Missing Active Storage `service: …` configuration for #{service_config.inspect}" + end + 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}" + 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") + end +end diff --git a/lib/active_storage/service/mirror_service.rb b/lib/active_storage/service/mirror_service.rb index 7ec166aace..eec1f2af65 100644 --- a/lib/active_storage/service/mirror_service.rb +++ b/lib/active_storage/service/mirror_service.rb @@ -5,6 +5,17 @@ class ActiveStorage::Service::MirrorService < ActiveStorage::Service delegate :download, :exist?, :url, to: :primary + # Stitch together from named configuration. + def self.build(mirror_config, all_configurations) #:nodoc: + primary = ActiveStorage::Service.configure(mirror_config.fetch(:primary), all_configurations) + + mirrors = mirror_config.fetch(:mirrors).collect do |service_name| + ActiveStorage::Service.configure(service_name.to_sym, all_configurations) + end + + new primary: primary, mirrors: mirrors + end + def initialize(primary:, mirrors:) @primary, @mirrors = primary, mirrors end -- cgit v1.2.3