diff --git a/README.md b/README.md index ac0f296..4fd3afb 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,10 @@ Create your "env" file. Name it whatever you want. Most common is `.env`. The file is created with key/value pairs separated by `=`. ```text +APP_NAME=my_app LUCKY_ENV=development DEV_PORT=3002 +DB_NAME=${APP_NAME}_${LUCKY_ENV} ``` ### Crystal code diff --git a/spec/parser_spec.cr b/spec/parser_spec.cr index e6d524e..eaa7d6a 100644 --- a/spec/parser_spec.cr +++ b/spec/parser_spec.cr @@ -60,6 +60,9 @@ describe LuckyEnv::Parser do data["SECRET_KEY_BASE"].should eq "j5I0VrpzT1Of7dhCA=" data["ASSET_HOST"].should eq "https://luckyframework.org" data["ENV_WITH_SPACE"].should eq "start_end" + data["APP_NAME"].should eq "my_app" + data["DB_NAME"].should eq "my_app_development" + data["LITERAL"].should eq "${NOT_EXISTS_ENV}" end end end diff --git a/spec/support/.env b/spec/support/.env index 39709e4..da1bdfc 100644 --- a/spec/support/.env +++ b/spec/support/.env @@ -3,4 +3,7 @@ LUCKY_ENV=development DEV_PORT=3500 SECRET_KEY_BASE=j5I0VrpzT1Of7dhCA= ASSET_HOST="https://luckyframework.org" -ENV_WITH_SPACE= start_end +ENV_WITH_SPACE= start_end +APP_NAME=my_app +DB_NAME=${APP_NAME}_${LUCKY_ENV} +LITERAL=${NOT_EXISTS_ENV} diff --git a/src/lucky_env/parser.cr b/src/lucky_env/parser.cr index f00979e..148f195 100644 --- a/src/lucky_env/parser.cr +++ b/src/lucky_env/parser.cr @@ -33,7 +33,19 @@ module LuckyEnv hash[key] = value end - hash + keys = hash.keys + + hash.transform_values do |value| + keys.each do |key| + if value =~ /\$\{#{key}\}/ + value = value.sub("${#{key}}", hash[key]) + else + value + end + end + + value + end else raise MissingFileError.new <<-ERROR The file #{file_path} could not be found.