Module: Maildir::Subdirs

Defined in:
lib/maildir/subdirs.rb

Defined Under Namespace

Classes: MaildirNotFound

Constant Summary collapse

ROOT_NAME =
'INBOX'
DELIM =
'.'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
12
13
14
# File 'lib/maildir/subdirs.rb', line 9

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)


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

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



57
58
59
# File 'lib/maildir/subdirs.rb', line 57

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

#mailbox_pathObject

returns the logical mailbox path



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

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

#nameObject



27
28
29
# File 'lib/maildir/subdirs.rb', line 27

def name
  root? ? ROOT_NAME : subdir_parts(path).last
end

#rootObject

returns the Maildir representing the root directory



62
63
64
# File 'lib/maildir/subdirs.rb', line 62

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)


67
68
69
# File 'lib/maildir/subdirs.rb', line 67

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

#subdir_by_path(sd_path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/maildir/subdirs.rb', line 16

def subdir_by_path(sd_path)
  return self if sd_path == ROOT_NAME
  if(File.directory?(p = File.join(path, ".#{sd_path}")))
    Maildir.new(p, false)
  elsif(File.directory?(p = File.join(path, ".#{ROOT_NAME}.#{sd_path}")))
    Maildir.new(p, false)
  else
    raise MaildirNotFound
  end
end

#subdirs(only_direct = true) ⇒ Object

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



45
46
47
48
49
50
51
52
53
54
# File 'lib/maildir/subdirs.rb', line 45

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