Wednesday, October 1, 2008

Add a character set to Vpim's Vcard-Export

If you are using Vpim to export Vcards and ever need to include a character set on your lines in the generated Vcard-file, here's how to do it:
# open up Vpim's DirectoryInfo::Field class
module Vpim
class DirectoryInfo
class Field
class << self
alias_method :orig_create, :create

# we overwrite Field.create for setting the CHARSET
def create(name, value='', params={})
# specify the lines you don't want to add a charset to
lines_to_ignore = %w(BEGIN END VERSION)
params.merge!({'CHARSET' => 'ISO-8859-1'}) unless lines_to_ignore.include? name
orig_create(name, value, params)
end
end

end
end
end

1 comment: