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

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

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

  @email = email.strip.gsub(/\s+/, " ").downcase
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



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

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



117
118
119
120
# File 'lib/sup/person.rb', line 117

def self.from_address_list ss
  return [] if ss.nil?
  ss.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



81
82
83
# File 'lib/sup/person.rb', line 81

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

.full_address(name, email) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sup/person.rb', line 47

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)


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

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

#full_addressObject



59
60
61
# File 'lib/sup/person.rb', line 59

def full_address
  Person.full_address @name, @email
end

#hashObject



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

def hash; email.hash end

#indexable_contentObject

see comments in self.from_address



123
124
125
# File 'lib/sup/person.rb', line 123

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

#longnameObject



37
38
39
40
41
42
43
# File 'lib/sup/person.rb', line 37

def longname
  if @name && @email
    "#@name <#@email>"
  else
    @email
  end
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? :==
def hash; [name, email].hash; end


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sup/person.rb', line 24

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sup/person.rb', line 64

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



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

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