-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ license: MIT | |
development_dependencies: | ||
ameba: | ||
github: crystal-ameba/ameba | ||
version: ~> 1.0.0 | ||
version: ~> 1.5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Habitat | ||
# :nodoc: | ||
class MissingSettingError < Exception | ||
def initialize(type, setting_name, example) | ||
example ||= "some_value" | ||
super <<-ERROR | ||
The '#{setting_name}' setting for #{type} was nil, but the setting is required. | ||
Try this... | ||
#{type}.configure do |settings| | ||
settings.#{setting_name} = #{example} | ||
end | ||
ERROR | ||
end | ||
end | ||
|
||
# :nodoc: | ||
class InvalidSettingFormatError < Exception | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class Habitat | ||
# :nodoc: | ||
module SettingsHelpers | ||
macro setting(decl, example = nil, validation = nil) | ||
{% if decl.var.stringify.ends_with?('?') %} | ||
{% decl.raise <<-ERROR | ||
You cannot define a setting ending with '?'. Found #{decl.var} defined in #{@type}. | ||
Habitat already has a predicate method #{decl.var} used when checking for missing settings. | ||
ERROR | ||
%} | ||
{% end %} | ||
{% HABITAT_SETTINGS << {decl: decl, example: example, validation: validation} %} | ||
end | ||
|
||
macro inherit_habitat_settings_from_superclass | ||
{% if @type.superclass && @type.superclass.type_vars.size == 0 && @type.superclass.constant(:HABITAT_SETTINGS) %} | ||
{% for decl in @type.superclass.constant(:HABITAT_SETTINGS) %} | ||
{% HABITAT_SETTINGS << decl %} | ||
{% end %} | ||
{% end %} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Habitat | ||
module TempConfig | ||
# Temporarily changes the configuration | ||
# | ||
# This method will change the configuration to the passed in value for the | ||
# duration of the block. When the block is finished running, Habitat will | ||
# then reset to the value before the block | ||
# | ||
# ``` | ||
# MyServer.configure do |settings| | ||
# settings.port = 80 | ||
# end | ||
# | ||
# MyServer.settings.port # 80 | ||
# | ||
# MyServer.temp_config(port: 3000) do | ||
# MyServer.settings.port # 3000 | ||
# end | ||
# | ||
# MyServer.settings.port # 80 | ||
# ``` | ||
# | ||
# This can be very helpful when writing specs and you need to temporarily | ||
# change a value | ||
macro temp_config(**settings_with_values) | ||
{% for setting_name, setting_value in settings_with_values %} | ||
original_{{ setting_name }} = {{ @type.name }}.settings.{{setting_name}} | ||
{{ @type.name }}.settings.{{ setting_name }} = {{ setting_value }} | ||
{% end %} | ||
|
||
{{ yield }} | ||
|
||
{% for setting_name, _unused in settings_with_values %} | ||
{{ @type.name }}.settings.{{ setting_name }} = original_{{ setting_name }} | ||
{% end %} | ||
end | ||
end | ||
end |