class User
A class representing a syntactically valid user; that is, an email address. Once constructed, you can be sure that it's valid.
Public Class Methods
Initialize this User
object. If either of the local/domain parts is invalid, then either an {InvalidUserError} or an {InvalidDomainError} will be raised containing the reason why that part is invalid.
@param username [String] an email address from which to construct
this User.
# File lib/common/user.rb, line 42 def initialize(username) if not username.is_a?(String) msg = 'username must be a String ' msg += "but a #{username.class.to_s()} was given" raise InvalidUserError.new(msg) end parts = username.split('@') if parts.length() < 2 then msg = "the username #{username} does not contain an '@' symbol" raise InvalidUserError.new(msg) end localpart = parts[0] if localpart.length() > 64 then msg = "the local part of #{username} cannot have more than 64 characters" raise InvalidUserError(msg) end if localpart.empty? then msg = "the local part of #{username} cannot be empty" raise InvalidUserError.new(msg) end @localpart = localpart @domain = Domain.new(parts[1]) end
Public Instance Methods
Compare two User
objects for sorting purposes. The comparison is is based on their String representations.
@param other [User] the User
object to compare me to.
@return [0,1,-1] a trinary indicator of how self relates to other,
obtained by performing the same comparison on the String representations of *self* and *other*.
# File lib/common/user.rb, line 117 def <=>(other) return self.to_s() <=> other.to_s() end
Check if this User
is equal to some other User
. The comparison is based on their String representations.
@param other [User] the User
object to compare me to.
@return [Boolean] If self and other have equal String
representations, then true is returned. Otherwise, false is returned.
# File lib/common/user.rb, line 103 def ==(other) return self.to_s() == other.to_s() end
Obtain the domain part of this User
object as a string.
@return [String] a String representation of this User's domain.
# File lib/common/user.rb, line 30 def domainpart() return @domain.to_s() end
Obtain the “user” part of this User
object as a String.
@return [String] the “user” part of this User's “user@domain”
address.
# File lib/common/user.rb, line 79 def localpart() return @localpart end
Convert this User
to an email address string.
@return [String] an email address String constructed from this
user's local and domain parts.
# File lib/common/user.rb, line 89 def to_s() return @localpart + '@' + @domain.to_s() end