New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds environment boolean checks #302
Conversation
8271c32
to
f515cf8
Compare
Could we add it directly to Amber like it is in Rails. So |
I considered |
f515cf8
to
55413d4
Compare
I agree we shouldn't mimic or copy rails in a lot of things, but in this case I do like the way rails does it. Rails.env automatically calls to_s. This is the same for every instance in crystal and ruby. You might be right though. If you prefer it this way I'll approve it. |
@eliasjpr looks nice 👍 What about user-defined environments? |
@eliasjpr The implementation looks good 👍. FYI there is a test failing though. |
@veelenga really like the |
Another use case might be to check for the inclusion in a list of environments.
which is more or less consistent with |
I have added the It would be nice to populate the I attempted to approach the problem with I'm going to look at the failing specs. |
b482070
to
10ba651
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My only concerns here are that Amber::Env.is?("development")
doesn't seem like much of an improvement over ENV["AMBER_ENV"] == "development"
which already works. It might seem cleaner this way, but could we just create methods which return the existing VAR instead of storing the value multiple times?
src/amber/server/env.cr
Outdated
@@ -0,0 +1,27 @@ | |||
module Amber | |||
module Env | |||
ENVIRONMENTS = %w(development test production) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These could be dynamically defined at compile time by looking at file names in ./config/environments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have an example on how to do this? I got stuck...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. Give me a second.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 2 ways to do this.
Eval
This way requires less files but isn't quite as fast for compile time.
ENVIRONMENTS = {{ system("crystal eval \'puts Dir.glob(\"./config/environments/**\").map(&.split(/[\\/\\.]/)[-2]?)\'") }}
New File using Macro Run
This way is a bit faster as the compile can cache the compiled environment_names.cr
# environment_names.cr
puts Dir.glob("./config/environments/**").map(&.split(/[\/\.]/)[-2]?)
Then call ENVIRONMENTS = {{run("./path/to/environment_names.cr")}}
in env.cr.
src/amber/server/env.cr
Outdated
end | ||
|
||
def self.current | ||
Settings.env.not_nil!.downcase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Store env here is just duplicating values already stored in ENV["AMBER_ENV"]. Is this necessary? I would rather current
just wrap ENV["AMBER_ENV"]
and return it's values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would trust the Settings.env more than the ENV["AMBER_ENV"] since the latter can be modified outside of the APP after it has been started. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ENV["AMBER_ENV"] is defined by the env var at runtime and can't be changed outside of the app afterwards. Since the env var is where this value originates I forsee some possible issues where they could get out of sync and cause confusion if they're not pointing the same value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be fine using Settings.env
since its pointing to ENV["AMBER_ENV"]
. See https://github.com/amberframework/amber/pull/302/files#diff-b8648cef4f24d74b1b3b0cee94f1c9deR15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I'm ok with that then.
10ba651
to
5d78679
Compare
Updates. @elorest was right by using the Rails way it turned out to be very clean and feature rich kudos @elorest 🎉 This now supports the following. 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 |
dae7954
to
96e7b9b
Compare
@@ -0,0 +1,47 @@ | |||
require "../../../spec_helper" | |||
|
|||
describe Amber::Server do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of moving this under scripts. Would it be possible to move it and maintain git file history though instead of adding a new file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can undo the commit, and you can go ahead and move it in a separate PR. WDYT?
2b2ca30
to
9d502a4
Compare
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
9d502a4
to
3058844
Compare
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
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
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.
This PR addresses the issue by creating a Amber::Env module that
contains 3 module methods:
With the changes in this PR a developer should be able to call the
appropriate method to check for the desired environment.