Quoting anejr at tbtm.org, on Mon, Mar 20, 2006 at 05:28:53PM
-0600:> On Mar 20, 2006, at 4:03 PM, Sam Roberts wrote:
>
> >On Mon, Mar 20, 2006 at 12:59:03PM -0600, Al Evans wrote:
> >>by trial and error, because I wanted to construct my own
"N" field.
> >
> >Vpim::Maker::Vcard allows you to construct your own N field,
> >specifying the components of the field in any way you like.
> >
> >Why do you need to create the Field() directly?
>
> Uh... I was working from the examples instead of the source, and
> couldn''t figure out how to change a field without copying the
whole
> vcard. I want users to be able to edit the info obtained from their
> vCards. Now I''m doing this (for example)
ex_cpvcard.txt, at the end uses a Maker::Vcard to add a nickname to an
existing vCard.
> def url=(new_url)
> card = get_id_card
> f = card.field(''URL'').copy
> f.value = new_url
> f.freeze #????
> card.delete(card.field(''URL''))
> card.push(f)
> end
>
> I didn''t see any obvious way to change the contents of fields,
since
> they''re frozen.
Correct, if you twiddle their insides the card can become invalid.
There are countless fields in the vCard spec, I haven''t gone through to
try and implement every one. I''m happy to implement the ones you use,
though, if you give me a list.
Also, recognize that you can add support to Maker::Vcard yourself (and
if you do, I''d appreciate you emailing me the patches!).
This example will be in the next release. Note that it shows a simple
field, but using the example implementations, its easy to add more
complex examples.
# ex_mkyourown.rb
require ''vpim/maker/vcard''
module Vpim
module Maker
class Vcard
# Add a user-defined field, X-MY-OWN:.
#
# This can be done both to encode custom fields, or to add support for
# fields that Maker::Vcard doesn''t support. In the latter case,
please
# submit your methods so I can add them to vPim.
def add_my_own(value)
@card << Vpim::DirectoryInfo::Field.create(
''X-MY-OWN'', value.to_str );
end
end
end
end
Vpim::Maker::Vcard.make2 do |m|
m.add_name do |n|
n.given = ''Given''
end
m.add_my_own ''my value''
end
puts card
Or your example:
require ''vpim/maker/vcard''
module Vpim
module Maker
class Vcard
def add_url(value)
@card << Vpim::DirectoryInfo::Field.create( ''URL'',
value.to_str );
end
end
end
end
# ...
def url=(new_url)
card = get_id_card
card.delete(card.field(''URL''))
# ... note that the above will only delete the first URL, there is no
# limit on the number of URL fields in a vCard. You probably want to
# handle that.
Vpim::Maker::Vcard.make2(card) { |m| m.add_url new_url }
end
Cheers,
Sam