Skip to content

Commit

Permalink
Uses rails style for Amber environment checks
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasjpr committed Oct 13, 2017
1 parent 2624d5f commit 2b2ca30
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
20 changes: 10 additions & 10 deletions spec/amber/server/env_spec.cr
@@ -1,31 +1,31 @@
require "../../../spec_helper"

describe Amber::Env do
{% for env in Amber::Env::ENVIRONMENTS %}
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
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
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 = 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 = Amber.env.is? :staging

result.should be_falsey
end
Expand All @@ -36,8 +36,8 @@ describe Amber::Env 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 = 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
Expand All @@ -48,8 +48,8 @@ describe Amber::Env 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 = 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
Expand Down
35 changes: 21 additions & 14 deletions src/amber/server/env.cr
@@ -1,27 +1,34 @@
module Amber
module Env
ENVIRONMENTS = %w(development test production)
def self.env
Env.new(Settings.env.not_nil!.downcase)
end

class Env
def initialize(@env : String | Symbol)
end

def self.in?(environments : Array(String | Symbol))
(ENVIRONMENTS.map &.to_s.downcase).includes? current
def in?(environment_list : Array(String | Symbol))
(environment_list.map &.to_s.downcase).includes? @env
end

def self.is?(environment : String | Symbol)
current == environment.to_s.downcase
def is?(environment : String | Symbol)
@env == environment.to_s.downcase
end

def self.current
Settings.env.not_nil!.downcase
def to_s(io)
io << @env
end

def self.environments
ENVIRONMENTS
private def environments
(Dir.entries(CONFIG_DIR).map &.downcase.tr(".yml", "") + ENVIRONMENTS).to_set
end

{% for env in ENVIRONMENTS %}
def self.{{env.id}}?
current == {{env.id.stringify.downcase}}
macro method_missing(call)
environment = {{call.name.id.stringify.downcase}}
if environment.chars.last == '?'
environment = environment.downcase.tr("?", "")
(@env == environment)
end
end
{% end %}
end
end

0 comments on commit 2b2ca30

Please sign in to comment.