Class: Ravioli::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/ravioli/builder.rb

Overview

</td><td>

ENV`

</td><td>

ENV`

</td></tr><tr><td>

‘config/credentials/staging.yml.enc` (only if running on staging)

</td><td>

ENV`

</td><td>

ENV`

</td></tr></tbody></table>

Instance Method Summary collapse

Constructor Details

#initialize(class_name: "Configuration", hijack: false, namespace: nil, strict: false) ⇒ Builder

Returns a new instance of Builder.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ravioli/builder.rb', line 54

def initialize(class_name: "Configuration", hijack: false, namespace: nil, strict: false)
  configuration_class = if namespace.present?
    namespace.class_eval <<-EOC, __FILE__, __LINE__ + 1
      # class Configuration < Ravioli::Configuration; end
      class #{class_name.to_s.classify} < Ravioli::Configuration; end
    EOC
    namespace.const_get(class_name)
  else
    Ravioli::Configuration
  end
  @strict = !!strict
  @configuration = configuration_class.new
  @reload_credentials = Set.new
  @reload_paths = Set.new
  @hijack = !!hijack

  if @hijack
    # Put this builder on the configurations stack - it will intercept setters on the underyling
    # configuration object as it loads files, and mark those files as needing a reload once
    # loading is complete
    Ravioli.configurations.push(self)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object (private)

rubocop:disable Style/MethodMissingSuper rubocop:disable Style/MissingRespondToMissing



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/ravioli/builder.rb', line 210

def method_missing(*args, &block)
  if @current_path
    @reload_paths.add(@current_path)
  end

  if @current_credentials
    @reload_credentials.add(@current_credentials)
  end

  configuration.send(*args, &block)
end

Instance Method Details

#add_staging_flag!(is_staging = Rails.env.production? && ENV["STAGING"].present?) ⇒ Object

Automatically infer a ‘staging` status from the current environment

Parameters:

  • is_staging (boolean, #present?) (defaults to: Rails.env.production? && ENV["STAGING"].present?)

    whether or not the current environment is considered a staging environment



81
82
83
84
# File 'lib/ravioli/builder.rb', line 81

def add_staging_flag!(is_staging = Rails.env.production? && ENV["STAGING"].present?)
  is_staging = is_staging.present?
  configuration.staging = is_staging
end

#auto_load_credentials!Object

Loads Rails encrypted credentials that it can. Checks for corresponding private key files, or ENV vars based on the credentials preadmlogic



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ravioli/builder.rb', line 97

def auto_load_credentials!
  # Load the root config (supports using the master key or `RAILS_ROOT_KEY`)
  load_credentials(
    key_path: "config/master.key",
    env_names: %w[master root],
    quiet: true,
  )

  # Load any environment-specific configuration on top of it. Since Rails will try
  # `RAILS_MASTER_KEY` from the environment, we assume the same
  load_credentials(
    "config/credentials/#{Rails.env}",
    key_path: "config/credentials/#{Rails.env}.key",
    env_names: ["master"],
    quiet: true,
  )

  # Apply staging configuration on top of THAT, if need be
  if configuration.staging?
    load_credentials(
      "config/credentials/staging",
      env_names: %w[staging master],
      key_path: "config/credentials/staging.key",
      quiet: true,
    )
  end
end

#auto_load_files!Object

Iterates through the config directory (including nested folders) and calls load_file on each JSON or YAML file it finds. Ignores ‘config/locales`.



89
90
91
92
93
94
# File 'lib/ravioli/builder.rb', line 89

def auto_load_files!
  config_dir = Rails.root.join("config")
  Dir[config_dir.join("{[!locales/]**/*,*}.{json,yaml,yml}")].each do |config_file|
    auto_load_file(config_file)
  end
end

#build!Object

When the builder is done working, lock the configuration and return it



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ravioli/builder.rb', line 126

def build!
  if @hijack
    # Replace this builder with the underlying configuration on the configurations stack...
    Ravioli.configurations.delete(self)
    Ravioli.configurations.push(configuration)

    # ...and then reload any config file that referenced the configuration the first time it was
    # loaded!
    @reload_paths.each do |path|
      auto_load_file(path)
    end
  end

  configuration.freeze
end

#load_credentials(path = "credentials", key_path: path, env_names: path.split("/").last, quiet: false) ⇒ Object

Load secure credentials using a key either from a file or the ENV



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ravioli/builder.rb', line 151

def load_credentials(path = "credentials", key_path: path, env_names: path.split("/").last, quiet: false)
  error = nil
  env_names = Array(env_names).map { |env_name| parse_env_name(env_name) }
  env_names.each do |env_name|
    credentials = parse_credentials(path, env_name: env_name, key_path: key_path, quiet: quiet)
    if credentials.present?
      configuration.append(credentials)
      return credentials
    end
  rescue => e
    error = e
  end

  if error
    attempted_names = ["key file `#{key_path}'"]
    attempted_names.push(*env_names.map { |env_name| "`ENV[\"#{env_name}\"]'" })
    attempted_names = attempted_names.to_sentence(two_words_connector: " or ", last_word_connector: ", or ")
    warn(
      "Could not decrypt `#{path}.yml.enc' with #{attempted_names}",
      error,
      critical: false,
    )
  end

  {}
end

#load_file(path, **options) ⇒ Object

Load a file either with a given path or by name (e.g. ‘config/whatever.yml` or `:whatever`)



143
144
145
146
147
148
# File 'lib/ravioli/builder.rb', line 143

def load_file(path, **options)
  config = parse_config_file(path, **options)
  configuration.append(config) if config.present?
rescue => error
  warn "Could not load config file #{path}", error
end