global_config

A Crystal library for globally storing configuration values in nested contexts.

GlobalConfig

A Crystal library for globally storing configuration values in nested contexts.

Build Status

Installation

Add this to your application's shard.yml:

dependencies:
  global_config:
    github: mosop/global_config

Usage

require "global_config"

Application Example

Let's make an application using the Amazon Web Services API.

Define Configuration Values

Define each configuration value into your class/module.

module MeetsAws
  extend GlobalConfig::Store

  global_config :access_key_id, env: {
    :MY_APP_AWS_ACCESS_KEY_ID,
    :AWS_ACCESS_KEY_ID
  }

  global_config :secret_access_key, env: {
    :MY_APP_AWS_SECRET_ACCESS_KEY,
    :AWS_SECRET_ACCESS_KEY
  }

  global_config :region, env: {
    :MY_APP_DEFUALT_REGION,
    :AWS_DEFAULT_REGION
  }
end

Define a Nesting Method

Define a nesting method, that is a helper for setting per-fiber configuration values in nested contexts.

MeetsAws.global_config_context :sign_in,
  :access_key_id,
  :secret_access_key,
  :region

Then the MeetsAws.sign_in method is automatically defined.

Getting Values

You can get the configuration values by these methods:

  • MeetsAws.access_key_id
  • MeetsAws.secret_access_key
  • MeetsAws.region

If the value is not set, an exception is raised.

The exception's class is automatically defined. The class name is automatically generated by converting the value's name to the upper-camel case and prefixing "No" to the name.

It looks like:

  • MeetsAws::NoAccessKeyId
  • MeetsAws::NoSecretAccessKey
  • MeetsAws::NoRegion

Nilable getters are automatically defined too. If a value is not set, they returns nil.

MeetsAws.access_key_id?
MeetsAws.secret_access_key?
MeetsAws.region?

Initializing Values from Environment Variables (Automatic)

Each configuation value is automatically initialized by getting the environment variables that are specified with the env option in the above step.

If multiple variables are defined, they are evaluated from the former one. If one of the variables is set, the variable is used and the latter ones are ignored.

For example, if the variables are set as:

  • AWS_ACCESS_KEY_ID: CEAKDQDSOYQYEWMAGAGO
  • AWS_SECRET_ACCESS_KEY: nON5qHbvaE70moLJ...
  • MY_APP_DEFUALT_REGION: ap-northeast-1
  • AWS_DEFAULT_REGION: us-east-1

then the configuration values look like:

MeetsAws.access_key_id # "CEAKDQDSOYQYEWMAGAGO"
MeetsAws.secret_access_key # "nON5qHbvaE70moLJ..."
MeetsAws.region # "ap-northeast-1"

Setting Values Globally

You can globally set the configuration values:

MeetsAws.access_key_id = "STWUSDFLRNNBBSCCSQXC"
MeetsAws.secret_access_key = "6YzNGvZ34IrVeE62..."
MeetsAws.region = "eu-central-1"

MeetsAws.access_key_id # "STWUSDFLRNNBBSCCSQXC"
MeetsAws.secret_access_key # "6YzNGvZ34IrVeE62..."
MeetsAws.region # "eu-central-1"

Note: This overwrites the values that are initialized with the environment variables.

Setting Values in Nested Contexts

Calling the nesting method, you can set the configuration values in nested contexts. The configuration values are restored after the method returns.

MeetsAws.access_key_id # "STWUSDFLRNNBBSCCSQXC"
MeetsAws.secret_access_key # "6YzNGvZ34IrVeE62..."
MeetsAws.region # "eu-central-1"

MeetsAws.sign_in(access_key_id: "QEQDEOJYFEUJIJUHVOQD", secret_access_key: "RRAUvo9m8I9TYyjT...", region: "ap-southeast-1") do
  MeetsAws.access_key_id # "QEQDEOJYFEUJIJUHVOQD"
  MeetsAws.secret_access_key # "RRAUvo9m8I9TYyjT..."
  MeetsAws.region # "ap-southeast-1"
end

MeetsAws.access_key_id # "STWUSDFLRNNBBSCCSQXC"
MeetsAws.secret_access_key # "6YzNGvZ34IrVeE62..."
MeetsAws.region # "eu-central-1"

You can nest the contexts multiple times.

MeetsAws.sign_in(...) do
  MeetsAws.sign_in(...) do
    # ...
  end
end

Setting a Single Value in Nested Contexts

You can set each of the configuration values in nested contexts:

MeetsAws.region # "eu-central-1"

MeetsAws.region("us-west-1") do
  MeetsAws.region # "us-west-1"
end

MeetsAws.region # "eu-central-1"
Repository

global_config

Owner
Statistic
  • 0
  • 0
  • 0
  • 2
  • 0
  • almost 8 years ago
  • January 7, 2017
License

MIT License

Links
Synced at

Mon, 23 Dec 2024 02:14:46 GMT

Languages