Code Monkey home page Code Monkey logo

sting's Introduction

Sting - Minimal Settings Library

Gem Version Build Status Maintainability


Sting is a minimal, lightweight, multi-YAML settings library.


Installation

$ gem install sting

Features

  • Aggressively minimalistic.
  • Settings are accessible through a globally available class.
  • Can be used either as a singleton class or as an instance.
  • Load and merge one or more YAML files or hashes.
  • Settings objects are standard ruby hashes, arrays and basic types.
  • Ability to update settings at runtime.
  • Ability to extend (import) other YAML files.
  • ERB code in the YAML files will be evaluated.

Nonfeatures

  • No dot notation access to nested values - Use Settings.server['host'] instead of Settings.server.host.
  • No special generators for Rails. Usage with rails is still trivial.

Usage

Using as a singleton class

require 'sting'

# If you want to use a different name than Sting (optional)
Settings = Sting

# Load some YAML files. If the provided filename does not end with '.yml' or 
# '.yaml', we will add '.yml' to it.
Settings << 'one'
Settings << 'two.yml'

# Adding additional files can also be done with `#push`. These two are the 
# same.
Settings << 'one'
Settings.push 'one'

# Merge with another options hash
Settings << { port: 3000, host: 'localhost' }

# Load all files from a directory
Settings << "dir/that-contains/yamls"

# Load all files from a directory, recursively
Settings << "dir/that-contains/yamls/**/*.yml"

# Access values
p Settings.host
p Settings['host']
p Settings[:host]

# Access nested values
p Settings.server['host']

# Access nested values safely (get nil if any of the keys does not exist)
p Settings[:server, :host]
p Settings[:server, :production, :host]

# Get the hash of all values
p Settings.settings

# Check if a key is defined
p Settings.has_key? :hello

# Access value, but raise an exception if it does not exist
p Settings.host!

# Access boolean values, or check if a key exists
p Settings.host?

# Update a value (in memory only, not in file)
Settings.port = 3000

# Reset (erase) all values
Settings.reset!

Using as an instance

All the above operations are also available to instances of Sting.

require 'sting'

# Create an instance.
config = Sting.new

# Or, create an instance, and provide the first source file to it.
config = Sting.new 'settings'

# Load additional YAML files. 
config << 'local_settings'

Extending YAML files

Sting uses ExtendedYAML, which means you can use the extends key to load one or more YAML files into your loaded configuration files:

# Extend a single file (extension is optional)
extends: some-other-file.yml

# Extend multiple files
extends:
- some-file
- another-file

Using with Rails

You can use this however you wish in Rails. This is the recommended implementation:

Add sting to your Gemfile:

gem 'sting'

Create an initializer:

# config/initializers/settings.rb
Settings = Sting
Settings << "#{Rails.root}/config/settings"
Settings << "#{Rails.root}/config/settings/#{Rails.env}"

Create four config files:

  • config/settings.yml
  • config/settings/development.yml
  • config/settings/production.yml
  • config/settings/test.yml

sting's People

Contributors

dannyben avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

daniel-barrows

sting's Issues

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.