Class: Redwood::Person
Direct Known Subclasses
Instance Attribute Summary collapse
-
#email ⇒ Object
Returns the value of attribute email.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
- .from_address(s) ⇒ Object
- .from_address_list(ss) ⇒ Object
-
.from_name_and_email(name, email) ⇒ Object
return “canonical” person using contact manager or create one if not found or contact manager not available.
- .full_address(name, email) ⇒ Object
Instance Method Summary collapse
- #eql?(o) ⇒ Boolean
- #full_address ⇒ Object
- #hash ⇒ Object
-
#indexable_content ⇒ Object
see comments in self.from_address.
-
#initialize(name, email) ⇒ Person
constructor
A new instance of Person.
- #longname ⇒ Object
- #mediumname ⇒ Object
-
#shortname ⇒ Object
def == o; o && o.email == email; end alias :eql? :== def hash; [name, email].hash; end.
-
#sort_by_me ⇒ Object
when sorting addresses, sort by this.
- #to_s ⇒ Object
Constructor Details
#initialize(name, email) ⇒ Person
Returns a new instance of Person.
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
#email ⇒ Object
Returns the value of attribute email.
4 5 6 |
# File 'lib/sup/person.rb', line 4 def email @email end |
#name ⇒ Object
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 |
Instance Method Details
#eql?(o) ⇒ Boolean
130 |
# File 'lib/sup/person.rb', line 130 def eql? o; email.eql? o.email end |
#full_address ⇒ Object
62 63 64 |
# File 'lib/sup/person.rb', line 62 def full_address Person.full_address @name, @email end |
#hash ⇒ Object
131 |
# File 'lib/sup/person.rb', line 131 def hash; email.hash end |
#indexable_content ⇒ Object
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 |
#longname ⇒ Object
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 |
#mediumname ⇒ Object
48 |
# File 'lib/sup/person.rb', line 48 def mediumname; @name || @email; end |
#shortname ⇒ Object
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_me ⇒ Object
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_s ⇒ Object
21 |
# File 'lib/sup/person.rb', line 21 def to_s; "#@name <#@email>" end |