Class: Shimmer::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/shimmer/utils/config.rb

Defined Under Namespace

Classes: MissingConfigError

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, **options) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/shimmer/utils/config.rb', line 9

def method_missing(method_name, **options)
  default_provided = options.key?(:default)
  default_value = options.delete(:default) if default_provided
  raise ArgumentError, "unknown option#{"s" if options.length > 1}: #{options.keys.join(", ")}." if options.any?

  credentials = Rails.application.credentials
  method_name = method_name.to_s
  type = :string
  key = method_name.delete_suffix("!").delete_suffix("?").to_sym
  required = method_name.end_with?("!")
  type = :bool if method_name.end_with?("?")
  value = @stubbed_values&.dig(key)
  value ||= ENV[key.to_s.upcase].presence
  value ||= credentials.dig(ENV["CONFIG_ENV"]&.to_sym || Rails.env.to_sym, key)
  value ||= credentials[key]
  value = default_value if value.nil?
  raise MissingConfigError, "#{key.upcase} environment value is missing" if required && value.blank?

  coerce value, type
end

Instance Method Details

#respond_to_missing?(method_name, include_all) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/shimmer/utils/config.rb', line 30

def respond_to_missing?(method_name, include_all)
  true
end

#stub(**values) ⇒ Object



34
35
36
37
38
39
# File 'lib/shimmer/utils/config.rb', line 34

def stub(**values)
  @stubbed_values = values.to_h.deep_transform_keys(&:to_sym)
  yield
ensure
  @stubbed_values = nil
end