https://github.com/teliosdev/settingable
https://github.com/teliosdev/settingable
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/teliosdev/settingable
- Owner: teliosdev
- License: mit
- Created: 2015-06-26T00:34:20.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-08-04T06:50:51.000Z (over 10 years ago)
- Last Synced: 2025-06-03T20:09:26.349Z (7 months ago)
- Language: Ruby
- Size: 148 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# settingable [](https://travis-ci.org/medcat/settingable)
* [Homepage](https://rubygems.org/gems/settingable)
* [Documentation](http://rubydoc.info/gems/settingable/frames)
* [Email](mailto:me@medcat.me)
## 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.
```Ruby
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.
```Ruby
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.
```Ruby
module MyLibrary
class Settings
include Settingable::Settings
default_settings foo: "bar"
end
end
```
Now, check it out.
```Ruby
MyLibrary::Settings[:foo] # => "bar"
MyLibrary::Settings[:bar] # ! KeyError
```