https://github.com/mickey/black-hole-struct
:door: BlackHoleStruct is a data structure similar to an OpenStruct allowing autovivification
https://github.com/mickey/black-hole-struct
data-structure ruby
Last synced: about 1 year ago
JSON representation
:door: BlackHoleStruct is a data structure similar to an OpenStruct allowing autovivification
- Host: GitHub
- URL: https://github.com/mickey/black-hole-struct
- Owner: mickey
- Created: 2017-01-01T23:36:52.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-03T22:41:02.000Z (over 9 years ago)
- Last Synced: 2025-04-20T06:02:57.102Z (about 1 year ago)
- Topics: data-structure, ruby
- Language: Ruby
- Homepage:
- Size: 9.77 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://circleci.com/gh/mickey/black-hole-struct/tree/master)
# BlackHoleStruct
**BlackHoleStruct** is a data structure similar to an `OpenStruct` that allows:
- infinite chaining of attributes or [autovivification](https://en.wikipedia.org/wiki/Autovivification)
- deep merging of BlackHoleStruct/Hash

## Installation
Add it to your Gemfile:
```ruby
gem "black_hole_struct"
```
Or install the gem manually:
```sh
$ gem install black_hole_struct
```
## Basic Usage
```ruby
require "black_hole_struct"
config = BlackHoleStruct.new
config.dashboard.theme = "white"
config.dashboard.time.from = "now-1h"
config.dashboard.time.to = "now"
puts config.dashboard.theme # "white"
puts config.dashboard.time # #
puts config.dashboard.time.from # "now-1h"
config[:connection][:host] = "localhost"
config[:connection][:port] = 3000
puts config.to_h
# {
# connection: {
# host: "localhost",
# port: 3000
# }
# dashboard: {
# theme: "white",
# time: {
# from: "now-1h",
# to: "now"
# }
# }
# }
config = BlackHoleStruct.new(theme: "white", connection: {port: 3000})
config.deep_merge!(connection: {host: 'localhost'})
puts config.to_h
# {
# connection: {
# host: "localhost",
# port: 3000
# }
# theme: "white"
# }
```
## Is it any good
[Yes](https://news.ycombinator.com/item?id=3067434)
## Advanced usage
Check the [documentation](http://www.rubydoc.info/github/mickey/black-hole-struct/master/BlackHoleStruct).