Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds environment boolean checks (#302)
Amber currently ships with 3 different environments: development, test and production. As a developer I would like to sometimes run code conditionally depending on the environment that the application is. ```crystal if Amber.env.development? # ... code here ... end ``` This PR addresses the issue by creating a Amber::Env module that contains 3 module methods: ```crystal Amber.env.development? Amber.env.test? Amber.env.production? Amber.env.integration? Amber.env.staging? Amber.env.sandbox? Amber.env.in? %w(development test) Amber.env.in? %i(development test) Amber.env.is? "test" Amber.env.is? :test ``` With the changes in this PR a developer should be able to call the appropriate method to check for the desired environment. Adds Additional Module methods `in?` and `is?` methods to check for inclusion of the environments Uses rails style for Amber environment checks
- Loading branch information
Showing
6 changed files
with
103 additions
and
6 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
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,59 @@ | ||
require "../../../spec_helper" | ||
|
||
describe Amber do | ||
{% for env in %w(development staging test sandbox production) %} | ||
describe ".{{env.id}}?" do | ||
it "returns true when the environment is {{env.id}}" do | ||
Amber::Settings.env = {{env}} | ||
Amber.env.{{env.id}}?.should be_truthy | ||
end | ||
|
||
it "returns false when the environment does not match" do | ||
Amber::Settings.env = "invalid environment" | ||
Amber.env.{{env.id}}?.should be_falsey | ||
end | ||
end | ||
{% end %} | ||
|
||
describe ".is?" do | ||
it "returns true when the environment matches the argument" do | ||
Amber::Settings.env = "staging" | ||
result = Amber.env.is? :staging | ||
|
||
result.should be_truthy | ||
end | ||
|
||
it "returns true when the environment matches the argument" do | ||
Amber::Settings.env = "invalid" | ||
result = Amber.env.is? :staging | ||
|
||
result.should be_falsey | ||
end | ||
end | ||
|
||
describe ".in?" do | ||
context "when settings environment is in list" do | ||
it "returns true" do | ||
Amber::Settings.env = "development" | ||
|
||
symbols_result = Amber.env.in? %i(development test production) | ||
strings_result = Amber.env.in? %w(development test production) | ||
|
||
symbols_result.should be_truthy | ||
strings_result.should be_truthy | ||
end | ||
end | ||
|
||
context "when settings environment is not in list" do | ||
it "returns false" do | ||
Amber::Settings.env = "invalid" | ||
|
||
symbols_result = Amber.env.in? %w(development test production) | ||
strings_result = Amber.env.in? %w(development test production) | ||
|
||
symbols_result.should be_falsey | ||
strings_result.should be_falsey | ||
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
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,34 @@ | ||
module Amber | ||
def self.env | ||
Env.new(Settings.env.not_nil!.downcase) | ||
end | ||
|
||
class Env | ||
def initialize(@env : String | Symbol) | ||
end | ||
|
||
def in?(environment_list : Array(String | Symbol)) | ||
(environment_list.map &.to_s.downcase).includes? @env | ||
end | ||
|
||
def is?(environment : String | Symbol) | ||
@env == environment.to_s.downcase | ||
end | ||
|
||
def to_s(io) | ||
io << @env | ||
end | ||
|
||
private def environments | ||
(Dir.entries(CONFIG_DIR).map &.downcase.tr(".yml", "") + ENVIRONMENTS).to_set | ||
end | ||
|
||
macro method_missing(call) | ||
environment = {{call.name.id.stringify.downcase}} | ||
if environment.chars.last == '?' | ||
environment = environment.downcase.tr("?", "") | ||
(@env == environment) | ||
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