README.md in qonfig-0.0.0 vs README.md in qonfig-0.1.0

- old
+ new

@@ -1,43 +1,237 @@ -# Qonfig +# Qonfig &middot; [![Gem Version](https://badge.fury.io/rb/qonfig.svg)](https://badge.fury.io/rb/qonfig) [![Build Status](https://travis-ci.org/0exp/qonfig.svg?branch=master)](https://travis-ci.org/0exp/qonfig) [![Coverage Status](https://coveralls.io/repos/github/0exp/qonfig/badge.svg)](https://coveralls.io/github/0exp/qonfig) -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/qonfig`. To experiment with that code, run `bin/console` for an interactive prompt. +Config. Defined as a class. Used as an instance. Support for inheritance and composition. +Lazy instantiation. Command-style DSL. Extremely simple to define. Extremely simple to use. That's all. -TODO: Delete this and the text above, and describe your gem - ## Installation -Add this line to your application's Gemfile: - ```ruby gem 'qonfig' ``` -And then execute: +```shell +$ bundle install +# --- or --- +gem install 'qonfig' +``` - $ bundle +```ruby +require 'qonfig' +``` -Or install it yourself as: +## Usage - $ gem install qonfig +- [Definition and Access](#definition-and-access) +- [Configuration](#configuration) +- [Inheritance](#inheritance) +- [Composition](#composition) +- [Hash representation](#hash-representation) +- [State freeze](#state-freeze) -## Usage +--- -TODO: Write usage instructions here +### Definition and Access -## Development +```ruby +class Config < Qonfig::DataSet + # nil by default + setting :project_id -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. + # nested setting + setting :vendor_api do + setting :host, 'app.service.com' + setting :port, 12345 + end -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). + setting :enable_graphql, false -## Contributing + # nested setting reopening + setting :vendor_api do + setting :user, 'test_user' + setting :password, 'test_password' + end +end -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/qonfig. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. +config = Config.new +config.settings.project_id # => nil +config.settings.vendor_api.host # => 'api.service.com' +config.settings.vendor_api.port # => 12345 +config.settings.vendor_api.user # => 'test_user' +config.settings.vendor_api.password # => 'test_password' +config.settings.enable_graphql # => false + +config.settings[:project_id] # => nil +config.settings[:vendor_api][:host] # => 'api.service.com' +config.settings[:vendor_api][:port] # => 12345 +config.settings[:vendor_api][:user] # => 'test_user' +config.settings[:vendor_api][:password] # => 'test_password' +config.settings[:enable_graphql] # => false +``` + +--- + +### Configuration + +```ruby +class Config < Qonfig::DataSet + setting :testing do + setting :engine, :rspec + setting :parallel, true + end + + setting :geo_api do + setting :provider, :google_maps + end + + setting :enable_middlewares, false +end + +config = Config.new + +# configure via block +config.configure do |conf| + conf.enable_middlewares = true + conf.geo_api.provider = :yandex_maps + conf.testing.engine = :mini_test +end + +# configure via settings object (by option name) +config.settings.enable_middlewares = false +config.settings.geo_api.provider = :apple_maps +config.settings.testing.engine = :ultra_test + +# configure via settings object (by setting key) +config.settings[:enable_middlewares] = true +config.settings[:geo_api][:provider] = :rambler_maps +config.settings[:testing][:engine] = :mega_test +``` + +--- + +### Inheritance + +```ruby +class CommonConfig < Qonfig::DataSet + setting :uploader, :fog +end + +class ProjectConfig < CommonConfig + setting :auth_provider, :github +end + +project_config = ProjectConfig.new + +# inherited setting +project_config.settings.uploader # => :fog + +# own setting +project_config.settings.auth_provider # => :github +``` + +--- + +### Composition + +```ruby +class SharedConfig < Qonfig::DataSet + setting :logger, Logger.new +end + +class ServerConfig < Qonfig::DataSet + setting :port, 12345 + setting :address, '0.0.0.0' +end + +class DatabaseConfig < Qonfig::DataSet + setting :user, 'test' + setting :password, 'testpaswd' +end + +class ProjectConfig < Qonfig::DataSet + compose SharedConfig + + setting :server do + compose ServerConfig + end + + setting :db do + compose DatabaseConfig + end +end + +project_config = ProjectConfig.new + +# fields from SharedConfig +project_config.settings.logger # => #<Logger:0x66f57048> + +# fields from ServerConfig +project_config.settings.server.port # => 12345 +project_config.settings.server.address # => '0.0.0.0' + +# fields from DatabaseConfig +project_config.settings.db.user # => 'test' +project_config.settings.db.password # => 'testpaswd' +``` + +--- + +### Hash representation + +```ruby +class Config < Qonfig::DataSet + setting :serializers do + setting :json do + setting :engine, :ok + end + + setting :hash do + setting :engine, :native + end + end + + setting :adapter do + setting :default: :memory_sync + end + + setting :logger, Logger.new(STDOUT) +end + +Config.new.to_h + +{ + serializers: { + json: { engine: :ok }, + hash: { engine: :native }, + }, + adapter: { default: :memory_sync }, + logger: #<Logger:0x4b0d79fc> +} +``` + +--- + +### State freeze + +```ruby +class Config < Qonfig::DataSet + setting :logger, Logger.new(STDOUT) + setting :worker, :sidekiq +end + +config = Config.new +config.freeze! + +config.settings.logger = Logger.new(StringIO.new) # => Qonfig::FrozenSettingsError +config.settings.worker = :que # => Qonfig::FrozenSettingsError +``` + +--- + ## License -The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). +Released under MIT License. -## Code of Conduct +## Authors -Everyone interacting in the Qonfig project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/qonfig/blob/master/CODE_OF_CONDUCT.md). +Rustam Ibragimov.