ASCM
ASCM stands to A Small Configuration Manager. It simply reads
configuration keys available in your Environment (ENV
) and defines class
methods in a given class.
Installation
Add this line to your application's Gemfile:
gem 'ascm'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ascm
Usage
To use ASCM, define a class and inherit ASCM::Reader
:
class Config < ASCM::Reader
def_setting :database_url
def_setting :redis_url
def_setting :max_connections, default: 20, map: -> i { i.to_i }
def_setting :is_debug, default: false, map: -> v { ['yes', 'true', '1'].include? v }
end
def_setting
instructs the Reader
to look for a environment key with the
provided name. For instance, our def_setting :is_debug
will look for a
IS_DEBUG
key in the current application environment, and will define a few
class methods in our Config
class:
is_debug
: Returns the found IS_DEBUG
value, or nil
, if none is
found (and no default
is provided), passing any found value through the
provided map
lambdahas_is_debug?
returns true
as long as is_debug
is not nil
,
returning false
otherwise.
Protip: ASCM defines an extra is_debug?
(note the question mark) when a
variable begins has is_
as prefix.
def_setting
also support a few modifiers:
map
expects a lambda (or Proc
), passing the found value as the first (and
only) parameter of the provided lambda. In case the lambda throws an Error,
ASCM automatically falls back to the default
provided value.default
defines a default value in case the provided key cannot be found.
Defaults to nil
. Notice: map
lambdas are not called when a default
value is used.
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
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/victorgama/ascm. 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.
Code of Conduct
Everyone interacting in the ASCM project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
License
The MIT License (MIT)
Copyright (c) 2018 Victor Gama
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.