Module: Unifig::Init

Defined in:
lib/unifig/init.rb

Overview

Initializes Unifig with methods based on YAML.

Class Method Summary collapse

Class Method Details

.load(str, env: nil) ⇒ Object

Loads a string of YAML to configure Unifig.

Examples:

Unifig::Init.load(<<~YML, env: :development)
  unifig:
    envs:
      development:
        providers: local

  FOO_BAR:
    value: "baz"
YML

Parameters:

  • str (String)

    A YAML config.

  • env (Symbol) (defaults to: nil)

    An environment name to load.

Raises:



27
28
29
30
31
32
# File 'lib/unifig/init.rb', line 27

def load(str, env: nil)
  yml = Psych.load(str, symbolize_names: true)
  exec!(yml, env: env)
rescue Psych::SyntaxError, Psych::BadAlias => e
  raise YAMLSyntaxError, e.message
end

.load_file(file_path, env: nil) ⇒ Object

Loads a YAML file to configure Unifig.

Examples:

Unifig::Init.load_file('config.yml', env: :development)

Parameters:

  • file_path (String)

    The path to a YAML config file.

  • env (Symbol) (defaults to: nil)

    An environment name to load.

Raises:



43
44
45
46
47
# File 'lib/unifig/init.rb', line 43

def load_file(file_path, env: nil)
  # Ruby 2.7 Psych.load_file doesn't support the :symbolize_names flag.
  # After Ruby 2.7 this can be changed to Psych.load_file if that's faster.
  load(File.read(file_path), env: env)
end