Project

nac

0.0
No commit activity in last 3 years
No release in over 3 years
NAC stands for "not another config"
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 2.15
~> 0.7.1
~> 5.0
~> 0.12.2
~> 10.0
~> 0.17.0
 Project Readme

Nac

A very simple yet flexible configuration writer/reader library.

CircleCI Maintainability

Installation

Add this line to your application's Gemfile:

gem 'nac'

And then execute:

$ bundle

Or install it yourself as:

$ gem install nac

How it works

The source config file and its path will be created if they don't already exist. The data is cached when the file is loaded and after a new value is saved.

Usage

Basic

This simple example shows how to load, write, and read.

require 'nac'

# load or create a new config
config = Nac::Config.new('my_config')

# set values
config.set('name', 'Chris')
config.set('color', 'Blue')

# get a single value
puts config.get('name')

# get all data
puts config.get
#=> { name: 'Chris', color: 'Blue' }

Nesting Keys

It's possible to read and write nested data by passing an array to the set and get methods.

require 'nac'

config = Nac::Config.new('my_config')

config.set(%[user name first], 'Homer')
config.set(%[user name last], 'Simpson')

puts config.get('user')
#=> { name: { first: 'Homer', last: 'Simpson' }}

puts config.get(%[user name])
#=> { first: 'Homer', last: 'Simpson' }

puts config.get(%[user name last])
#=> { last: 'Simpson' }

# nil is returned if the key doesn't exist
puts config.get(%[user name middle])
#=> nil

# Or you can specify the return value if the key doesn't exist
puts config.get(%[user name middle], 'J')
#=> 'J'

# Symbol arrays work too!

config.set(%i{look ma no}, 'hands')
puts config.get(:look)
#=> { ma: { no: 'hands' } }

Options

  • template: Optional value that will be converted to yaml and written to the config file. Default is {}.
  • init!: When true, force the configuration file to be rewritten. Default is false.

Note: One doesn't need to specify init! when using template. The template will not be written if the configuration file exists. However, using init! will re-initialize the config file and the template will be written. Similarly, template is not required when using init!.

Usage

requre 'nac'

options = {
  template: { some: { default: [:options] } },
  init!: true
}

config = Nac::Config.new('my_config', options)

puts config.get
#=> { some: { default: [:options] } }

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/chris-roerig/nac.

License

The gem is available as open source under the terms of the MIT License.