Git Product home page Git Product logo

config_hash's Introduction

config_hash

A hash that is nice to use for configurations. It takes an existing hash in at construction time, and by default returns a Hash that:

  1. freezes all values recursively throughout the entire config
    • can be overridden with "freeze: false"
  2. ensures indifferent access to keys ('foo' and :foo are the same key)
  3. sets up '.' accessors for all values (given {foo: :bar}, can use config.foo)

This is done singularly at construction time. If you want, you can also assign processors via the :processors initialization argument, which will modify the value returned (both with . and []) via methods at access time.

Usage

# standard accessors
config = ConfigHash.new({foo: :bar})
config.foo # :bar
config['foo'] # :bar
config[:foo] # :bar
config.x # raises an error!

# with special prcoessors

# constantize special processor
class SomeClass; end
config = ConfigHash.new({klass_reference: '::SomeClass'}, constantize: true)
config.klass_reference # SomeClass (the class itself0, NOT '::SomeClass'

# sometimes, you want to post-process instead of pre-process. this slows down the
# config hash, but may be necessary, e:g:

config = ConfigHash.new({klass_reference: '::DefinedLaterClass'}, constantize: true, lazy_loading: true)
# config.klass_reference -- would at this point return the string, as DefinedLaterClass
# does not exist.
class DefinedLaterClass; end
config.klass_reference # now returns the class DefinedLaterClass

# custom processor
module Singleton
  def self.processor(value)
    value.is_a?(String) ? value + ' world' : value
  end
end
config = ConfigHash.new({foo: [{bar: 'hello'}]}, processors: [Singleton.method(:processor)])
config.foo # [{bar: 'hello'}]
config.foo[0].bar # 'hello world'

# by default, missing values raise an error:
config = ConfigHash.new({foo: :bar})
config.baz # raises an error!
config[:baz] # raises an error!

# if you want to have it return nil like a typical hash, pass raise_on_missing: false
config = ConfigHash.new({foo: :bar}, raise_on_missing: false)
config.baz # nil
config[:baz] # nil

config_hash's People

Contributors

kuraiou avatar

Stargazers

Daniel Rench avatar Juanito Fatas avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.