class Codebreaker::Player
Constants
- NAME_LENGTH_RANGE
Attributes
created_at[R]
errors[R]
name[R]
Public Class Methods
new(name)
click to toggle source
# File lib/codebreaker/player.rb, line 9 def initialize(name) @name = name @created_at = DateTime.now @errors = [] end
Public Instance Methods
valid?()
click to toggle source
# File lib/codebreaker/player.rb, line 15 def valid? assert errors.empty? end
Private Instance Methods
assert()
click to toggle source
# File lib/codebreaker/player.rb, line 22 def assert validate_not_empty validate_string if errors.empty? validate_min_length if errors.empty? validate_max_length if errors.empty? end
validate_max_length()
click to toggle source
# File lib/codebreaker/player.rb, line 42 def validate_max_length max_length = NAME_LENGTH_RANGE.max errors << I18n.t(:'errors.player.max_length_name', length: max_length) if name.length > max_length end
validate_min_length()
click to toggle source
# File lib/codebreaker/player.rb, line 37 def validate_min_length min_length = NAME_LENGTH_RANGE.min errors << I18n.t(:'errors.player.min_length_name', length: min_length) if name.length < min_length end
validate_not_empty()
click to toggle source
# File lib/codebreaker/player.rb, line 29 def validate_not_empty errors << I18n.t(:'errors.player.blank_name') if name.to_s.strip.empty? end
validate_string()
click to toggle source
# File lib/codebreaker/player.rb, line 33 def validate_string errors << I18n.t(:'errors.player.non_string_name') unless !!(name =~ /\A[a-zA-Z_0-9]+\z/) end