Class: Stable::Validators::AppName

Inherits:
Object
  • Object
show all
Defined in:
lib/stable/validators/app_name.rb

Overview

Validates and normalizes Rails application names

Constant Summary collapse

VALID_PATTERN =
/\A[a-z0-9]+(-[a-z0-9]+)*\z/
MAX_LENGTH =
63

Class Method Summary collapse

Class Method Details

.call!(name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/stable/validators/app_name.rb', line 10

def self.call!(name)
  normalized = normalize(name)

  unless valid?(normalized)
    raise Thor::Error, "      Invalid app name: \"\#{name}\"\n\n      Use only:\n        - lowercase letters (a-z)\n        - numbers (0-9)\n        - hyphens (-)\n\n      Rules:\n        - no spaces or underscores\n        - cannot start or end with a hyphen\n        - max \#{MAX_LENGTH} characters\n\n      Example:\n        stable new my-app\n    MSG\n  end\n\n  normalized\nend\n"

.normalize(name) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/stable/validators/app_name.rb', line 35

def self.normalize(name)
  name
    .downcase
    .strip
    .gsub(/\s+/, '-') # spaces → hyphens
    .gsub(/[^a-z0-9-]/, '') # drop invalid chars
    .gsub(/-+/, '-')        # collapse hyphens
    .gsub(/\A-|-+\z/, '')   # trim hyphens
end

.valid?(name) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/stable/validators/app_name.rb', line 45

def self.valid?(name)
  name.length <= MAX_LENGTH && VALID_PATTERN.match?(name)
end