Class: Swallow::MailDirFolder
- Inherits:
-
Object
- Object
- Swallow::MailDirFolder
- Defined in:
- lib/maildir.rb
Instance Method Summary collapse
- #add(emails) ⇒ Object
- #count ⇒ Object
- #delete_all_emails ⇒ Object
- #emails ⇒ Object
- #emails_newer_than(limit) ⇒ Object
- #emails_older_than(limit) ⇒ Object
- #enum ⇒ Object
-
#initialize(folder_path) ⇒ MailDirFolder
constructor
A new instance of MailDirFolder.
- #length(sub_path) ⇒ Object
- #path ⇒ Object
- #read_count ⇒ Object
- #status ⇒ Object
- #sweep!(limit) ⇒ Object
- #to_s ⇒ Object
- #unknown_count ⇒ Object
- #unread_count ⇒ Object
Constructor Details
#initialize(folder_path) ⇒ MailDirFolder
Returns a new instance of MailDirFolder.
8 9 10 11 |
# File 'lib/maildir.rb', line 8 def initialize(folder_path) @folder_path = folder_path @sub_folders = ["cur", "new", "tmp"] end |
Instance Method Details
#add(emails) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/maildir.rb', line 78 def add(emails) # We'll take these emails from some other location and add them to our "cur" folder. emails.each do |email| email.move_to(File.join(@folder_path,"cur")) end end |
#count ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/maildir.rb', line 30 def count v = 0 @sub_folders.each { |dir| v += length(dir) } return v end |
#delete_all_emails ⇒ Object
72 73 74 75 76 |
# File 'lib/maildir.rb', line 72 def delete_all_emails count = emails.length emails.each {|email| email.delete } puts "Deleted #{count} emails" end |
#emails ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/maildir.rb', line 59 def emails list = Array.new @sub_folders.each do |sub_path| path = File.join(@folder_path, sub_path) Dir.open(path).each do |f| filename = File.join(path,f) next if File.directory?(filename) list << Email.new(filename) end end return list end |
#emails_newer_than(limit) ⇒ Object
85 86 87 |
# File 'lib/maildir.rb', line 85 def emails_newer_than(limit) emails.find_all { |e| e.newer_than?(limit) } end |
#emails_older_than(limit) ⇒ Object
89 90 91 |
# File 'lib/maildir.rb', line 89 def emails_older_than(limit) emails.find_all { |e| e.older_than?(limit) } end |
#enum ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/maildir.rb', line 13 def enum Dir.chdir(@folder_path) #puts "Subfolders:" a = Array.new Dir.glob("\.*").sort.each { |dir| next unless File.directory?(dir) next if dir == "." or dir == ".." #puts "#{dir}" a << dir } a end |
#length(sub_path) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/maildir.rb', line 50 def length(sub_path) length = 0 Dir.open(File.join(@folder_path,sub_path)).each do |filename| next if filename == "." or filename == ".." length += 1 end length end |
#path ⇒ Object
26 27 28 |
# File 'lib/maildir.rb', line 26 def path @folder_path end |
#read_count ⇒ Object
42 43 44 |
# File 'lib/maildir.rb', line 42 def read_count emails.find_all{ |e| e.read == true }.length end |
#status ⇒ Object
100 101 102 |
# File 'lib/maildir.rb', line 100 def status "#{@folder_path} has #{unread_count} unread messages and #{read_count} read messages (#{unknown_count})." end |
#sweep!(limit) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/maildir.rb', line 93 def sweep!(limit) if limit.kind_of? Integer then @sub_folder.each do |sub_path| Dir.open(File.join(@folder_path,sub_path)).sweep!(limit) end end end |
#to_s ⇒ Object
103 104 105 |
# File 'lib/maildir.rb', line 103 def to_s "#{@folder_path}" end |
#unknown_count ⇒ Object
46 47 48 |
# File 'lib/maildir.rb', line 46 def unknown_count length("tmp") end |
#unread_count ⇒ Object
38 39 40 |
# File 'lib/maildir.rb', line 38 def unread_count emails.find_all{ |e| e.read == false }.length end |