Class: Rails::Credentials::Conflict::PathResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/credentials/conflict/path_resolver.rb,
sig/rails/credentials/conflict/path_resolver.rbs

Overview

Resolves file system paths for Rails encrypted credentials and their corresponding key files.

Supports both the default credentials (+config/credentials.yml.enc+) and environment-specific ones (+config/credentials/.yml.enc+).

Constant Summary collapse

VALID_ENVIRONMENT =
/\A[a-z0-9_]+\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment = nil) ⇒ PathResolver

Returns a new instance of PathResolver.

Parameters:

  • environment (String, nil) (defaults to: nil)


16
17
18
19
20
21
22
23
24
# File 'lib/rails/credentials/conflict/path_resolver.rb', line 16

def initialize(environment = nil)
  if environment && !VALID_ENVIRONMENT.match?(environment.to_s)
    raise Error,
          "Invalid environment name: #{environment.inspect}. " \
          "Must contain only lowercase letters, digits, and underscores."
  end

  @environment = environment
end

Instance Attribute Details

#environmentString? (readonly)

Returns the value of attribute environment.

Returns:

  • (String, nil)


14
15
16
# File 'lib/rails/credentials/conflict/path_resolver.rb', line 14

def environment
  @environment
end

Instance Method Details

#credentials_pathPathname

Returns the absolute Pathname to the encrypted credentials file.

Returns:

  • (Pathname)


27
28
29
30
31
32
33
# File 'lib/rails/credentials/conflict/path_resolver.rb', line 27

def credentials_path
  if environment
    ::Rails.root.join("config", "credentials", "#{environment}.yml.enc")
  else
    ::Rails.root.join("config", "credentials.yml.enc")
  end
end

#key_pathPathname

Returns the absolute Pathname to the encryption key file.

Returns:

  • (Pathname)


36
37
38
39
40
41
42
# File 'lib/rails/credentials/conflict/path_resolver.rb', line 36

def key_path
  if environment
    ::Rails.root.join("config", "credentials", "#{environment}.key")
  else
    ::Rails.root.join("config", "master.key")
  end
end

#relative_credentials_pathPathname

Returns the credentials path relative to Rails.root.

Returns:

  • (Pathname)


45
46
47
# File 'lib/rails/credentials/conflict/path_resolver.rb', line 45

def relative_credentials_path
  credentials_path.relative_path_from(::Rails.root)
end