• Stars
    star
    83
  • Rank 392,854 (Top 8 %)
  • Language
    Crystal
  • License
    MIT License
  • Created over 7 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easily configure settings for Crystal projects

Habitat

API Documentation Website

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)"

    # If you need the value to match a specific format, you can create
    # your own validation.
    setting protocol : String, validation: :validate_protocol
  end

  # Read more on validations below
  def self.validate_protocol(value : String)
    value.match(/^http(?:s)*:$/) || Habitat.raise_validation_error("The protocol must be `http:` or `https:`.")
  end

  # Access them with the `settings` method like this.
  def start
    start_server_on port: settings.port
  end
end

# Configure your settings
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}"

Setting validations

The validation option takes a Symbol which matches a class method that will run your custom validation. This can be useful if your setting needs to be in a specific format like maybe a 4 digit code that can start with a 0.

class Secret
  Habitat.create do
    setting code : String, validation: :validate_code
  end

  # The validation method will take an argument of the same type.
  # If your setting is `Int32`, then this argument will also be `Int32`.
  #
  # Use any method of validation you'd like here. (i.e. regex, other custom methods, etc...)
  # If your validation fails, you can call `Habitat.raise_validation_error` with your custom error
  # message
  def self.validate_code(value : String)
    value.match(/^\d{4}$/) || Habitat.raise_validation_error("Be sure the code is only 4 digits")
  end
end

Secret.configure do |settings|

  # Even though the code is the correct type, this will still
  # raise an error for us.
  settings.code = "ABCD"

  # This value will pass our validation
  settings.code = "0123"
end

Temp Config

There are some cases in which you may want to temporarily change a setting value. (i.e. specs, one off jobs, etc...)

Habitat comes with a built-in method temp_config that allows you to do this:

class Server
  Habitat.create do
    setting hostname : String
  end
end

Server.configure do |settings|
  settings.hostname = "localhost"
end

Server.settings.hostname #=> "localhost"

Server.temp_config(hostname: "fancyhost.com") do
  # This seting affects the value globally while inside this block
  Server.settings.hostname #=> "fancyhost.com"
end

# Once the block exits, the original value is returned
Server.settings.hostname #=> "localhost"

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

More Repositories

1

lucky

A full-featured Crystal web framework that catches bugs for you, runs incredibly fast, and helps you write code that lasts.
Crystal
2,599
star
2

avram

A Crystal database wrapper for reading, writing, and migrating Postgres databases.
Crystal
164
star
3

lucky_cli

A Crystal command-line tool for generating new Lucky Web Applications.
Crystal
87
star
4

carbon

Email library for Crystal. Testable, adapter-based, and catches bugs for you. Comes with an adapter for SendGrid.
Crystal
84
star
5

lucky_flow

Automated browser tests for web applications. Similar to Ruby's Capybara.
Crystal
51
star
6

lucky_record

Renamed to Avram ->> https://github.com/luckyframework/avram
Crystal
48
star
7

website

The Lucky website
Crystal
37
star
8

lucky_router

The router for the Lucky framework, can be used outside Lucky
Crystal
23
star
9

dexter

A logger with maximum customizability and surgical precision
Crystal
21
star
10

breeze

A development dashboard for Lucky Apps
Crystal
19
star
11

lucky_migrator

LuckyMigrator has been merged into LuckyRecord
Crystal
16
star
12

wordsmith

Handles pluralization, ordinalizing words, etc.
Crystal
14
star
13

authentic

An authentication library for Lucky projects
Crystal
14
star
14

pulsar

Pubsub and Instrumentation for Crystal
Crystal
14
star
15

avram_slugify

AvramSlugify generates slugs for database columns. These slugs can be used for creating nice looking URLs, permalinks, invite codes, etc.
Crystal
13
star
16

website-old

The old website for Lucky (archived)
HTML
11
star
17

carbon_smtp_adapter

An smtp adapter for the carbon email library
Crystal
10
star
18

html2lucky

Convert regular HTML into Lucky's Crystal DSL for HTML.
Crystal
10
star
19

lucky_inflector

This project has been moved to ->
Crystal
8
star
20

lucky_task

Library for creating command line tasks for the Lucky CLI
Crystal
7
star
21

lucky_cache

Caching library for the Lucky Framework
Crystal
5
star
22

lucky_env

An environment variable manager
Crystal
5
star
23

lucky_sec_tester

Crystal
4
star
24

homebrew-lucky

Ruby
3
star
25

teeplate

Fork of mosop/teeplate for generating files
Crystal
3
star
26

lucky_template

A simple, yet versatile, library for creating file and folder structures as code templates.
Crystal
3
star
27

lucky_hxml

Similar to LuckyHTML, but for Hyperview (https://hyperview.org/).
Crystal
2
star
28

upgrade-diffs

Diffs for easier upgrading of Lucky versions
Crystal
1
star
29

carbon_sendgrid_adapter

Crystal
1
star