Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/3den/dot_hash
Access hash properties with the dot notation
https://github.com/3den/dot_hash
Last synced: about 12 hours ago
JSON representation
Access hash properties with the dot notation
- Host: GitHub
- URL: https://github.com/3den/dot_hash
- Owner: 3den
- License: mit
- Created: 2013-01-11T16:35:14.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2017-05-25T19:12:28.000Z (over 7 years ago)
- Last Synced: 2024-05-13T03:20:27.967Z (6 months ago)
- Language: Ruby
- Size: 669 KB
- Stars: 14
- Watchers: 5
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DotHash
[![Code Climate](https://codeclimate.com/github/3den/dot_hash.png)](https://codeclimate.com/github/3den/dot_hash) [![Build Status](https://travis-ci.org/3den/dot_hash.png?branch=master)](https://travis-ci.org/3den/dot_hash)A very efficient gem that lets you use hashes as object properties. It is almost as fast as a plain Hash
since it's complexity is also linear, `O(n) # where N is the number of nested parents of the given property`.## Installation
Add this line to your application's Gemfile:
gem 'dot_hash'
And then execute:
$ bundle
Or install it yourself as:
$ gem install dot_hash
## Usage
You can convert and hash to DotHash's properties.
```ruby
some_hash = {size: {height: 100, width: 500}, "color" => "red"}
properties = DotHash.load(some_hash)properties.size.height # returns 100, it is the same as some_hash[:size][:height]
properties.color # returns "red", it works with Strings and Symbol keys
properties[:color] # returns "red", can be used like a hash with string keys
properties["color"] # returns "red", can be used like a hash with symbol keys
```You can use DotHash::Settings to manage all configs of your app it can load yml, json with or without ERB code embeded.
```ruby
# App Settings
class Settings < DotHash::Settings
load(
'path/to/some/settings.json',
'path/to/settings-directory/',
{something: 'Some Value'}
)
end# Use the settings as a Singleton
Settings.something
Settings.other.stuff.from_yml_settings# Create a settings instance from some YML
swagger = Settings.new Rails.root.join('config', 'my-swagger.yml')
swagger.info.title # returns the title from swagger doc
```DotHash supports Rails and is very easy to manage fancy settings with it.
```ruby
class Settings < DotHash::Settings
load(
Rails.root.join('config', 'settings.yml'), # loads config/settings.yml
Rails.root.join('package.json'), # loads package.json
*Dir(Rails.root.join('config', 'settings', '*.yml')), # loads all config/settings/*.yml but dont go to nested directories
Rails.root.join('config', 'settings', Rails.env), # loads all files on config/settings//
Rails.root.join('config', 'settings.local.yml') # loads config/settings.local.yml
)
end
```Check the tests for more details.
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/3den/dot_hash/trend.png)](https://bitdeli.com/free "Bitdeli Badge")