Class: Maildir::Message
- Inherits:
-
Object
- Object
- Maildir::Message
- Includes:
- Comparable
- Defined in:
- lib/maildir/message.rb
Constant Summary collapse
- COLON =
COLON seperates the unique name from the info
':'- INFO =
The default info, to which flags are appended
"2,"- FLAG_NAMES =
{ :passed => 'P', :replied => 'R', :seen => 'S', :trashed => 'T', :draft => 'D', :flagged => 'F' }
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#info ⇒ Object
Returns the value of attribute info.
-
#unique_name ⇒ Object
readonly
Returns the value of attribute unique_name.
Class Method Summary collapse
-
.create(maildir, data) ⇒ Object
Create a new message in maildir with data.
-
.serializer ⇒ Object
DEPRECATED: Get the serializer.
-
.serializer=(serializer) ⇒ Object
DEPRECATED: Set the serializer.
Instance Method Summary collapse
-
#<=>(message) ⇒ Object
Compares messages by their paths.
-
#add_flag(flag) ⇒ Object
Adds a flag to a message.
-
#atime ⇒ Object
Returns the message's atime, or false if the file doesn't exist.
-
#data ⇒ Object
Returns the message's data from disk.
-
#destroy ⇒ Object
Deletes the message path and freezes the message object.
-
#filename ⇒ Object
Returns the filename of the message.
-
#flags ⇒ Object
Returns an array of single letter flags applied to the message.
-
#flags=(*flags) ⇒ Object
Sets the flags on a message.
-
#initialize(maildir, key = nil) ⇒ Message
constructor
Create a new, unwritten message or instantiate an existing message.
-
#inspect ⇒ Object
Friendly inspect method.
-
#key ⇒ Object
Returns the key to identify the message.
-
#mtime ⇒ Object
Returns the message's mtime, or false if the file doesn't exist.
-
#path ⇒ Object
Returns the full path to the message.
-
#process ⇒ Object
Move a message from new to cur, add info.
-
#remove_flag(flags) ⇒ Object
Removes flags from a message.
-
#serializer ⇒ Object
Helper to get serializer.
-
#utime(atime, mtime) ⇒ Object
Updates the modification and access time.
-
#write(data) ⇒ Object
Writes data to disk.
Constructor Details
#initialize(maildir, key = nil) ⇒ Message
Create a new, unwritten message or instantiate an existing message. If key is nil, create a new message:
Message.new(maildir) # => a new, unwritten message
If key is not nil, instantiate a message object for the message at
key.
Message.new(maildir, key) # => an existing message
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/maildir/message.rb', line 41 def initialize(maildir, key=nil) @maildir = maildir if key.nil? @dir = :tmp @info = nil @unique_name = Maildir::UniqueName.create else parse_key(key) end unless Maildir::SUBDIRS.include? dir raise ArgumentError, "State must be in #{Maildir::SUBDIRS.inspect}" end end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
32 33 34 |
# File 'lib/maildir/message.rb', line 32 def dir @dir end |
#info ⇒ Object
Returns the value of attribute info.
32 33 34 |
# File 'lib/maildir/message.rb', line 32 def info @info end |
#unique_name ⇒ Object (readonly)
Returns the value of attribute unique_name.
32 33 34 |
# File 'lib/maildir/message.rb', line 32 def unique_name @unique_name end |
Class Method Details
.create(maildir, data) ⇒ Object
Create a new message in maildir with data. The message is first written to the tmp dir, then moved to new. This is a shortcut for:
= Maildir::Message.new(maildir)
.write(data)
14 15 16 17 18 |
# File 'lib/maildir/message.rb', line 14 def self.create(maildir, data) = self.new(maildir) .write(data) end |
.serializer ⇒ Object
DEPRECATED: Get the serializer.
22 23 24 |
# File 'lib/maildir/message.rb', line 22 def self.serializer Maildir.serializer end |
.serializer=(serializer) ⇒ Object
DEPRECATED: Set the serializer.
28 29 30 |
# File 'lib/maildir/message.rb', line 28 def self.serializer=(serializer) Maildir.serializer = serializer end |
Instance Method Details
#<=>(message) ⇒ Object
Compares messages by their paths. If message is a different class, return nil. Otherwise, return 1, 0, or -1.
59 60 61 62 63 64 |
# File 'lib/maildir/message.rb', line 59 def <=>() # Return nil if comparing different classes return nil unless self.class === self.path <=> .path end |
#add_flag(flag) ⇒ Object
Adds a flag to a message. Returns the message's key if successful, false otherwise.
134 135 136 |
# File 'lib/maildir/message.rb', line 134 def add_flag(flag) self.flags = (flags << flag.upcase) end |
#atime ⇒ Object
Returns the message's atime, or false if the file doesn't exist.
175 176 177 |
# File 'lib/maildir/message.rb', line 175 def atime guard { File.atime(path) } end |
#data ⇒ Object
Returns the message's data from disk. If the path doesn't exist, freeze's the object and raises Errno:ENOENT
164 165 166 |
# File 'lib/maildir/message.rb', line 164 def data guard(true) { serializer.load(path) } end |
#destroy ⇒ Object
Deletes the message path and freezes the message object. Returns 1 if the file was destroyed, false if the file was missings.
186 187 188 189 |
# File 'lib/maildir/message.rb', line 186 def destroy freeze guard { File.delete(path) } end |
#filename ⇒ Object
Returns the filename of the message
148 149 150 |
# File 'lib/maildir/message.rb', line 148 def filename [unique_name, info].compact.join(COLON) end |
#flags ⇒ Object
Returns an array of single letter flags applied to the message
122 123 124 |
# File 'lib/maildir/message.rb', line 122 def flags @info.to_s.sub(INFO,'').split(//) end |
#flags=(*flags) ⇒ Object
Sets the flags on a message. Returns the message's key if successful, false otherwise.
128 129 130 |
# File 'lib/maildir/message.rb', line 128 def flags=(*flags) self.info = INFO + sort_flags(flags.flatten.join('')) end |
#inspect ⇒ Object
Friendly inspect method
67 68 69 |
# File 'lib/maildir/message.rb', line 67 def inspect "#<#{self.class} key=#{key} maildir=#{@maildir.inspect}>" end |
#key ⇒ Object
Returns the key to identify the message
153 154 155 |
# File 'lib/maildir/message.rb', line 153 def key File.join(dir.to_s, filename) end |
#mtime ⇒ Object
Returns the message's mtime, or false if the file doesn't exist.
180 181 182 |
# File 'lib/maildir/message.rb', line 180 def mtime guard { File.mtime(path) } end |
#path ⇒ Object
Returns the full path to the message
158 159 160 |
# File 'lib/maildir/message.rb', line 158 def path File.join(@maildir.path, key) end |
#process ⇒ Object
Move a message from new to cur, add info. Returns the message's key
92 93 94 |
# File 'lib/maildir/message.rb', line 92 def process rename(:cur, INFO) end |
#remove_flag(flags) ⇒ Object
Removes flags from a message. Returns the message's key if successful, false otherwise.
- flags
String or Array
142 143 144 145 |
# File 'lib/maildir/message.rb', line 142 def remove_flag(flags) return self.flags if !flags || flags.empty? self.flags = self.flags.reject { |f| f =~ /[#{Array(flags).join}]/i } end |
#serializer ⇒ Object
Helper to get serializer.
72 73 74 |
# File 'lib/maildir/message.rb', line 72 def serializer @maildir.serializer end |
#utime(atime, mtime) ⇒ Object
Updates the modification and access time. Returns 1 if successful, false otherwise.
170 171 172 |
# File 'lib/maildir/message.rb', line 170 def utime(atime, mtime) guard { File.utime(atime, mtime, path) } end |
#write(data) ⇒ Object
Writes data to disk. Can only be called on messages instantiated without a key (which haven't been written to disk). After successfully writing to disk, rename the message to the new dir
Returns the message's key
81 82 83 84 85 86 87 88 |
# File 'lib/maildir/message.rb', line 81 def write(data) raise "Can only write to messages in tmp" unless :tmp == @dir # Write out contents to tmp serializer.dump(data, path) rename(:new) end |