   #       isValid = User.validateUsername('demosthenes42')
   #     catch validateErr
   #       message = validateErr.message
+  #
   @validateUsername: (username) ->
     unless /\S+/.test username
       throw new @ValidationError "Username must not be blank"


    return true

  # Construction
  # ------------

  # Creates a new user with the given attributes (username, password, and email):
  #
  #     user = new User
  #       username: 'demosthenes'
  #       password: 'secret'
  #       email:    'demos@athens.net'
  constructor: (params, opts={}) ->
    @username = params.username
    @email    = params.email
    @password = params.password

  # Validation
  # ----------

  # Performs instance validation, returns true on success:
  #
  #     isValid = user.validate()
  #
  # Also sets properties `validationStatus` and `validationError`:
  #
  #     user.validationStatus # True on success, otherwise false.
  #     user.validationError  # Null on success, otherwise the last error object.
  validate: () ->
    try
      @validateUsername() and @validatePassword() and @validateEmail()
      @validationStatus = true
      @validationError = null
    catch err
      @validationStatus = false
      @validationError = err
    finally
      return @validationStatus

  # Instance validator methods
  # --------------------------

  # Each calls the static validator, passing parameters from the instance:
  #
  #     try
  #       isValid = user.validateUsername()
  #     catch validationErr
  #       message = validationErr.message
  validateUsername: () ->
    User.validateUsername @username

  validatePassword: () ->
    User.validatePassword @password

  validateEmail: () ->
    User.validateEmail @email

module.exports = User


    return true

  @validatePasswordHash: (password_hash) ->
    unless /\S+/.test password_hash
      throw new @ValidationError "Password hash must not be blank"

    return true

  @validateEmail: (email) ->
    unless /\S+/.test email
      throw new @ValidationError "Email must not be blank"
