Project

konfigyu

0.0
No commit activity in last 3 years
No release in over 3 years
Allows for customization of your YAML config file with basic requirements.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 10.0.2
~> 10.0
~> 3.8.0
~> 0.59.2
~> 1.29.1

Runtime

~> 1.3.0
~> 1.6
 Project Readme

Konfigyu

コンフィギュ

CircleCI branch GitHub GitHub issues Gem

When adding an application-specific YAML config file to your project, often you end up writing a bunch of boilerplate code to manage the file's location, reading in the contents, and managing required values.

This gem strives to make this an easier process to manage so you can start writing your application quicker. It relies on the sycl and syck gems for loading the yaml.

Installation

Add this line to your application's Gemfile:

gem 'konfigyu'

And then execute:

$ bundle

Or install it yourself as:

$ gem install konfigyu

Usage

Basic Usage

Create a YAML config file. For example, ~/konfigyu.yml

foo:
  bar: value
  baz: something-else
log:
  level: info

Then, in your code, you can instantiate a new Konfigyu::Config object pointing to that file:

require 'konfigyu'

config = Konfigyu::Config.new('~/konfigyu.yml')

You can access the config values directly, through data, or through array [] notation:

puts config.foo.baz       # Outputs "something-else"
puts config.data.foo.baz  # Outputs "something-else"
puts config['foo.baz']    # Also outputs "something-else"

There's a caveat with the dot notation that comes from the way methods are interpreted in ruby. If you have a config tree that is the same name as a reserved word in ruby, eg. class, you will have to access it through the array [] notation instead of the dot notation.

class:
  name: 'Computer Programming'
puts config.class     # Outputs: Konfigyu::Config
puts config['class']  # Outputs: "Computer Programming"

Required values

You can ensure that certain values are required, and optionally that they contain specific values by passing in a hash of options to Konfigyu:

require 'konfigyu'

config = Konfigyu::Config.new('~/konfigyu.yml' {
  required_fields: ['foo', 'foo.bar', 'log', 'log.level'],
  required_values: { 'log.level': %w[none fatal error warn info debug] }
})

Note that if you do not list a field in required_fields but include it in required_values, it will stil validate the contents of the field, but only if the field is non-empty.

If validation fails, a Konfigui::InvalidConfigException will be raised.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/bratta/konfigyu. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

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

Code of Conduct

Everyone interacting in the Konfigyu project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.