]>
gitweb.michael.orlitzky.com - mailshears.git/blob - lib/common/user.rb
1 require 'common/domain'
2 require 'common/errors'
4 # A class representing a syntactically valid user; that is, an email
5 # address. Once constructed, you can be sure that it's valid.
9 # @localpart is a String containing the "user" part of "user@domain".
12 # @domain contains a {Domain} object representing the domain part of
17 # Obtain the {Domain} object corresponding to this User.
19 # @return [Domain] the domain corresponding to this User.
26 # Obtain the domain part of this User object as a string.
28 # @return [String] a String representation of this User's domain.
35 # Initialize this User object. If either of the local/domain parts
36 # is invalid, then either an {InvalidUserError} or an {InvalidDomainError}
37 # will be raised containing the reason why that part is invalid.
39 # @param username [String] an email address from which to construct
42 def initialize(username
)
44 if not username
.is_a
?(String
)
45 msg
= 'username must be a String '
46 msg +
= "but a #{username.class.to_s()} was given"
47 raise InvalidUserError
.new(msg
)
50 parts
= username
.split('@')
52 if parts
.length() < 2 then
53 msg
= "the username #{username} does not contain an '@' symbol"
54 raise InvalidUserError
.new(msg
)
59 if localpart
.length() > 64 then
60 msg
= "the local part of #{username} cannot have more than 64 characters"
61 raise InvalidUserError(msg
)
64 if localpart
.empty
? then
65 msg
= "the local part of #{username} cannot be empty"
66 raise InvalidUserError
.new(msg
)
69 @localpart = localpart
70 @domain = Domain
.new(parts
[1])
74 # Obtain the "user" part of this User object as a String.
76 # @return [String] the "user" part of this User's "user@domain"
84 # Convert this User to an email address string.
86 # @return [String] an email address String constructed from this
87 # user's local and domain parts.
90 return @localpart +
'@' +
@domain.to_s()
94 # Check if this User is equal to some other User. The comparison
95 # is based on their String representations.
97 # @param other [User] the User object to compare me to.
99 # @return [Boolean] If *self* and *other* have equal String
100 # representations, then true is returned. Otherwise, false is
104 return self.to_s() == other
.to_s()
108 # Compare two User objects for sorting purposes. The comparison is
109 # is based on their String representations.
111 # @param other [User] the User object to compare me to.
113 # @return [0,1,-1] a trinary indicator of how *self* relates to *other*,
114 # obtained by performing the same comparison on the String
115 # representations of *self* and *other*.
118 return self.to_s() <=> other
.to_s()