Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/svenfuchs/hashr
Simple Hash extension to make working with nested hashes (e.g. for configuration) easier and less error-prone.
https://github.com/svenfuchs/hashr
Last synced: 4 days ago
JSON representation
Simple Hash extension to make working with nested hashes (e.g. for configuration) easier and less error-prone.
- Host: GitHub
- URL: https://github.com/svenfuchs/hashr
- Owner: svenfuchs
- License: mit
- Created: 2011-07-23T16:29:11.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2017-04-24T16:24:51.000Z (over 7 years ago)
- Last Synced: 2024-12-15T20:15:15.925Z (11 days ago)
- Language: Ruby
- Homepage: http://github.com/svenfuchs/hashr
- Size: 56.6 KB
- Stars: 110
- Watchers: 4
- Forks: 14
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
[![Build Status](https://secure.travis-ci.org/svenfuchs/hashr.png)](http://travis-ci.org/svenfuchs/hashr)
# Hashr
Hashr is a very simple and tiny class which makes using nested hashes for
configuration (and other purposes) easier.It supports the following features:
* method read and write access
* automatic predicate (boolean, i.e. `?`) methods
* easy defaults
* indifferent (strings vs symbols) keys## Usage
Directly use Hashr instances like this:
```ruby
config = Hashr.new(foo: { bar: 'bar' })config.foo? # => true
config.foo # => { bar: 'bar' }config.foo.bar? # => true
config.foo.bar # => 'bar'config.foo.bar = 'bar'
config.foo.bar # => 'bar'config.foo.baz = 'baz'
config.foo.baz # => 'baz'
```Hash core methods are not available but assume you mean to look up keys with
the same name:```ruby
config = Hashr.new(count: 1, key: 'key')
config.count # => 1
config.key # => 'key'
```In order to check a hash stored on a certain key you can convert it to a Ruby
Hash:```ruby
config = Hashr.new(count: 1, key: 'key')
config.to_h.count # => 2
config.to_h.key # => raises ArgumentError: "wrong number of arguments (0 for 1)"
```Missing keys won't raise an exception but instead behave like Hash access:
```ruby
config = Hashr.new
config.foo? # => false
config.foo # => nil
```## Defaults
Defaults can be defined per class:
```ruby
class Config < Hashr
default boxes: { memory: '1024' }
endconfig = Config.new
config.boxes.memory # => 1024
```Or passed to the instance:
```ruby
data = {}
defaults = { boxes: { memory: '1024' } }config = Hashr.new(data, defaults)
config.boxes.memory # => 1024
```## Environment defaults
Hashr includes a simple module that makes it easy to overwrite configuration
defaults from environment variables:```ruby
class Config < Hashr
extend Hashr::Envself.env_namespace = 'foo'
default boxes: { memory: '1024' }
end
```Now when an environment variable is defined then it will overwrite the default:
```ruby
ENV['FOO_BOXES_MEMORY'] = '2048'
config = Config.new
config.boxes.memory # => '2048'
```## Other libraries
You also might want to check out OpenStruct and Hashie.
* [OpenStruct](http://ruby-doc.org/stdlib/libdoc/ostruct/rdoc/classes/OpenStruct.html) does less but comes as a Ruby standard library.
* [Hashie](https://github.com/intridea/hashie) has a bunch of support classes (like `Mash`, `Dash`, `Trash`) which all support different features that you might need.## License
[MIT License](https://github.com/svenfuchs/hashr/blob/master/MIT-LICENSE)