Class: Validator::Email

Inherits:
Object
  • Object
show all
Defined in:
lib/validator/email.rb

Constant Summary collapse

LOCAL_LENGTH =
64

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Email

Returns a new instance of Email.



12
13
14
15
# File 'lib/validator/email.rb', line 12

def initialize(value)
  @value = value
  self.parse
end

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



3
4
5
# File 'lib/validator/email.rb', line 3

def domain
  @domain
end

#localObject

Returns the local part (the left hand side of the @ sign in the email address) of the address

a = Email.new('[email protected]')
a.local #=> 'vitaliy'


8
9
10
# File 'lib/validator/email.rb', line 8

def local
  @local
end

Instance Method Details

#is_email?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/validator/email.rb', line 21

def is_email?
  @value.split(/@/).size == 2
end

#parseObject



17
18
19
# File 'lib/validator/email.rb', line 17

def parse
  @local, @domain = @value.split(/@/)
end

#valid?Boolean

valid if passes all conditions

Returns:

  • (Boolean)


34
35
36
# File 'lib/validator/email.rb', line 34

def valid?
  is_email? and valid_by_local_length? and valid_by_regexp?
end

#valid_by_local_length?(local_length = nil) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/validator/email.rb', line 25

def valid_by_local_length?(local_length = nil)
  @local.length <= (local_length || LOCAL_LENGTH)
end

#valid_by_regexp?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/validator/email.rb', line 29

def valid_by_regexp?
  @value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
end