Giter VIP home page Giter VIP logo

settled's Introduction

Settled

A framework for defining settings.

Installation

Add this line to your application's Gemfile:

gem 'settled'

And then execute:

$ bundle

Or install it yourself as:

$ gem install settled

Usage

Settled allows you to define settings in many different ways:

  • Environment variables (coming soon)
  • JSON configuration file(s)
  • YAML configuration file(s)
  • Property file(s) (coming soon)
  • Database table(s) (coming soon)
  • Inline (coming soon)

In fact, you can mix and match the ways you define setting within a single application.

For example:

Settled::Settings.build do

  instance :configatron
  instance :constant, :CONFIG

  container Hashie::Mash

  file :json, '/etc/local/foo.json'

  files :yaml, %w(/etc/local/bar.yml /etc/local/baz.yml)

  namespace :some_service do

    file :json, '/etc/local/app/some_service/config.json'

  end

end

Settled.configuration # your settings
CONFIG                # your settings
configatron           # your settings

File(s)

The file(s) directive allow you to specify one or more files you would like to read your settings from.

It is important to remember that the files are read and the values merged in the order you specify them. Thus, repeating settings will be overwritten by the most recent file that has them.

Given a JSON config file /etc/local/one.config.

{
  "foo": "bar",
  "bam": {
    "baz": "bang"
  }
}

And another JSON config file /etc/local/two.config.

{
  "foo": "not-bar"
}

You can read the specify to read the file.

Settled::Settings.build do
  ...
  files :json, '/etc/local/one.config', '/etc/local/two.config'
  ...
end

Settled.configuration['foo']        # => 'not-bar'
Settled.configuration['bam']['baz'] # => 'bang'

The container refers to the type of the settings instance. The default is hash. However, you can customize it with the container directive. Any class that accepts a hash to its initializer can be used as a container.

Given a JSON config file.

{
  "foo": "bar",
  "bam": {
    "baz": "bang"
  }
}

You could use a Hashie::Mash.

Settled::Settings.build do
  ...
  file :json, 'path/to/file'
  container Hashie::Mash
  ...
end

Settled.configuration.class   # => Hashie::Mash
Settled.configuration.bam.baz # => 'bang'

You could use a custom class.

class DotNotationContainer

  def initialize( settings_hash )
    @settings = settings_hash
  end

  def []( dot_path )
    val = @settings

    dot_path.split( '.' ).each do |part|
      val = val[part]
    end

    val
  end

end

Settled::Settings.build do
  ...
  file :json, 'path/to/file'
  container DotNotationContainer
  ...
end

Settled.configuration.class      # => DotNotationContainer
Settled.configuration['bam.baz'] # => 'bang'

Instance

Instances are the method(s) by which you would like to be able to access your settings. There are 2 instance strategies: [:configatron, :constant]. Of course, you're settings will always be available by Settled::configuration, even if the instance directive is not specified.

Constant

When you specify a constant, Settled creates a constant with the settings instance.

Settled::Settings.build do
  ...
  instance :constant, :CONFIG
  ...
end

Settled.configuration # your settings
CONFIG                # your settings

Configatron

When you specify configatron, Settled extends Kernel with a method configatron that returns your settings. This is for compatibility with the configatron project in order to provide a phased approach to a refactor instaead of a cut-over.

Settled::Settings.build do
  ...
  instance :configatron
  ...
end

Settled.configuration # your settings
configatron           # your settings

settled's People

Watchers

Jason Harrelson avatar James Cloos avatar  avatar

Forkers

kleopatra999

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.