Class: Mail::TestRetriever

Inherits:
Retriever show all
Defined in:
lib/mail/network/retriever_methods/test_retriever.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Retriever

#all, #find_and_delete, #first, #last

Constructor Details

#initialize(values) ⇒ TestRetriever

Returns a new instance of TestRetriever.



16
17
18
# File 'lib/mail/network/retriever_methods/test_retriever.rb', line 16

def initialize(values)
  @@emails = []
end

Class Method Details

.emailsObject



8
9
10
# File 'lib/mail/network/retriever_methods/test_retriever.rb', line 8

def self.emails
  @@emails
end

.emails=(val) ⇒ Object



12
13
14
# File 'lib/mail/network/retriever_methods/test_retriever.rb', line 12

def self.emails=(val)
  @@emails = val
end

Instance Method Details

#find(options = nil, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mail/network/retriever_methods/test_retriever.rb', line 20

def find(options = nil, &block)
  options = options ? Hash[options] : {}
  options[:count] ||= :all
  options[:order] ||= :asc
  options[:what] ||= :first
  emails_index = (0...@@emails.size).to_a
  emails_index.reverse! if options[:what] == :last
  emails_index = case count = options[:count]
    when :all then emails_index
    when Integer then emails_index[0, count]
    else
      raise 'Invalid count option value: ' + count.inspect
  end
  if options[:what] == :last && options[:order] == :asc || options[:what] == :first && options[:order] == :desc
    emails_index.reverse!
  end
  emails_index.each { |idx| @@emails[idx].mark_for_delete = true } if options[:delete_after_find]
  emails = emails_index.map { |idx| @@emails[idx] }
  emails.each { |email| yield email } if block_given?
  @@emails.reject!(&:is_marked_for_delete?) if options[:delete_after_find]
  emails.size == 1 && options[:count] == 1 ? emails.first : emails
end