Class: Maildir
Defined Under Namespace
Modules: Keywords, Serializer, Subdirs Classes: Message, UniqueName
Constant Summary collapse
- SUBDIRS =
[:tmp, :new, :cur].freeze
- READABLE_DIRS =
SUBDIRS.reject{|s| :tmp == s}.freeze
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#<=>(maildir) ⇒ Object
Compare maildirs by their paths.
-
#add(data) ⇒ Object
Writes data object out as a new message.
-
#create_directories ⇒ Object
Ensure subdirectories exist.
-
#delete(key) ⇒ Object
Deletes the message for key by calling destroy() on the message.
-
#get(key) ⇒ Object
Returns a message object for key.
-
#get_stale_tmp(time = Time.now - 129600) ⇒ Object
Finds messages in the tmp folder that have not been modified since
time
. -
#initialize(path, create = true) ⇒ Maildir
constructor
Create a new maildir at
path
. -
#inspect ⇒ Object
Friendly inspect method.
-
#list(dir, options = {}) ⇒ Object
Returns an arry of messages from :new or :cur directory, sorted by key.
Constructor Details
#initialize(path, create = true) ⇒ Maildir
Create a new maildir at path
. If create
is true, will ensure that the required subdirectories exist.
13 14 15 16 17 18 |
# File 'lib/maildir.rb', line 13 def initialize(path, create = true) @path = File.(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 |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/maildir.rb', line 9 def path @path end |
Instance Method Details
#<=>(maildir) ⇒ Object
Compare maildirs by their paths. If maildir is a different class, return nil. Otherwise, return 1, 0, or -1.
23 24 25 26 27 28 |
# File 'lib/maildir.rb', line 23 def <=>(maildir) # Return nil if comparing different classes return nil unless self.class === maildir self.path <=> maildir.path end |
#add(data) ⇒ Object
Writes data object out as a new message. Returns a Maildir::Message. See Maildir::Message.create for more.
80 81 82 |
# File 'lib/maildir.rb', line 80 def add(data) Maildir::Message.create(self, data) end |
#create_directories ⇒ Object
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.
45 46 47 48 49 50 |
# File 'lib/maildir.rb', line 45 def create_directories SUBDIRS.each do |subdir| subdir_path = File.join(path, subdir.to_s) FileUtils.mkdir_p(subdir_path) end end |
#delete(key) ⇒ Object
Deletes the message for key by calling destroy() on the message.
90 91 92 |
# File 'lib/maildir.rb', line 90 def delete(key) get(key).destroy end |
#get(key) ⇒ Object
Returns a message object for key
85 86 87 |
# File 'lib/maildir.rb', line 85 def get(key) Maildir::Message.new(self, key) end |
#get_stale_tmp(time = Time.now - 129600) ⇒ Object
Finds messages in the tmp folder that have not been modified since time
. time
defaults to 36 hours ago.
96 97 98 99 100 |
# File 'lib/maildir.rb', line 96 def get_stale_tmp(time = Time.now - 129600) list(:tmp).select do || (mtime = .mtime) && mtime < time end end |
#inspect ⇒ Object
Friendly inspect method
31 32 33 |
# File 'lib/maildir.rb', line 31 def inspect "#<#{self.class} path=#{@path}>" end |
#list(dir, options = {}) ⇒ Object
Returns an arry of messages from :new or :cur directory, sorted by key. 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/maildir.rb', line 58 def list(dir, = {}) unless SUBDIRS.include? dir.to_sym raise ArgumentError, "dir must be :new, :cur, or :tmp" end keys = get_dir_listing(dir) # Sort the keys (chronological order) # TODO: make sorting configurable keys.sort! # Apply the limit after sorting if limit = [:limit] keys = keys[0,limit] end # Map keys to message objects keys.map{|key| get(key)} end |