Class: RailsMailerSandbox::Storage
- Inherits:
-
Object
- Object
- RailsMailerSandbox::Storage
- Defined in:
- lib/rails_mailer_sandbox.rb
Class Method Summary collapse
- .add(mail) ⇒ Object
- .all ⇒ Object
- .clear ⇒ Object
- .count ⇒ Object
- .delete(id) ⇒ Object
- .find(id) ⇒ Object
Class Method Details
.add(mail) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/rails_mailer_sandbox.rb', line 102 def add(mail) = Message.new(mail) locked do |directory| temporary = File.join(directory, ".#{.id}.tmp") begin File.open(temporary, "wb", 0o600) do |file| file.write(.raw_source) file.flush file.fsync end File.rename(temporary, File.join(directory, "#{.id}.eml")) ensure File.delete(temporary) if File.exist?(temporary) end end end |
.all ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/rails_mailer_sandbox.rb', line 120 def all locked do |directory| Dir.glob(File.join(directory, "*.eml")).sort_by { |path| [File.mtime(path), path] }.reverse.map do |path| Message.new(Mail.read(path), id: File.basename(path, ".eml")) end end end |
.clear ⇒ Object
144 145 146 |
# File 'lib/rails_mailer_sandbox.rb', line 144 def clear locked { |directory| Dir.glob(File.join(directory, "*.eml")).each { |path| File.delete(path) } } end |
.count ⇒ Object
148 149 150 |
# File 'lib/rails_mailer_sandbox.rb', line 148 def count locked { |directory| Dir.glob(File.join(directory, "*.eml")).size } end |
.delete(id) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/rails_mailer_sandbox.rb', line 136 def delete(id) return unless valid_id?(id) locked do |directory| path = File.join(directory, "#{id}.eml") File.delete(path) if File.file?(path) end end |