Skip to content

Commit

Permalink
Adds environment boolean checks
Browse files Browse the repository at this point in the history
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:

Amber::Env.development?
Amber::Env.test?
Amber::Env.production?

With the changes in this PR a developer should be able to call the
appropriate method to check for the desired environment.
  • Loading branch information
eliasjpr committed Oct 12, 2017
1 parent 5bc546a commit f515cf8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
17 changes: 17 additions & 0 deletions spec/amber/server/env_spec.cr
@@ -0,0 +1,17 @@
require "../../../spec_helper"

describe Amber::Env do
{% for env in Amber::Env::ENVIRONMENTS %}
describe ".{{env.id}}?" do
it "returns true when the environment is {{env.id}}" do
Amber::Settings.env = {{env}}
Amber::Env.{{env.id}}?.should eq true
end

it "returns false when the environment does not match" do
Amber::Settings.env = "invalid environment"
Amber::Env.{{env.id}}?.should eq false
end
end
{% end %}
end
9 changes: 9 additions & 0 deletions src/amber/server/env.cr
@@ -0,0 +1,9 @@
module Amber
module ENV
{% for env in Settings::ENVIRONMENTS %}
def self.{{env.id}}?
Settings.env.not_nil!.downcase == {{env.downcase.id.stringify}}
end
{% end %}
end
end
3 changes: 2 additions & 1 deletion src/amber/server/settings.cr
Expand Up @@ -23,7 +23,8 @@ module Amber
class_property redis_url = ""
class_property session : Hash(Symbol, Symbol | String | Int32)

# loads settings from environment yaml
# Loads environment yml settings from the current AMBER_ENV environment variable
# and defaults to development environment
{{ run("./environment.cr") }}
end
end

0 comments on commit f515cf8

Please sign in to comment.