class Maildir::UniqueName

Class for generating unique file names for new messages

Public Class Methods

counter() click to toggle source

Return a thread-safe increasing counter

# File lib/maildir/unique_name.rb, line 7
def counter
  @counter_mutex ||= Mutex.new
  @counter_mutex.synchronize do
    @counter = @counter.to_i + 1
  end
end
create() click to toggle source
# File lib/maildir/unique_name.rb, line 14
def create
  self.new.to_s
end
new() click to toggle source

Return a unique file name based on strategy

# File lib/maildir/unique_name.rb, line 20
def initialize
  # Use the same time object
  @now = Time.now
end

Public Instance Methods

to_s() click to toggle source

Return the name as a string

# File lib/maildir/unique_name.rb, line 26
def to_s
  [left, middle, right].join(".")
end

Protected Instance Methods

delivery_count() click to toggle source
# File lib/maildir/unique_name.rb, line 56
def delivery_count
  self.class.counter.to_s
end
left() click to toggle source

The left part of the unique name is the number of seconds from since the UNIX epoch

# File lib/maildir/unique_name.rb, line 33
def left
  @now.to_i.to_s
end
microsecond() click to toggle source
# File lib/maildir/unique_name.rb, line 48
def microsecond
  @now.usec.to_s
end
middle() click to toggle source

The middle part contains the microsecond, the process id, and a per-process incrementing counter

# File lib/maildir/unique_name.rb, line 39
def middle
  "M#{'%06d' % microsecond}P#{process_id}Q#{delivery_count}"
end
process_id() click to toggle source
# File lib/maildir/unique_name.rb, line 52
def process_id
  Process.pid.to_s
end
right() click to toggle source

The right part is the hostname

# File lib/maildir/unique_name.rb, line 44
def right
  Socket.gethostname
end