Module: Maildir::Subdirs

Defined in:
lib/maildir/subdirs.rb

Constant Summary collapse

ROOT_NAME =
'INBOX'
DELIM =
'.'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
# File 'lib/maildir/subdirs.rb', line 7

def self.included(base)
  base.instance_eval do 
    alias_method :inspect_without_subdirs, :inspect
    alias_method :inspect, :inspect_with_subdirs
  end
end

Instance Method Details

#create_subdir(name) ⇒ Object

Raises:

  • (ArgumentError)


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

def create_subdir(name)
  raise ArgumentError.new("'name' may not contain delimiter character (#{DELIM})") if name.include?(DELIM)
  full_name = (root? ? [] : subdir_parts(File.basename(path))).push(name).unshift('').join(DELIM)
  md = Maildir.new(File.join(path, full_name), true)
  @subdirs << md if @subdirs
  md
end

#inspect_with_subdirsObject

Friendly inspect method



40
41
42
# File 'lib/maildir/subdirs.rb', line 40

def inspect_with_subdirs
  "#<#{self.class} path=#{@path} mailbox_path=#{mailbox_path}>"
end

#mailbox_pathObject

returns the logical mailbox path



23
24
25
# File 'lib/maildir/subdirs.rb', line 23

def mailbox_path
  @mailbox_path ||= root? ? ROOT_NAME : subdir_parts(File.basename(path)).unshift(ROOT_NAME).join(DELIM)
end

#rootObject

returns the Maildir representing the root directory



45
46
47
# File 'lib/maildir/subdirs.rb', line 45

def root
  root? ? self : Maildir.new(File.dirname(path), false)
end

#root?Boolean

returns true if the parent directory doesn’t look like a maildir

Returns:

  • (Boolean)


50
51
52
# File 'lib/maildir/subdirs.rb', line 50

def root?
  ! ((Dir.entries(File.dirname(path)) & %w(cur new tmp)).size == 3)
end

#subdirs(only_direct = true) ⇒ Object

returns an array of Maildir objects representing the direct subdirectories of this Maildir



28
29
30
31
32
33
34
35
36
37
# File 'lib/maildir/subdirs.rb', line 28

def subdirs(only_direct=true)
  if root?
    @subdirs ||= (Dir.entries(path) - %w(. ..)).select {|e|
      e =~ /^\./ && File.directory?(File.join(path, e)) && (only_direct ? subdir_parts(e).size == 1 : true)
    }.map { |e| Maildir.new(File.join(path, e), false) }
  else
    my_parts = subdir_parts(File.basename(path))
    @subdirs ||= root.subdirs(false).select { |md| subdir_parts(File.basename(md.path))[0..-2] == my_parts }
  end
end