lib/vcard/dirinfo.rb in vcard-0.1.1 vs lib/vcard/dirinfo.rb in vcard-0.2.0
- old
+ new
@@ -1,14 +1,12 @@
-=begin
- Copyright (C) 2008 Sam Roberts
+# Copyright (C) 2008 Sam Roberts
- This library is free software; you can redistribute it and/or modify it
- under the same terms as the ruby language itself, see the file COPYING for
- details.
-=end
+# This library is free software; you can redistribute it and/or modify
+# it under the same terms as the ruby language itself, see the file
+# LICENSE-VPIM.txt for details.
-module Vpim
+module Vcard
# An RFC 2425 directory info object.
#
# A directory information object is a sequence of fields. The basic
# structure of the object, and the way in which it is broken into fields
# is common to all profiles of the directory info type.
@@ -17,27 +15,27 @@
#
# - [RFC2425] the directory information framework (ftp://ftp.ietf.org/rfc/rfc2425.txt)
#
# Here's an example of encoding a simple vCard using the low-level APIs:
#
- # card = Vpim::Vcard.create
- # card << Vpim::DirectoryInfo::Field.create('EMAIL', 'user.name@example.com', 'TYPE' => 'INTERNET' )
- # card << Vpim::DirectoryInfo::Field.create('URL', 'http://www.example.com/user' )
- # card << Vpim::DirectoryInfo::Field.create('FN', 'User Name' )
+ # card = Vcard::Vcard.create
+ # card << Vcard::DirectoryInfo::Field.create("EMAIL", "user.name@example.com", "TYPE" => "INTERNET" )
+ # card << Vcard::DirectoryInfo::Field.create("URL", "http://www.example.com/user" )
+ # card << Vcard::DirectoryInfo::Field.create("FN", "User Name" )
# puts card.to_s
#
- # Don't do it like that, use Vpim::Vcard::Maker.
+ # Don't do it like that, use Vcard::Vcard::Maker.
class DirectoryInfo
include Enumerable
private_class_method :new
# Initialize a DirectoryInfo object from +fields+. If +profile+ is
# specified, check the BEGIN/END fields.
def initialize(fields, profile = nil) #:nodoc:
if fields.detect { |f| ! f.kind_of? DirectoryInfo::Field }
- raise ArgumentError, 'fields must be an array of DirectoryInfo::Field objects'
+ raise ArgumentError, "fields must be an array of DirectoryInfo::Field objects"
end
@string = nil # this is used as a flag to indicate that recoding will be necessary
@fields = fields
@@ -51,11 +49,11 @@
# #join("\n"), or an IO object (which will be read to end-of-file).
#
# The lines in the string may be delimited using IETF (CRLF) or Unix (LF) conventions.
#
# A DirectoryInfo is mutable, you can add new fields to it, see
- # Vpim::DirectoryInfo::Field#create() for how to create a new Field.
+ # Vcard::DirectoryInfo::Field#create() for how to create a new Field.
#
# TODO: I don't believe this is ever used, maybe I can remove it.
def DirectoryInfo.decode(card) #:nodoc:
if card.respond_to? :to_str
string = card.to_str
@@ -65,11 +63,11 @@
string = card.read(nil)
else
raise ArgumentError, "DirectoryInfo cannot be created from a #{card.type}"
end
- fields = Vpim.decode(string)
+ fields = ::Vcard.decode(string)
new(fields)
end
# Create a new DirectoryInfo object. The +fields+ are an optional array of
@@ -82,13 +80,13 @@
# and see Field#create().
def DirectoryInfo.create(fields = [], profile = nil)
if profile
p = profile.to_str
- f = [ Field.create('BEGIN', p) ]
+ f = [ Field.create("BEGIN", p) ]
f.concat fields
- f.push Field.create('END', p)
+ f.push Field.create("END", p)
fields = f
end
new(fields, profile)
end
@@ -101,11 +99,11 @@
end
# The value of the first field named +name+, or nil if no
# match is found.
def [](name)
- enum_by_name(name).each { |f| return f.value if f.value != ''}
+ enum_by_name(name).each { |f| return f.value if f.value != ""}
enum_by_name(name).each { |f| return f.value }
nil
end
# An array of all the values of fields named +name+, converted to text
@@ -151,15 +149,15 @@
#
# Examples:
#
# Print all the nicknames in a card:
#
- # card.enum_by_name('NICKNAME') { |f| puts f.value }
+ # card.enum_by_name("NICKNAME") { |f| puts f.value }
#
# Print an Array of the preferred email addresses in the card:
#
- # pref_emails = card.enum_by_name('EMAIL').select { |f| f.pref? }
+ # pref_emails = card.enum_by_name("EMAIL").select { |f| f.pref? }
def enum_by_name(name)
Enumerator.new(self, Proc.new { |field| field.name?(name) })
end
# Returns an Enumerator for each Field for which #group?(+group+) is true.
@@ -170,13 +168,13 @@
# card.enum_by_group(group).each do |field|
# puts "#{group} -> #{field.name}"
# end
# end
#
- # or to get an array of all the fields in group 'AGROUP', you could do:
+ # or to get an array of all the fields in group "AGROUP", you could do:
#
- # card.enum_by_group('AGROUP').to_a
+ # card.enum_by_group("AGROUP").to_a
def enum_by_group(group)
Enumerator.new(self, Proc.new { |field| field.group?(group) })
end
# Returns an Enumerator for each Field for which +cond+.call(field) is true.
@@ -222,12 +220,12 @@
# creating a new Vcard, and copying over all the fields that you still
# want, rather than using #delete. This is easy with Vcard::Maker#copy, see
# the Vcard::Maker examples.
def delete(field)
case
- when field.name?('BEGIN'), field.name?('END')
- raise ArgumentError, 'Cannot delete BEGIN or END fields.'
+ when field.name?("BEGIN"), field.name?("END")
+ raise ArgumentError, "Cannot delete BEGIN or END fields."
else
@fields.delete field
end
self
@@ -249,13 +247,13 @@
# they are the specified profile.
def check_begin_end(profile=nil) #:nodoc:
unless @fields.first
raise "No fields to check"
end
- unless @fields.first.name? 'BEGIN'
+ unless @fields.first.name? "BEGIN"
raise "Needs BEGIN, found: #{@fields.first.encode nil}"
end
- unless @fields.last.name? 'END'
+ unless @fields.last.name? "END"
raise "Needs END, found: #{@fields.last.encode nil}"
end
unless @fields.last.value? @fields.first.value
raise "BEGIN/END mismatch: (#{@fields.first.value} != #{@fields.last.value}"
end