class Settei::Loaders::SimpleLoader

Loader designed to load hash from environment variable and YAML files. After initialization, call {#load} to load hash into memory, then call {#as_hash}, {#as_env_value}, or {#as_env_assignment} to obtain the results. e.g. `loader.load(:production).as_hash`

Attributes

env_name[R]

Public Class Methods

new(dir: nil, env_name: 'APP_CONFIG') click to toggle source

@param dir [String] path of directory containing config YAML files @param env_name [String] name for environment variable

# File lib/settei/loaders/simple_loader.rb, line 16
def initialize(dir: nil, env_name: 'APP_CONFIG')
  @dir = dir
  @env_name = env_name
end

Public Instance Methods

as_env_assignment() click to toggle source

Convenience method for outputting “NAME=VALUE” in one call. @return [String] {#as_env_value} with assignment to `env_name`

# File lib/settei/loaders/simple_loader.rb, line 70
def as_env_assignment
  ensure_loaded!

  "#{@env_name}=#{as_env_value}"
end
as_env_value() click to toggle source

@return [String] serialized config hash, for passing as environment variable

# File lib/settei/loaders/simple_loader.rb, line 60
def as_env_value
  ensure_loaded!

  Base64.strict_encode64(
    Zlib::Deflate.deflate(@yaml)
  )
end
as_hash() click to toggle source

@return [Hash]

# File lib/settei/loaders/simple_loader.rb, line 53
def as_hash
  ensure_loaded!

  YAML.load(@yaml)
end
load(environment = nil) click to toggle source

Loads yaml file into memory. If `ENV` exists, loader loads from it, otherwise load from YAML file.

YAML file is picked based on provided `environment`. If environment is not specified, or specified environment file does not exist, default.yml is loaded.

@param environment [String] application environment (e.g. production/test) @return [self] for further chaining

# File lib/settei/loaders/simple_loader.rb, line 32
def load(environment = nil)
  if !ENV[@env_name].nil?
    @yaml = Zlib::Inflate.inflate(
      Base64.strict_decode64(ENV[@env_name])
    )
  else
    if environment
      env_specific_file_path = "#{@dir}/#{environment}.yml"
      if File.exist?(env_specific_file_path)
        file_path = env_specific_file_path
      end
    end
    file_path ||= "#{@dir}/default.yml"

    @yaml = open(file_path).read
  end

  self
end

Private Instance Methods

ensure_loaded!() click to toggle source
# File lib/settei/loaders/simple_loader.rb, line 78
def ensure_loaded!
  if !@yaml
    raise 'Call `load` first to load YAML into memory.'
  end
end