Define a custom YAML encoder and decoder.

Drop support for deserializing YAML made with Mechanize <2.1.
This commit is contained in:
Akinori MUSHA 2012-10-22 00:50:05 +09:00
parent 53e5b270ee
commit d59a0ee5ca

View file

@ -9,6 +9,13 @@ end
# This class is used to represent an HTTP Cookie. # This class is used to represent an HTTP Cookie.
class HTTP::Cookie class HTTP::Cookie
PERSISTENT_PROPERTIES = %w[
name value
domain for_domain path
secure
expires created_at accessed_at
]
# In Ruby < 1.9.3 URI() does not accept an URI object. # In Ruby < 1.9.3 URI() does not accept an URI object.
if RUBY_VERSION < "1.9.3" if RUBY_VERSION < "1.9.3"
module URIFix module URIFix
@ -293,18 +300,31 @@ class HTTP::Cookie
"#{@name}=#{@value}" "#{@name}=#{@value}"
end end
# YAML serialization helper for Syck.
def to_yaml_properties
PERSISTENT_PROPERTIES.map { |name| "@#{name}" }
end
# YAML serialization helper for Psych.
def encode_with(coder)
PERSISTENT_PROPERTIES.each { |key|
coder[key.to_s] = instance_variable_get(:"@#{key}")
}
end
# YAML deserialization helper for Syck.
def init_with(coder) def init_with(coder)
yaml_initialize(coder.tag, coder.map) yaml_initialize(coder.tag, coder.map)
end end
# YAML deserialization helper for Psych.
def yaml_initialize(tag, map) def yaml_initialize(tag, map)
@for_domain = true # for forward compatibility
map.each { |key, value| map.each { |key, value|
case key case key
when 'domain' when 'name'
self.domain = value # ditto @name = value
else when *PERSISTENT_PROPERTIES
instance_variable_set(:"@#{key}", value) send(:"#{key}=", value)
end end
} }
end end