Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from robyurkowski/b-129-dotenv-error-when-das…
…herized-application-name Add ApplicationName, fixing dasherized application names and prohibiting use of restricted words.
- Loading branch information
Showing
8 changed files
with
186 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
module Lotus | ||
# An application name. | ||
# | ||
# @since x.x.x | ||
class ApplicationName | ||
|
||
# A list of words that are prohibited from forming the application name | ||
# | ||
# @since x.x.x | ||
RESERVED_WORDS = %w(lotus).freeze | ||
|
||
|
||
# Initialize and check against reserved words | ||
# | ||
# An application name needs to be translated in quite a few ways: | ||
# First, it must be checked against a list of reserved words and rejected | ||
# if it is invalid. Secondly, assuming it is not invalid, it must be able | ||
# to be output roughly as given, but with the following changes: | ||
# | ||
# 1. downcased, | ||
# 2. with surrounding spaces removed, | ||
# 3. with internal whitespace rendered as underscores | ||
# 4. with underscores de-duplicated | ||
# | ||
# which is the default output. It must also be transformable into an | ||
# environment variable. | ||
# | ||
# @return [Lotus::ApplicationName] a new instance of the application name | ||
# | ||
# @since x.x.x | ||
def initialize(name) | ||
@name = sanitize(name) | ||
ensure_validity! | ||
end | ||
|
||
|
||
# Returns the cleaned application name. | ||
# | ||
# @example | ||
# ApplicationName.new("my-App ").to_s # => "my-app" | ||
# | ||
# @since x.x.x | ||
def to_s | ||
@name | ||
end | ||
|
||
|
||
# Returns the application name uppercased with non-alphanumeric characters | ||
# as underscores. | ||
# | ||
# @example | ||
# ApplicationName.new("my-app").to_env_s => "MY_APP" | ||
# | ||
# @since x.x.x | ||
def to_env_s | ||
@name.upcase.gsub(/\W/, '_') | ||
end | ||
|
||
|
||
# Returns true if a potential application name matches one of the reserved | ||
# words. | ||
# | ||
# @example | ||
# Lotus::ApplicationName.invalid?("lotus") # => true | ||
# | ||
# @since x.x.x | ||
def self.invalid?(name) | ||
RESERVED_WORDS.include?(name) | ||
end | ||
|
||
|
||
private | ||
|
||
# Raises RuntimeError with explanation if the provided name is invalid. | ||
# | ||
# @api private | ||
# @since x.x.x | ||
def ensure_validity! | ||
if self.class.invalid?(@name) | ||
raise RuntimeError, | ||
"application name must not be any one of the following: " + | ||
RESERVED_WORDS.join(", ") | ||
end | ||
end | ||
|
||
|
||
# Cleans a string to be a functioning application name. | ||
# | ||
# @api private | ||
# @since x.x.x | ||
def sanitize(name) | ||
name | ||
.downcase | ||
.strip | ||
.gsub(/\s/, '_') | ||
.gsub(/_{2,}/, '_') | ||
end | ||
end | ||
end |
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
2 changes: 1 addition & 1 deletion
2
lib/lotus/generators/application/container/config/.env.development.tt
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,2 +1,2 @@ | ||
# Define ENV variables for development environment | ||
<%= config[:app_name].upcase %>_DATABASE_URL="file:///db/<%= config[:app_name] %>_development" | ||
<%= config[:app_name].to_env_s %>_DATABASE_URL="file:///db/<%= config[:app_name] %>_development" |
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,2 +1,2 @@ | ||
# Define ENV variables for test environment | ||
<%= config[:app_name].upcase %>_DATABASE_URL="file:///db/<%= config[:app_name] %>_test" | ||
<%= config[:app_name].to_env_s %>_DATABASE_URL="file:///db/<%= config[:app_name] %>_test" |
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'test_helper' | ||
require 'lotus/application_name' | ||
|
||
describe Lotus::ApplicationName do | ||
|
||
describe "formats" do | ||
|
||
describe "#to_s" do | ||
it "renders downcased" do | ||
application_name = Lotus::ApplicationName.new("MY-APP") | ||
application_name.to_s.must_equal "my-app" | ||
end | ||
|
||
it "renders trimmed" do | ||
application_name = Lotus::ApplicationName.new(" my-app ") | ||
application_name.to_s.must_equal "my-app" | ||
end | ||
|
||
it "renders internal spaces as underscores" do | ||
application_name = Lotus::ApplicationName.new("my app") | ||
application_name.to_s.must_equal "my_app" | ||
end | ||
|
||
it "renders with underscores de-duplicated" do | ||
application_name = Lotus::ApplicationName.new("my _app") | ||
application_name.to_s.must_equal "my_app" | ||
end | ||
end | ||
|
||
|
||
describe "to_env_s" do | ||
it "renders uppercased with non-alphanumeric characters as underscores" do | ||
application_name = Lotus::ApplicationName.new("my-app") | ||
application_name.to_env_s.must_equal "MY_APP" | ||
end | ||
end | ||
end | ||
|
||
|
||
describe "reserved words" do | ||
it "prohibits 'lotus'" do | ||
-> { Lotus::ApplicationName.new("lotus") }.must_raise RuntimeError | ||
end | ||
end | ||
end |
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