Module: Schrank

Defined in:
lib/schrank.rb,
lib/schrank/version.rb

Overview

Namespace module.

Constant Summary collapse

VERSION =
'2.0.0'

Class Method Summary collapse

Class Method Details

.development?Boolean

Returns true when the application is in development mode, otherwise returns false.


48
49
50
# File 'lib/schrank.rb', line 48

def self.development?
  ((defined?(Rails) && Rails.env) || ENV['RACK_ENV'] || ENV['APP_ENV']) == 'development'
end

.load(path, &defaults) ⇒ ActiveSupport::HashWithIndifferentAccess

Attempts to load YAML file. Falls back on defaults hash when file is not found.

In development mode, the defaults hash is merged with the YAML file, with the file taking precedence. This is intentional, since it allows shared config to live in your application source code, but per-user secrets (such as access keys) can be kept in a file and out of version control.

Examples:

Simply load your config.

config = Schrank.load(Rails.root.join('config/production.yml'))

Load your config, providing defaults.

config = Schrank.load(Rails.root.join('config/production.yml')) do
  {
    provider: 's3',
    prefix: 'folder/'
  }
end

Yield Returns:

  • (Hash)

    Default config


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/schrank.rb', line 31

def self.load(path, &defaults)
  config = {}

  if yaml_path(path).exist?
    config.merge!(defaults.call) if defaults && development?
    config.merge!(YAML.load(yaml_path(path).open('r:utf-8')))
  else
    puts "[Schrank] Could not find config file at #{path}, loading defaults..."
    config.merge!(defaults.call) if defaults
  end

  config.with_indifferent_access
end

.yaml_path(path) ⇒ Object


53
54
55
# File 'lib/schrank.rb', line 53

def self.yaml_path(path)
  Pathname.new(path)
end