Code Monkey home page Code Monkey logo

dead_simple_conf's Introduction

This is my own implementation of a REALLY simple configuration library, and when I say simple I mean it ! Most of the library available out there are far too complicated.

I decided to extract this code in a proper gem when I got tired of copying it over and over in my projects.

Continuous integration (Build Status)

This gem is tested against these ruby by travis-ci.org:

  • mri 1.9.2
  • mri 1.8.7

What this gem provides

My requirements for a config parser are:

  • check that each line typed in the yaml file match a valid parameter I spent far too much time debugging application loading just because activerecord or any other gem did not warned me that a key was unknown (like "size" instead of "pool_size", you get the idea).
    What I expect is to have some return, taise an error, throw me a brick, do something damn !

  • have a useable syntax to read the values

  • something minimal which can be extended as needed (look at the source to really figure out how small this library is, there may even be more text in this README ! )

And honestly that's all, I don't want or need 100 different allowed formats with complex cases handled, so now that we are clear let's see how this thing works.

How it works

You have this yaml file:

logger:
  level: debug
  output: /tmp/file.log

app:
  data_path: /tmp
  
  server:
    port: 9000
    address: 127.0.0.1

Here is how you would use it:

require 'bundler/setup'
require 'dead_simple_conf'

module ConfigLoader
  class ServerConfig < DeadSimpleConf::ConfigBlock
    attr_accessor :port, :address
  end

  class AppConfig < DeadSimpleConf::ConfigBlock
    attr_accessor :data_path
    sub_section :server, ServerConfig
  
  end

  class LoggerConfig < DeadSimpleConf::ConfigBlock
    attr_accessor :level, :output
  end

  class MyConfig < DeadSimpleConf::ConfigBlock
    sub_section :app, AppConfig
    sub_section :logger, LoggerConfig  
  end
  
  def self.load(path)
    raw_data = YAML.load_file(path)
    MyConfig.new(raw_data)
  end
end

conf = ConfigLoader.load("conf.yml")
puts "conf.app.data_path    : #{conf.app.data_path}"
puts "conf.app.server.port  : #{conf.app.server.port}"

You can see both files in the examples folder.

Extending it

Since I am only using standard class with getter and setters you should be able to extend it without much trouble, one one the thing which comes in mind is to use activemodel validation to validate incoming entries.

dead_simple_conf's People

Contributors

schmurfy avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  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.