Class: UsernameValidator
- Inherits:
-
Object
- Object
- UsernameValidator
- Defined in:
- app/models/username_validator.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
-
.perform_validation(object, field_name) ⇒ Object
Public: Perform the validation of a field in a given object it adds the errors (if any) to the object that we’re giving as parameter.
Instance Method Summary collapse
-
#initialize(username) ⇒ UsernameValidator
constructor
A new instance of UsernameValidator.
- #user ⇒ Object
- #valid_format? ⇒ Boolean
Constructor Details
#initialize(username) ⇒ UsernameValidator
Returns a new instance of UsernameValidator.
18 19 20 21 |
# File 'app/models/username_validator.rb', line 18 def initialize(username) @username = username&.unicode_normalize @errors = [] end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
23 24 25 |
# File 'app/models/username_validator.rb', line 23 def errors @errors end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
24 25 26 |
# File 'app/models/username_validator.rb', line 24 def username @username end |
Class Method Details
.perform_validation(object, field_name) ⇒ Object
Public: Perform the validation of a field in a given object it adds the errors (if any) to the object that we’re giving as parameter
object - Object in which we’re performing the validation field_name - name of the field that we’re validating
Example: UsernameValidator.perform_validation(user, ‘name’)
11 12 13 14 15 16 |
# File 'app/models/username_validator.rb', line 11 def self.perform_validation(object, field_name) validator = UsernameValidator.new(object.public_send(field_name)) unless validator.valid_format? validator.errors.each { |e| object.errors.add(field_name.to_sym, e) } end end |
Instance Method Details
#user ⇒ Object
26 27 28 |
# File 'app/models/username_validator.rb', line 26 def user @user ||= User.new(user) end |
#valid_format? ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/models/username_validator.rb', line 30 def valid_format? username_present? username_length_min? username_length_max? username_char_valid? username_char_allowed? username_first_char_valid? username_last_char_valid? username_no_double_special? username_does_not_end_with_confusing_suffix? errors.empty? end |