Class: Mailer::FileCache

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mailer/file_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(home, mailbox_configs = {}) ⇒ FileCache

Returns a new instance of FileCache.



14
15
16
17
18
19
20
# File 'lib/mailer/file_cache.rb', line 14

def initialize(home, mailbox_configs={})
  @home = returning File.expand_path(home) do |path|
    FileUtils.mkdir_p(path)
    @name = File.basename(path)
    @mailbox = Mailer::Mailbox.new(mailbox_configs)
  end
end

Instance Attribute Details

#homeObject (readonly)

Returns the value of attribute home.



12
13
14
# File 'lib/mailer/file_cache.rb', line 12

def home
  @home
end

#mailboxObject (readonly)

Returns the value of attribute mailbox.



12
13
14
# File 'lib/mailer/file_cache.rb', line 12

def mailbox
  @mailbox
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/mailer/file_cache.rb', line 12

def name
  @name
end

Instance Method Details

#clear!Object

empties the cache of all entries



71
72
73
74
75
76
77
78
# File 'lib/mailer/file_cache.rb', line 71

def clear!
  returning true do
    self.keys.each do |key|
      self.delete(key)
    end
    @entries = nil
  end
end

#delete(key) ⇒ Object



63
64
65
66
67
68
# File 'lib/mailer/file_cache.rb', line 63

def delete(key)
  returning self.key_path(key) do |path|
    FileUtils.rm_rf(path)
    @entries = nil
  end
end

#eachObject



103
104
105
106
107
108
109
# File 'lib/mailer/file_cache.rb', line 103

def each
  self.keys.each do |key|
    self.read(key) do |file|
      yield(file) if block_given?
    end
  end
end

#email(key) ⇒ Object



44
45
46
47
48
# File 'lib/mailer/file_cache.rb', line 44

def email(key)
  read(key) do |file|
    Mailer::Email.parse(file.read)
  end
end

#emailsObject



88
89
90
91
92
# File 'lib/mailer/file_cache.rb', line 88

def emails
  self.collect do |file|
    Mailer::Email.parse(file.read)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/mailer/file_cache.rb', line 99

def empty?
  self.entries.empty?
end

#entriesObject



80
81
82
# File 'lib/mailer/file_cache.rb', line 80

def entries
  @entries ||= Dir[cache_entries_path]
end

#get_new_mail!Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/mailer/file_cache.rb', line 22

def get_new_mail!
  returning([]) do |file_paths|
    @mailbox.check do |email|
      email.each do |mail|
        file_paths << self.write(mail)
        mail.delete
      end
    end
  end
end

#keysObject



84
85
86
# File 'lib/mailer/file_cache.rb', line 84

def keys
  self.entries.collect{|file| File.basename(file)}
end

#lengthObject Also known as: size



94
95
96
# File 'lib/mailer/file_cache.rb', line 94

def length
  self.entries.length
end

#read(key) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/mailer/file_cache.rb', line 33

def read(key)
  path = self.key_path(key)
  if File.exists?(path)
    File.open(path, 'r') do |file|
      yield(file) if block_given?
    end
  else
    raise ArgumentError, "the file '#{path}' does not exists"
  end
end

#write(*args) ⇒ Object Also known as: <<

> write(mail_obj)

> write(custom_key, mail_obj)



52
53
54
55
56
57
58
59
60
# File 'lib/mailer/file_cache.rb', line 52

def write(*args)
  key, mail = get_key_and_mail(args)
  returning self.key_path(key) do |path|
    File.open(path, 'w+') do |file|
      file.write mail.pop
    end
    @entries = nil
  end
end