Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uses rails style for Amber environment checks
- Loading branch information
Showing
2 changed files
with
31 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |