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 |
# 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
#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
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 |
Instance Method Details
#eql?(o) ⇒ Boolean
127 |
# File 'lib/sup/person.rb', line 127 def eql? o; email.eql? o.email end |
#full_address ⇒ Object
59 60 61 |
# File 'lib/sup/person.rb', line 59 def full_address Person.full_address @name, @email end |
#hash ⇒ Object
128 |
# File 'lib/sup/person.rb', line 128 def hash; email.hash end |
#indexable_content ⇒ Object
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 |
#longname ⇒ Object
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 |
#mediumname ⇒ Object
45 |
# File 'lib/sup/person.rb', line 45 def mediumname; @name || @email; end |
#shortname ⇒ Object
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_me ⇒ Object
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_s ⇒ Object
18 |
# File 'lib/sup/person.rb', line 18 def to_s; "#@name <#@email>" end |