lib/vcard/field.rb in vcard-0.2.13 vs lib/vcard/field.rb in vcard-0.2.14
- old
+ new
@@ -99,14 +99,16 @@
line << value.to_str
end
line
end
+
# Decode a field.
def Field.decode0(atline) # :nodoc:
if !(atline =~ Bnf::LINE)
- raise ::Vcard::InvalidEncodingError, atline
+ raise(::Vcard::InvalidEncodingError, atline) if ::Vcard.configuration.raise_on_invalid_line?
+ return false
end
atgroup = $1.upcase
atname = $2.upcase
paramslist = $3
@@ -163,23 +165,31 @@
atparams[name] << ($1 || $2)
end
end
end
- [ atgroup, atname, atparams, atvalue ]
+ [ true, atgroup, atname, atparams, atvalue ]
end
def initialize(line) # :nodoc:
@line = line.to_str
- @group, @name, @params, @value = Field.decode0(@line)
+ @valid, @group, @name, @params, @value = Field.decode0(@line)
- @params.each do |pname,pvalues|
- pvalues.freeze
+ if valid?
+ @params.each do |pname,pvalues|
+ pvalues.freeze
+ end
+ else
+ @group = @name = ''
end
self
end
+ def valid?
+ @valid
+ end
+
# Create a field by decoding +line+, a String which must already be
# unfolded. Decoded fields are frozen, but see #copy().
def Field.decode(line)
new(line).freeze
end
@@ -321,11 +331,11 @@
end
end
# Is the #name of this Field +name+? Names are case insensitive.
def name?(name)
- @name.casecmp(name) == 0
+ @name.to_s.casecmp(name) == 0
end
# Is the #group of this field +group+? Group names are case insensitive.
# A +group+ of nil matches if the field has no group.
def group?(group)
@@ -588,10 +598,10 @@
# FIXME - should change this so it doesn't assign to @line here, so @line
# is used to preserve original encoding. That way, #encode can only wrap
# new fields, not old fields.
def mutate(g, n, p, v) #:nodoc:
line = Field.encode0(g, n, p, v)
- @group, @name, @params, @value = Field.decode0(line)
+ @valid, @group, @name, @params, @value = Field.decode0(line)
@line = line
self
rescue ::Vcard::InvalidEncodingError => e
raise ArgumentError, e.to_s
end