habitat

Easily configure settings for Crystal projects

Habitat

Easily configure settings for Crystal projects

Installation

Add this to your application's shard.yml:

dependencies:
  habitat:
    github: luckyframework/habitat

Usage

require "habitat"
class MyServer
  Habitat.create do
    setting port : Int32
    setting debug_errors : Bool = true

    # Optionally add examples to settings that appear in error messages
    # when the value is not set.
    #
    # Use `String#dump` when you want the example to be wrapped in quotes
    setting host : String, example: "127.0.0.1".dump
    setting logger : Logger, example: "Logger.new(STDOUT)"
  end

  # Access them like this
  def start
    start_server_on port: settings.port
  end
end

MyServer.configure do |settings|
  settings.port = 8080
end

# At the very end of your program use this
# It will raise if you forgot to set any settings
Habitat.raise_if_missing_settings!

Settings can also be accessed from outside the class:

port = MyServer.settings.port
puts "The server is starting on port #{port}"

Contributing

  1. Fork it ( https://github.com/luckyframework/habitat/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

Github statistic:
  • 38
  • 4
  • 2
  • 6
  • 2 months ago

License:

MIT License

Links: