Class: Hanami::ApplicationName

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/application_name.rb

Overview

An application name.

Since:

  • 0.2.1

Constant Summary collapse

RESERVED_WORDS =

A list of words that are prohibited from forming the application name

Since:

  • 0.2.1

%w(hanami).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Hanami::ApplicationName

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.

Since:

  • 0.2.1



32
33
34
35
# File 'lib/hanami/application_name.rb', line 32

def initialize(name)
  @name = sanitize(name.to_s)
  ensure_validity!
end

Class Method Details

.invalid?(name) ⇒ TrueClass, FalseClass

Returns true if a potential application name matches one of the reserved words.

Examples:

Hanami::ApplicationName.invalid?("hanami") # => true

Parameters:

  • name (String)

    the application name

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.2.1



76
77
78
# File 'lib/hanami/application_name.rb', line 76

def self.invalid?(name)
  RESERVED_WORDS.include?(name)
end

Instance Method Details

#to_env_sString

Returns the application name uppercased with non-alphanumeric characters as underscores.

Examples:

ApplicationName.new("my-app").to_env_s => "MY_APP"

Returns:

  • (String)

    the upcased name

Since:

  • 0.2.1



62
63
64
# File 'lib/hanami/application_name.rb', line 62

def to_env_s
  @name.upcase.gsub(/\W/, '_')
end

#to_sString Also known as: to_str

Returns the cleaned application name.

Examples:

ApplicationName.new("my-App ").to_s # => "my_app"

Returns:

  • (String)

    the sanitized name

Since:

  • 0.2.1



45
46
47
# File 'lib/hanami/application_name.rb', line 45

def to_s
  @name
end