Class: Maildir::Serializer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/maildir/serializer/base.rb

Overview

The Maildir::Serializer::Base class reads & writes data to disk as a string. Other serializers (e.g. Maildir::Serializer::Mail) can extend this class to do some pre- and post-processing of the string.

The Serializer API has two methods:

load(path) # => returns data
dump(data, path) # => returns number of bytes written

Direct Known Subclasses

JSON, Mail, Marshal, YAML

Instance Method Summary collapse

Instance Method Details

#dump(data, path) ⇒ Object

Writes data to path. Returns number of bytes written. If data acts like an IO object (i.e., data responds to the read method), we call data.read or the more efficient IO.copy_stream available in ruby 1.9.1.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/maildir/serializer/base.rb', line 22

def dump(data, path)
  if data.respond_to?(:read)
    if IO.respond_to?(:copy_stream)
      IO.copy_stream(data, path)
    else
      write(data.read, path)
    end
  else
    write(data, path)
  end
end

#load(path) ⇒ Object

Reads the file at path. Returns the contents of path.



12
13
14
15
16
# File 'lib/maildir/serializer/base.rb', line 12

def load(path)
  File.open(path,'rb') do |f|
    f.read
  end
end