Skip to content

Dynamically load development or test default values #266

@laserlemon

Description

@laserlemon

Using a Rails app as an example, a developer may want to load certain default values when Rails.env.development? and others when running the test suite and Rails.env.test?. Both of these cases should use the same Envfile. So what's the most intuitive way to load different default values based on the environment, keeping in mind that Figaro 2.0 is dropping the explicit Rails dependency?

Some initial thoughts:

# Envfile
defaults ".env.yml", key: -> { Rails.env }
string :foo
# .env.yml
development:
  foo: bar
test:
  foo: baz

# Envfile
defaults ".env.yml"
defaults ".env.test.yml", if: -> { Rails.env.test? }
string :foo
# .env.yml
foo: bar
# .env.test.yml
foo: baz

I'd like to be explicit about what's being loaded and avoid hidden conventions. Think about Bundler and how you source "https://rubygems.org" at the top of every Gemfile. The Bundler team could have made that the default source since it is 95% of the time, but they opted for clarity.

I'd love to get others' thoughts on these two ideas and suggestions for more ideas. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions