DEV Community

Edmundo Sanchez
Edmundo Sanchez

Posted on

Ruby, config this

Config_this

Have you ever wondered where is the right way to store sensitive information like passwords for your ruby scripts and gems?

I have, doing some research I found a few ways to do that, like the Simplest configuration block implementation for a ruby gem ever or Creating a configurable Ruby gem.

I also found using a file is the best way to store sensitive information but there are use cases to have Environmental variables or just hard-coding values.

However following that for every script I write is a bit cumbersome for me, I like easy things and automation so as a learning exercise I created a gem called config_this.

With Config_this you can load your configuration parameters from a YAML or json file, a block or a hash.

Installation

Add this line to your application's Gemfile:

gem 'config_this'
Enter fullscreen mode Exit fullscreen mode

And then execute:

$ bundle install

Or install it yourself as:

$ gem install config_this

Usage

The Config Module is the main interface for this gem, as you know you can not instantiate a Module so be careful with your values

require "config"
Enter fullscreen mode Exit fullscreen mode

Get the Configuration object

This will get the latest object you created.

Config.get
 => #<Config::Configuration:0x000055d538ef2dc0>
Enter fullscreen mode Exit fullscreen mode

Setting the configuration Object

If you plan on having only one configuration object you don't need to assign it, it will always return

Config.set(id:42, name: "Arthur Dent")
 => #<Config::Configuration:0x000055d538f3c9c0 @id=42, @name="Arthur Dent">
Enter fullscreen mode Exit fullscreen mode

If you do plan to have multiple configurations, handle them properly

Config.set(id:42, name: "Arthur Dent")
 => #<Config::Configuration:0x000055d538f3c9c0 @id=42, @name="Arthur Dent">
number1 = Config.set(id:1, name: "Stefán Karl Stefánsson")
 => #<Config::Configuration:0x000055d53866ea00 @id=1, @name="Stefán Karl Stefánsson">
Enter fullscreen mode Exit fullscreen mode

Setting it With a Hash

As you noticed, this is how you set it with a hash:

Config.set(id:42, name: "Arthur Dent")
 => #<Config::Configuration:0x000055d538f3c9c0 @id=42, @name="Arthur Dent">
Enter fullscreen mode Exit fullscreen mode

Setting it with a YAML file

Config.yaml_file("/absolute/path/file.yaml")
 => #<Config::Configuration:0x000055d538ed7d90 @id=42, @name="Arthur Dent", @weapon="Towel">
Enter fullscreen mode Exit fullscreen mode

Setting it with a JSON file

Config.json_file("/absolute/path/file.json")
 => #<Config::Configuration:0x000055d538a8f210 @id=42, @name="Arthur Dent", @weapon="Towel">
Enter fullscreen mode Exit fullscreen mode

Setting it with a Environmental Variables

Be sure the variables are set and accesible

ENV["id"] = "42"
ENV["name"] = "Arthur Dent"
Config.env_variables("id","name")
 => #<Config::Configuration:0x000055d538ef8b58 @id="42", @name="Arthur Dent">
Enter fullscreen mode Exit fullscreen mode

Setting it with a Block

To do this, you have to use the Config::Configuration class directly

config = Config::Configuration.new do |c|
  c.id = 1
  c.name = "Stefán Karl Stefánsson"
end
p config
 => #<Config::Configuration:0x00005630c8eda630 @id=1, @name="Stefán Karl Stefánsson">
Enter fullscreen mode Exit fullscreen mode

Using the Configuration

Config.get
 => #<Config::Configuration:0x000055d538f3c9c0 @id=42, @name="Arthur Dent">
Config.get.id
 => 42
config = Config.get
config.id
 => 42
config.name
 => "Arthur Dent"
Enter fullscreen mode Exit fullscreen mode

You can also just set new values to the Configuration object

config = Config.get
config.location = "Earth"
p config
 => #<Config::Configuration:0x00005630c87f0e00 @id=42, @name="Arthur Dent", @location="Earth">
Enter fullscreen mode Exit fullscreen mode

Top comments (0)