Class: UsernameNotReservedValidator::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/username_not_reserved_validator/validator.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{ case_insensitive: true }

Instance Method Summary collapse

Constructor Details

#initialize(username, options = {}) ⇒ Validator

Returns a new instance of Validator.



5
6
7
8
9
10
11
12
13
# File 'lib/username_not_reserved_validator/validator.rb', line 5

def initialize(username, options = {})
  @username = username
  @options = DEFAULT_OPTIONS.merge(options)

  # patch for historical typo
  if @options.key?(:case_insencitive)
    @options[:case_insensitive] = @options[:case_insencitive]
  end
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/username_not_reserved_validator/validator.rb', line 15

def valid?
  additional_reserved_names = @options[:additional_reserved_names] || []
  words = ReservedNames.list + additional_reserved_names
  words += words.map(&:pluralize)

  if @options[:case_insensitive]
    words.map!(&:downcase)
    username = @username.downcase
  else
    username = @username
  end

  words.include?(username).!
end