Class: Virginity::Vcard::NameHandler
- Inherits:
-
Object
- Object
- Virginity::Vcard::NameHandler
- Defined in:
- lib/virginity/vcard/name_handler.rb
Overview
Instance Method Summary collapse
- #add_nickname(nick) ⇒ Object
-
#complete ⇒ Object
generate the fn using the complete name including prefix, additional parts and suffix.
-
#empty? ⇒ Boolean
are all parts of the N field empty? (“N:;;;;”).
-
#fn ⇒ Object
add a fn if it’s not there (since it is required by the vCard specs) and return it.
-
#generate_fn(options = {}) ⇒ Object
generate a FN field using the following fields n > nickname > org > email > impp > tel.
- #has_nickname?(nick) ⇒ Boolean
-
#initialize(vcard) ⇒ NameHandler
constructor
takes a Vcard object or a String.
-
#merge_with!(other_name, options = {}) ⇒ Object
merge this name with other_name; conflicting parts will raise a MergeError.
-
#n ⇒ Object
add a n if it’s not there (since it is required by the vCard specs) and return it.
-
#nicknames ⇒ Object
an array with all nicknames.
- #remove_nickname(nick) ⇒ Object
-
#reset_formatted! ⇒ Object
regenerate the formatted name (that is the FN field).
-
#to_s ⇒ Object
(also: #formatted)
formatted name.
Constructor Details
#initialize(vcard) ⇒ NameHandler
takes a Vcard object or a String
16 17 18 19 20 21 22 |
# File 'lib/virginity/vcard/name_handler.rb', line 16 def initialize(vcard) if vcard.is_a? Vcard @vcard = vcard else @vcard = Vcard.from_vcard(vcard.to_s) end end |
Instance Method Details
#add_nickname(nick) ⇒ Object
111 112 113 114 115 |
# File 'lib/virginity/vcard/name_handler.rb', line 111 def add_nickname(nick) @vcard << SeparatedField.new("NICKNAME", EncodingDecoding::encode_text_list([nick])) reset_formatted! nick end |
#complete ⇒ Object
generate the fn using the complete name including prefix, additional parts and suffix
72 73 74 |
# File 'lib/virginity/vcard/name_handler.rb', line 72 def complete generate_fn(:complete_name => true) end |
#empty? ⇒ Boolean
are all parts of the N field empty? (“N:;;;;”)
102 103 104 |
# File 'lib/virginity/vcard/name_handler.rb', line 102 def empty? Name::PARTS.all? { |part| send(part).empty? } end |
#fn ⇒ Object
add a fn if it’s not there (since it is required by the vCard specs) and return it
77 78 79 |
# File 'lib/virginity/vcard/name_handler.rb', line 77 def fn @vcard.lines_with_name("FN").first || @vcard.add_field("FN:#{EncodingDecoding::encode_text(generate_fn)}") end |
#generate_fn(options = {}) ⇒ Object
generate a FN field using the following fields n > nickname > org > email > impp > tel
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/virginity/vcard/name_handler.rb', line 38 def generate_fn( = {}) nfield = n g = nfield.given.empty? ? nil : nfield.given f = nfield.family.empty? ? nil : nfield.family unless [g, f].compact.empty? if [:include_nickname] nick = @vcard.nicknames.empty? ? nil : "\"#{@vcard.nicknames.first.values.first}\"" [g, nick, f].compact.join(" ") elsif [:complete_name] prefix = nfield.prefix.empty? ? nil : nfield.prefix additional = nfield.additional.empty? ? nil : nfield.additional suffix = nfield.suffix.empty? ? nil : nfield.suffix [prefix, g, additional, f, suffix].compact.join(" ") else [g, f].compact.join(" ") end else if not @vcard.nicknames.empty? nicknames.first elsif @vcard.organisations.first @vcard.organisations.first.values.first elsif @vcard.emails.first @vcard.emails.first.address elsif @vcard.impps.first @vcard.impps.first.address elsif @vcard.telephones.first @vcard.telephones.first.number else "" end end end |
#has_nickname?(nick) ⇒ Boolean
126 127 128 |
# File 'lib/virginity/vcard/name_handler.rb', line 126 def has_nickname?(nick) @vcard.nicknames.any? { |nickname| nickname.values.include?(nick) } end |
#merge_with!(other_name, options = {}) ⇒ Object
merge this name with other_name; conflicting parts will raise a MergeError
if the option :simple_name_resolving is true we choose the value in this name instead of raising an error. Parts that are not present in self will be filled in with the value from other_name
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/virginity/vcard/name_handler.rb', line 133 def merge_with!(other_name, = {}) Name::PARTS.each do |part| own, his = send(part).rstrip, other_name.send(part).rstrip if own.empty? send "#{part}=", his elsif his.empty? or own == his # then nothing needs to be done else # :simple_name_resolving means keep our own name, don't take over his. iow: do nothing unless [:simple_name_resolving] raise MergeError, "#{part} name is different: '#{own}' and '#{his}'" end end end self end |
#n ⇒ Object
add a n if it’s not there (since it is required by the vCard specs) and return it
82 83 84 |
# File 'lib/virginity/vcard/name_handler.rb', line 82 def n @vcard.lines_with_name("N").first || @vcard.add_field("N:;;;;") end |
#nicknames ⇒ Object
an array with all nicknames
107 108 109 |
# File 'lib/virginity/vcard/name_handler.rb', line 107 def nicknames @vcard.nicknames.map {|n| n.values.to_a }.flatten end |
#remove_nickname(nick) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/virginity/vcard/name_handler.rb', line 117 def remove_nickname(nick) @vcard.nicknames.each do |nickname| nickname.values.delete(nick) @vcard.delete nickname if nickname.raw_value.empty? # the singular 'value' is meant here, don't change it to values! end reset_formatted! nick end |
#reset_formatted! ⇒ Object
regenerate the formatted name (that is the FN field)
31 32 33 34 |
# File 'lib/virginity/vcard/name_handler.rb', line 31 def reset_formatted! @vcard.delete(*@vcard.lines_with_name("FN")) fn.text end |
#to_s ⇒ Object Also known as: formatted
formatted name
25 26 27 |
# File 'lib/virginity/vcard/name_handler.rb', line 25 def to_s fn.text end |