Class: AccountNameValidator Abstract

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/account_name_validator.rb

Overview

This class is abstract.

Subclass this validator to perform your specific account name validations.

Generic EachValidator that validates account names on various communication services such as Skype, Yahoo!, etc. This class provides a simple DSL for describing valid account names on these sites, performing all validation for you.

Subclass this class and use the provided DSL to describe account names on a given site. An example for a fictional service called Talkalot:

class TalkalotValidator < AccountNameValidator
  min_length 5
  max_length 64
  valid_chars "A-Z0-9_"
end

With your validator defined you can now use it in your Active Record models like any other EachValidator:

  validates :talkalot_id,
            talkalot: true

This class automatically handles the following options passed to the validates method:

| | | |:-------------|:--------------------------------| | :allow_nil | Allows nil values. | | :message | Provide a custom error message. |

Error messages generated by this class are stored in the translation table. The localization keys used are generated by the ActiveModel::Errors#generate_message method (see its documentation for more information). The lastmost element of the localization key is the error message key. The error message key is a combination of a validator subclass's AccountNameValidator.error_key_prefix and the error key suffix for a given constraint.

By default the error key prefix is the underscored

As an example, for the TalkalotValidator example above, the error message key used in the event that a two-letter account name is given would be :talkalot_too_short. If you wanted to override the prefix, you could do:

class TalkalotValidator < AccountNameValidator
  error_key_prefix :talky
end

In this case the error message key for a two-letter account name would be :talky_too_short. (You'd do this if Talkalot accounts were called "talkies," for example.)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_validation(key) {|value| ... } ⇒ Object

Describes a custom restraint on account names.

Parameters:

  • key (Symbol)

    The error message key suffix to use.

Yields:

  • (value)

    The custom validation.

Yield Parameters:

  • value (String)

    The value to validate. This will have been coerced into a String.

Yield Returns:

  • (true, false)

    Whether or not the validation succeeded.



98
99
100
101
102
# File 'lib/account_name_validator.rb', line 98

def self.add_validation(key, &block)
  return unless block

  self.validations += [[block, key]]
end

.error_key_prefixSymbol .error_key_prefix(value) ⇒ Object

Overloads:

  • .error_key_prefixSymbol

    Returns the prefix for error message keys used by this class.

    Returns:

    • (Symbol)

      The error message key prefix.

  • .error_key_prefix(value) ⇒ Object

    Sets the error message key prefix this class uses.

    Parameters:

    • value (Symbol)

      The new error message key prefix.



84
85
86
87
88
# File 'lib/account_name_validator.rb', line 84

def self.error_key_prefix(value=nil)
  return @error_key_prefix || to_s.demodulize.sub(/Validator$/, "").underscore.to_sym unless value

  @error_key_prefix = value
end

.first_char(charlist) ⇒ Object

Enforces a valid set of characters for the first character of the account name. Uses the "invalid_first_char" error message key suffix.

Parameters:

  • charlist (String)

    A set of valid characters, in regex character class format (e.g., "[A-Z0-9_]").



138
139
140
# File 'lib/account_name_validator.rb', line 138

def self.first_char(charlist)
  add_validation(:invalid_first_char) { |value| value[0] =~ /\A[#{charlist}]\z/ }
end

.max_length(num) ⇒ Object

Enforces a maximum length on account names. Uses the "too_long" error message key suffix.

Parameters:

  • num (Fixnum)

    The maximum number of characters.



118
119
120
# File 'lib/account_name_validator.rb', line 118

def self.max_length(num)
  add_validation(:too_long) { |value| value.length <= num }
end

.min_length(num) ⇒ Object

Enforces a minimum length on account names. Uses the "too_short" error message key suffix.

Parameters:

  • num (Fixnum)

    The minimum number of characters.



109
110
111
# File 'lib/account_name_validator.rb', line 109

def self.min_length(num)
  add_validation(:too_short) { |value| value.length >= num }
end

.valid_chars(charlist) ⇒ Object

Enforces a valid set of characters. Uses the "invalid_chars" error message key suffix.

Parameters:

  • charlist (String)

    A set of valid characters, in regex character class format (e.g., "[A-Z0-9_]").



128
129
130
# File 'lib/account_name_validator.rb', line 128

def self.valid_chars(charlist)
  add_validation(:invalid_chars) { |value| value =~ /\A[#{charlist}]+\z/ }
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/account_name_validator.rb', line 68

def validate_each(record, attribute, value)
  return if options[:allow_nil] && value.nil?
  return if options[:allow_blank] && value.blank?
  return unless self.class.validations

  self.class.validations.each { |block, key| record.errors.add(attribute, options[:message] || record.errors.generate_message(attribute, :"#{self.class.error_key_prefix}_#{key}")) unless block[value.to_s] }
end