Class: Minbox::Inbox
Class Method Summary collapse
Instance Method Summary collapse
- #each ⇒ Object
- #emails(count: 0) ⇒ Object
- #empty! ⇒ Object
-
#initialize(root_dir:) ⇒ Inbox
constructor
A new instance of Inbox.
- #open(subject:) ⇒ Object
- #wait_until(seconds: 10, wait: 0.1) ⇒ Object
- #wait_until!(seconds: 10, wait: 0.1, &block) ⇒ Object
Constructor Details
#initialize(root_dir:) ⇒ Inbox
Returns a new instance of Inbox.
12 13 14 15 16 |
# File 'lib/minbox/inbox.rb', line 12 def initialize(root_dir:) @semaphore = Mutex.new start_listening(root_dir) empty! end |
Class Method Details
.instance(root_dir:) ⇒ Object
7 8 9 10 |
# File 'lib/minbox/inbox.rb', line 7 def self.instance(root_dir:) @instances ||= {} @instances[root_dir] ||= new(root_dir: root_dir) end |
Instance Method Details
#each ⇒ Object
52 53 54 55 56 |
# File 'lib/minbox/inbox.rb', line 52 def each @emails.each do |_id, email| yield email end end |
#emails(count: 0) ⇒ Object
18 19 20 21 |
# File 'lib/minbox/inbox.rb', line 18 def emails(count: 0) wait_until { |x| x.count >= count } if count.positive? with_lock(&:values) end |
#empty! ⇒ Object
46 47 48 49 50 |
# File 'lib/minbox/inbox.rb', line 46 def empty! with_lock do @emails = {} end end |
#open(subject:) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/minbox/inbox.rb', line 38 def open(subject:) wait_until do emails.find do |email| subject.is_a?(String) ? email.subject == subject : email.subject.match?(subject) end end end |
#wait_until(seconds: 10, wait: 0.1) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/minbox/inbox.rb', line 23 def wait_until(seconds: 10, wait: 0.1) iterations = (seconds / wait).to_i iterations.times do result = yield(self) return result if result sleep wait end nil end |
#wait_until!(seconds: 10, wait: 0.1, &block) ⇒ Object
34 35 36 |
# File 'lib/minbox/inbox.rb', line 34 def wait_until!(seconds: 10, wait: 0.1, &block) raise "timeout: expired" unless wait_until(seconds: seconds, wait: wait, &block) end |