aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_storage/service/configurator.rb
blob: 5054e07ec71c6b349b6a61d49d3631b770f1ac90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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