Class: AccountNameValidator Abstract
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- AccountNameValidator
- Defined in:
- lib/account_name_validator.rb
Overview
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:
““ ruby 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`:
““ ruby
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:
““ ruby 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.)
Direct Known Subclasses
AimValidator, DiscordValidator, SignalValidator, SkypeValidator, SteamValidator, TelegramValidator, XboxLiveValidator, YahooImValidator
Class Method Summary collapse
-
.add_validation(key) {|value| ... } ⇒ Object
Describes a custom restraint on account names.
- .error_key_prefix(value = nil) ⇒ Object
-
.first_char(charlist) ⇒ Object
Enforces a valid set of characters for the first character of the account name.
-
.max_length(num) ⇒ Object
Enforces a maximum length on account names.
-
.min_length(num) ⇒ Object
Enforces a minimum length on account names.
-
.valid_chars(charlist) ⇒ Object
Enforces a valid set of characters.
Instance Method Summary collapse
Class Method Details
.add_validation(key) {|value| ... } ⇒ Object
Describes a custom restraint on account names.
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_prefix ⇒ Symbol .error_key_prefix(value) ⇒ Object
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.
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.
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.
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.
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 [:allow_nil] && value.nil? return if [:allow_blank] && value.blank? return unless self.class.validations self.class.validations.each { |block, key| record.errors.add(attribute, [:message] || record.errors.(attribute, :"#{self.class.error_key_prefix}_#{key}")) unless block[value.to_s] } end |