Module: DataMapper::Types::Imap::NetIMAPAddressType

Included in:
Bcc, Cc, From, ReplyTo, Sender, To
Defined in:
lib/dm-imap-adapter/types.rb

Class Method Summary collapse

Class Method Details

.dump(value, property) ⇒ Object



9
10
11
# File 'lib/dm-imap-adapter/types.rb', line 9

def self.dump(value, property)
  typecast(value, property)
end

.load(value, property) ⇒ Object



5
6
7
# File 'lib/dm-imap-adapter/types.rb', line 5

def self.load(value, property)
  typecast(value, property)
end

.typecast(value, property) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dm-imap-adapter/types.rb', line 13

def self.typecast(value, property)
  if value.nil? || value.empty?
    []
  else
    # TODO: don't just test the first element of the array
    if value.is_a?(Array) && value.first.is_a?(Net::IMAP::Address)
      value
    else
      value = [value].flatten
      
      value.map do |val|
      
        if val =~ /<.*>$/
          matches = val.match(/(.*)<(.*)@(.*)>/)
          if matches
            n = Net::IMAP::Address.new
            n.name = matches[0].strip
            n.mailbox = matches[1].strip
            n.host = matches[2].strip
            n
          else
            nil
          end#matches
        else
          matches = val.split "@"
          if matches
            n = Net::IMAP::Address.new
            n.mailbox = matches[0].strip
            n.host = matches[1].strip
            n
          else
            nil
          end#matches
        end#val =~
        
      end.compact #end of each
    end#value.is_a?
  end#if value.nil?
end