Class: Stable::Validators::AppName
- Inherits:
-
Object
- Object
- Stable::Validators::AppName
- 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, <<~MSG Invalid app name: "#{name}" Use only: - lowercase letters (a-z) - numbers (0-9) - hyphens (-) Rules: - no spaces or underscores - cannot start or end with a hyphen - max #{MAX_LENGTH} characters Example: stable new my-app MSG end normalized end |
.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
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 |