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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sup/person.rb', line 88

def self.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



120
121
122
123
# File 'lib/sup/person.rb', line 120

def self.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



84
85
86
# File 'lib/sup/person.rb', line 84

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

.full_address(name, email) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sup/person.rb', line 50

def Person.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)


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

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

#full_addressObject



62
63
64
# File 'lib/sup/person.rb', line 62

def full_address
  Person.full_address @name, @email
end

#hashObject



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

def hash; email.hash end

#indexable_contentObject

see comments in self.from_address



126
127
128
# File 'lib/sup/person.rb', line 126

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

#longnameObject



40
41
42
43
44
45
46
# File 'lib/sup/person.rb', line 40

def longname
  if @name && @email
    "#@name <#@email>"
  else
    @email
  end
end

#mediumnameObject



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

def mediumname; @name || @email; end

#shortnameObject

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

alias :eql? :==
def hash; [name, email].hash; end


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sup/person.rb', line 27

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sup/person.rb', line 67

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
# File 'lib/sup/person.rb', line 21

def to_s; "#@name <#@email>" end