Class: Redwood::Person

Inherits:
Object show all
Defined in:
lib/sup/person.rb

Direct Known Subclasses

Account

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, email) ⇒ Person

Returns a new instance of Person.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sup/person.rb', line 6

def initialize name, email
  raise ArgumentError, "email can't be nil" unless email

  email.fix_encoding!

  @name = if name
    name.fix_encoding!
    name = name.strip.gsub(/\s+/, " ")
    name =~ /^(['"]\s*)(.*?)(\s*["'])$/ ? $2 : name
    name.gsub('\\\\', '\\')
  end

  @email = email.strip.gsub(/\s+/, " ")
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



4
5
6
# File 'lib/sup/person.rb', line 4

def email
  @email
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/sup/person.rb', line 4

def name
  @name
end

Class Method Details

.from_address(s) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sup/person.rb', line 100

def from_address s
  return nil if s.nil?

  ## try and parse an email address and name
  name, email = case s
    when /(.+?) ((\S+?)@\S+) \3/
      ## ok, this first match cause is insane, but bear with me.  email
      ## addresses are stored in the to/from/etc fields of the index in a
      ## weird format: "name address first-part-of-address", i.e.  spaces
      ## separating those three bits, and no <>'s. this is the output of
      ## #indexable_content. here, we reverse-engineer that format to extract
      ## a valid address.
      ##
      ## we store things this way to allow searches on a to/from/etc field to
      ## match any of those parts. a more robust solution would be to store a
      ## separate, non-indexed field with the proper headers. but this way we
      ## save precious bits, and it's backwards-compatible with older indexes.
      [$1, $2]
    when /["'](.*?)["'] <(.*?)>/, /([^,]+) <(.*?)>/
      a, b = $1, $2
      [a.gsub('\"', '"'), b]
    when /<((\S+?)@\S+?)>/
      [$2, $1]
    when /((\S+?)@\S+)/
      [$2, $1]
    else
      [nil, s]
    end

  from_name_and_email name, email
end

.from_address_list(ss) ⇒ Object



132
133
134
135
# File 'lib/sup/person.rb', line 132

def from_address_list ss
  return [] if ss.nil?
  ss.dup.split_on_commas.map { |s| self.from_address s }
end

.from_name_and_email(name, email) ⇒ Object

return “canonical” person using contact manager or create one if not found or contact manager not available



96
97
98
# File 'lib/sup/person.rb', line 96

def from_name_and_email name, email
  ContactManager.instantiated? && ContactManager.person_for(email) || Person.new(name, email)
end

.full_address(name, email) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sup/person.rb', line 82

def full_address name, email
  if name && email
    if name =~ /[",@]/
      "#{name.inspect} <#{email}>" # escape quotes
    else
      "#{name} <#{email}>"
    end
  else
    email
  end
end

Instance Method Details

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


71
# File 'lib/sup/person.rb', line 71

def eql? o; email.eql? o.email end

#full_addressObject



51
52
53
# File 'lib/sup/person.rb', line 51

def full_address
  Person.full_address @name, @email
end

#hashObject



72
# File 'lib/sup/person.rb', line 72

def hash; email.hash end

#indexable_contentObject

see comments in self.from_address



76
77
78
# File 'lib/sup/person.rb', line 76

def indexable_content
  [name, email, email.split(/@/).first].join(" ")
end

#longnameObject



47
48
49
# File 'lib/sup/person.rb', line 47

def longname
  to_s
end

#mediumnameObject



45
# File 'lib/sup/person.rb', line 45

def mediumname; @name || @email; end

#shortnameObject

def == o; o && o.email == email; end

alias :eql? :==


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sup/person.rb', line 32

def shortname
  case @name
  when /\S+, (\S+)/
    $1
  when /(\S+) \S+/
    $1
  when nil
    @email
  else
    @name
  end
end

#sort_by_meObject

when sorting addresses, sort by this



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/sup/person.rb', line 56

def sort_by_me
  case @name
  when /^(\S+), \S+/
    $1
  when /^\S+ \S+ (\S+)/
    $1
  when /^\S+ (\S+)/
    $1
  when nil
    @email
  else
    @name
  end.downcase
end

#to_sObject



21
22
23
24
25
26
27
# File 'lib/sup/person.rb', line 21

def to_s
  if @name
    "#@name <#@email>"
  else
    @email
  end
end