Default serializer.
Create a new maildir at path
. If create
is true,
will ensure that the required subdirectories exist.
# File lib/maildir.rb, line 35 def initialize(path, create = true) @path = File.expand_path(path) @path = File.join(@path, '/') # Ensure path has a trailing slash @path_regexp = /^#{Regexp.quote(@path)}/ # For parsing directory listings create_directories if create end
Gets the default serializer.
# File lib/maildir.rb, line 24 def self.serializer @@serializer end
Sets the default serializer.
# File lib/maildir.rb, line 29 def self.serializer=(serializer) @@serializer = serializer end
Compare maildirs by their paths. If maildir is a different class, return nil. Otherwise, return 1, 0, or -1.
# File lib/maildir.rb, line 50 def <=>(maildir) # Return nil if comparing different classes return nil unless self.class === maildir self.path <=> maildir.path end
Writes data object out as a new message. Returns a Maildir::Message. See Maildir::Message.create for more.
# File lib/maildir.rb, line 118 def add(data) Maildir::Message.create(self, data) end
Ensure subdirectories exist. This can safely be called multiple times, but must hit the disk. Avoid calling this if you're certain the directories exist.
# File lib/maildir.rb, line 72 def create_directories SUBDIRS.each do |subdir| subdir_path = File.join(path, subdir.to_s) FileUtils.mkdir_p(subdir_path) end end
Deletes the message for key by calling destroy() on the message.
# File lib/maildir.rb, line 128 def delete(key) get(key).destroy end
Returns a message object for key
# File lib/maildir.rb, line 123 def get(key) Maildir::Message.new(self, key) end
Finds messages in the tmp folder that have not been modified since
time
. time
defaults to 36 hours ago.
# File lib/maildir.rb, line 134 def get_stale_tmp(time = Time.now - 129600) list(:tmp).select do |message| (mtime = message.mtime) && mtime < time end end
Friendly inspect method
# File lib/maildir.rb, line 58 def inspect "#<#{self.class} path=#{@path}>" end
Returns an arry of messages from :new or :cur directory, sorted by key. If options is specified and directory is :cur, returns messages with flags specified
E.g. maildir.list(:cur, :flags => 'F') # => lists all messages with flag 'F' maildir.list(:cur, :flags => 'FS') # => lists all messages with flag 'F' and 'S'; Flags must be specified in acending ASCII order ('FS' and not 'SF') maildir.list(:cur, :flags => '') # => lists all messages without any flags This option does not work for :new directory
If options is specified, returns only so many keys.
E.g.
maildir.list(:new) # => all new messages maildir.list(:cur, :limit => 10) # => 10 oldest messages in cur
# File lib/maildir.rb, line 93 def list(dir, options = {}) unless SUBDIRS.include? dir.to_sym raise ArgumentError, "dir must be :new, :cur, or :tmp" end # Set flags to filter messages # Silently ignored if dir is :new flags = (dir.to_sym == :cur) ? options[:flags] : nil keys = get_dir_listing(dir, :flags => flags) # Sort the keys (chronological order) # TODO: make sorting configurable keys.sort! # Apply the limit after sorting if limit = options[:limit] keys = keys[0,limit] end # Map keys to message objects keys.map{|key| get(key)} end
Returns an array of keys in dir
# File lib/maildir.rb, line 142 def get_dir_listing(dir, options={}) filter = "*" filter = "#{filter}:2,#{options[:flags]}" if options[:flags] search_path = File.join(self.path, dir.to_s, filter) keys = Dir.glob(search_path) # Remove the maildir's path from the keys keys.each do |key| key.sub!(@path_regexp, "") end end