sandstrom
sandstrom

Reputation: 15092

Serialize Mail::Message to yaml like other objects

Problem

Normal objects serialize to something like:

"--- !ruby/object {}\n\n"

whereas Mail::Message serialize to:

"--- \nMime-Version: \"1.0\"\nbody: \"\"\nContent-Transfer-Encoding:[…]"

Question

How can I have Mail::Message serialized just like other objects?

Background

Gem Versions:

Code

Object.new.to_yaml #gives
"--- !ruby/object {}\n\n"

Mail::Message.new.to_yaml #gives
"--- \nMime-Version: \"1.0\"\nbody: \"\"\nContent-Transfer-Encoding: 7bit\nMessage-ID: <[email protected]>\nsubject: \nContent-Type: text/plain\nDate: Fri, 06 May 2011 15:47:17 +0000\n"

Desired output

"--- !ruby/object:Mail::Message {}\n\n"

Upvotes: 0

Views: 728

Answers (3)

Michał Szajbe
Michał Szajbe

Reputation: 9002

Use YAML directly instead of going through to_yaml method.

YAML.dump(Mail::Message.new)

Upvotes: 0

sandstrom
sandstrom

Reputation: 15092

The reason was a flawed patch to the Mail gem. Details are outlined here:

https://github.com/mikel/mail/pull/237

Upvotes: 1

Vasiliy Ermolovich
Vasiliy Ermolovich

Reputation: 24617

Since Mail::Message has the own to_yaml method - https://github.com/mikel/mail/blob/master/lib/mail/message.rb#L1714 - I think it's impossible without monkey-patching like

module Mail
  class Message
    def to_yaml
      self.class.name.to_yaml
    end
  end
end

irb(main):011:0> Mail::Message.new.to_yaml
=> "--- Mail::Message\n...\n"

Upvotes: 0

Related Questions