Module: Cre

Defined in:
lib/cre.rb,
lib/cre/railtie.rb,
lib/cre/version.rb

Defined Under Namespace

Classes: Railtie

Constant Summary collapse

Error =

Raised when Cre is used outside a booted Rails application.

Class.new(StandardError)
VERSION =
'2.2.0'

Class Method Summary collapse

Class Method Details

.defined_environmentsObject

NOTE: kept public for backward compatibility - the historic private keyword never applied to def self. methods, so this was always callable.



41
42
43
44
45
# File 'lib/cre.rb', line 41

def self.defined_environments
  @defined_environments ||=
    Dir.glob(Rails.root.join("config", "environments", "*.rb"))
       .map { |filename| File.basename(filename, ".rb").to_sym }
end

.dig(*credentials) ⇒ Object

Fetching Rails encrypted credentials with:

`Rails.application.credentials.dig(:environment, :credential)`

Is too long for my taste. Using Cre.dig is a lot shorter:

`Cre.dig(:environment, :credential)`

By default Rails.env finds the default environment for us when none is specified and it is called with the credential only:

`Cre.dig(:password)`

The dig method here is just an aesthetic thing to keep us in context.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cre.rb', line 18

def self.dig(*credentials)
  ensure_rails_application!
  return nil if credentials.empty?

  env, credentials = if defined_environments.include? credentials[0].to_sym
    [credentials[0].to_sym, credentials[1..-1].flatten.map(&:to_sym)]
  else
    [Rails.env.to_sym, [credentials].flatten.map(&:to_sym)]
  end
  Rails.application.credentials.dig(env, *credentials)
end