Home > heavy_hash

heavy_hash

Heavy_hash is a project mainly written in Ruby, it's free.

A nested hash with payload for the nodes

h1. HeavyHash

HeavyHash is a nested Hash like object which provides a payload array per node. I use it for Websockets, to remember connections to a client (each called a channel). The connections are opened on a URL by a client.

@channels = HeavyHash.new
@channels.path(@connection1.path) << @connection1
@channels.path(@connection2.path) << @connection2

... and in a sinatra app:

post '*' do
  clients = CHANNELS.path(params[:splat].first).parents_content(true)
  clients.each do |connection|
    connection.send params[:message]
  end
end

There are two methods to access a node: @[key1][key2]@ and @path('/key1/key2)@. The latter is just a convenience method for the first.

Each node has three getters: @content@, @childrens_content@ and @parents_content@. @content@ return a nodes payload (added by @<<@), @childrens_content@ returns the sum all subnodes payload, @parents_content@ returns the sum all parent nodes payload. Both, @childrens_content@ and @parents_content@ take an optional @true@ parameter to make them include the payload of the current node.

There are two methods to change a HeavyHash tree: @<<@ and @remove()@. @<<@ adds a payload to a node and @remove@ (surprise) removes it again. When removing the last payload from a node the node itself gets deleted. Also all empty parent nodes are deleted.

h2. Installation

gem install heavy_hash

h2. Issues

Head right over to the "Issues tab":https://github.com/niko/heavy_hash/issues

I'm glad to help.

h2. Other projects

You might want to look at the "tree gem":https://rubygems.org/gems/tree

It's got more of the usual tree access and traversal methods and the payload is less specific. Access and deep construction is less convenient (IHMO).

h2. License

h3. (The MIT License)

Copyright (c) 2011 Niko Dittmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Previous:sample