0.0
No commit activity in last 3 years
No release in over 3 years
Scoped Hash provides scoped access to hash structure
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 0
 Project Readme

Scoped hash library

It provides scoped view to a hash (mash) data structure. When the requested scope does not exist, it would fall back to the non-scope key.

require 'scoped_hash'
smash = ScopedHash.new( 'tax': { min: 20, max: 40},
    'tax(democratic)': { min: 30, max: 50},
    'tax(republican)': { min: 10, max: 30})

smash.scope('democratic')[:tax][:min] == 30
smash.scope('republican')[:tax][:min] == 10
smash.scope('green')[:tax][:min] == 20

The best practice of this library is to create a YAML configuration file with scope annotation, for example the "config.yaml" could be:

tax:
  min: 20
  max: 40
  max(green): 60
tax(democratic):
  min: 30
  max: 50
tax(republican):
  min: 10
  max: 30

And the scoped access to the configuration could be as following:

require 'yaml'
require 'scoped_hash'

config = ScopedHash.new(YAML.load_file('config.yml'))

config.scope('democratic')[:tax][:min] == 30
config.scope('republican')[:tax][:min] == 10
config.scope('green')[:tax][:min] == 20

config[:tax][:max] == 40
config.scope('green')[:tax][:max] == 60

Installation

Simply use gem to install this library:

gem install scoped_hash