0.0
No commit activity in last 3 years
No release in over 3 years
Handles configuring your gems.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
>= 0
 Project Readme

settingable Build Status

Install

$ gem install settingable

Description

A Settings module for your application. Its job is to make handling user-definable settings easy for you, so you can focus on the more important parts of your library. The main component of it is the Settingable::Settings module. Just include that in a class, and you're good to go.

module MyLibrary
  class Settings
    include Settingable::Settings
end

The Settings module provides a few methods. First, it defines the .instance class method. This returns a single instance upon repeated invocation, like the Singleton module. Then, it defines the .configure method. This is used to (ahem) configure the settings. It both yields itself and runs it in its context, so you may configure it however you like. It also forwards the methods .[], .[]=, and .fetch on to the actual instance itself as well.

The instance forwards the #[], #[]=, #fetch, and #key? method on to the Settingable::Hash powering the settings (see the documentation for more). The instance can also accept any other methods. If the method ends in an equal sign (i.e. a setter method), it is forwarded to #[]=; otherwise, it is forwarded to #[].

Here's a few examples.

MyLibrary::Settings.configure do |config|
  # These two are the same.
  config.value = 2
  config[:value] = 2
end

# These all are the same, and return the same value.
MyLibrary::Settings.value
MyLibrary::Settings[:value]
MyLibrary::Settings["value"]
MyLibrary::Settings.instance.value
MyLibrary::Settings.instance[:value]
MyLibrary::Settings.instance["value"]

If you attempt to access a setting value that isn't defined, even if you use the regular accessor (#[]), a KeyError will be raised. So, the module provides a .default_settings method, for you to provide default values for the settings.

module MyLibrary
  class Settings
    include Settingable::Settings

    default_settings foo: "bar"
  end
end

Now, check it out.

MyLibrary::Settings[:foo] # => "bar"
MyLibrary::Settings[:bar] # ! KeyError