Giter Club home page Giter Club logo

configly's Introduction

Configly - Minimal Settings Library

Gem Version Build Status Maintainability

Configly is a lightweight ruby Hash object with dot notation access.

It is designed for loading and using YAML configuration files.


Installation

$ gem install configly

Usage

Initialization

Initialize a Configly object from Hash:

# Initialize from hash
require 'configly'
hash = {server: {host: 'localhost', port: 3000}}
configly = hash.to_configly

or by loading one or more YAML files:

# Initialize by merging in YAML files
config = Configly.new
config << 'spec/fixtures/settings.yml'
puts config.imported.settings.also
#=> work

You can append additional YAML files by using either #<< or #load. The '.yml' extension is optional.

In addition, you may load YAML files to nested keys:

# Loading nested YAMLs
config = Configly.new
config << 'spec/fixtures/settings'

p config.imported.settings
#=> {:also=>"work"}

config.nested.settings.load 'spec/fixtures/settings'

p config.nested.settings
#=> {:imported=>{:settings=>{:also=>"work"}}}

Configly objects inherit from Hash:

puts configly.is_a? Configly  #=> true
puts configly.is_a? Hash      #=> true

Dot notation read access

Read values using dot notation:

# Dot notation access
puts configly.server.host
#=> localhost

Reading nonexistent deep values will not raise an error:

# Deep dot notation access
p configly.some.deeply.nested_value
#=> {}

To check if a key exists, use ?

# Check if a key exists
p configly.some.deeply.nested_value?
#=> false

p configly.server.port?
#=> true

To get the value or nil if it does not exist, use !:

# Get value or nil
p configly.some.deeply.nested_value!
#=> nil

p configly.server.port!
#=> 3000

Dot notation write access

Writing values is just as easy:

# Dot notation write access
configly.production.server.port = 4000
puts configly.production.server.port
#=> 4000

Arrays with hashes as values, will also work (as the nested hashes will be coerced into Configly objects):

# Arrays of hashes
configly.servers = [
  { host: 'prod1.example.com', port: 3000 },
  { host: 'prod2.example.com', port: 4000 },
]

puts configly.servers.first.host
#=> prod1.example.com

puts configly.servers.first.is_a? Configly
#=> true

Array-like access

Configly allows read/write access using the usual array/hash syntax #[] using either a string a symbol key:

# Array access
puts configly.server.port     #=> 3000
puts configly.server[:port]   #=> 3000
puts configly.server['port']  #=> 3000

configly.server[:port] = 4000
puts configly.server.port     #=> 4000

Limitations

Due to the fact that Configly is inheriting from Hash, and using method_missing to allow dot notation access, your settings hashes cannot use keys that are defined as methods in the Hash object.

When this case is identified, a KeyError will be raised.

# Reserved keys
configly.api.key = '53cr3t'
#=> #<KeyError: Reserved key: key>

configly's People

Contributors

dannyben avatar

Stargazers

taw420 avatar

Watchers

James Cloos avatar  avatar  avatar

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.