Module: Rails::Worktrees::ApplicationConfiguration

Defined in:
lib/rails/worktrees/application_configuration.rb

Overview

Copies explicit app configuration values onto the runtime configuration object.

Class Method Summary collapse

Class Method Details

.apply(source, configuration:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rails/worktrees/application_configuration.rb', line 7

def apply(source, configuration:)
  return configuration unless source

  Configuration::CONFIGURABLE_ATTRIBUTES.each do |attribute|
    next unless assigned?(source, attribute)

    configuration.public_send("#{attribute}=", value_for(source, attribute))
  end

  configuration
end

.assigned?(source, attribute) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/rails/worktrees/application_configuration.rb', line 19

def assigned?(source, attribute)
  key = attribute.to_sym
  hash = source.is_a?(Hash) ? source : source.to_h if source.respond_to?(:to_h)
  return hash.key?(key) || hash.key?(attribute.to_s) if hash

  source.respond_to?(:key?) && (source.key?(key) || source.key?(attribute.to_s))
end

.value_for(source, attribute) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/rails/worktrees/application_configuration.rb', line 27

def value_for(source, attribute)
  key = attribute.to_sym
  hash = source.is_a?(Hash) ? source : source.to_h if source.respond_to?(:to_h)
  return hash.fetch(key) { hash[attribute.to_s] } if hash

  source.public_send(attribute)
end