Class: Mail::PartsList

Inherits:
Array
  • Object
show all
Defined in:
lib/mail/parts_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ PartsList

Returns a new instance of PartsList.



8
9
10
11
# File 'lib/mail/parts_list.rb', line 8

def initialize(*args)
  @parts = Array.new(*args)
  super @parts
end

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



6
7
8
# File 'lib/mail/parts_list.rb', line 6

def parts
  @parts
end

Instance Method Details

#attachmentsObject



24
25
26
# File 'lib/mail/parts_list.rb', line 24

def attachments
  Mail::AttachmentsList.new(@parts)
end

#collectObject Also known as: map



28
29
30
31
32
33
34
35
36
# File 'lib/mail/parts_list.rb', line 28

def collect
  if block_given?
    ary = PartsList.new
    each { |o| ary << yield(o) }
    ary
  else
    to_a
  end
end

#collect!Object

Raises:

  • (NoMethodError)


43
44
45
# File 'lib/mail/parts_list.rb', line 43

def collect!
  raise NoMethodError, "#collect! is not defined, please call #collect and create a new PartsList"
end

#delete_attachmentsObject



98
99
100
101
102
# File 'lib/mail/parts_list.rb', line 98

def delete_attachments
  recursive_delete_if { |part|
    part.attachment?
  }
end

#encode_with(coder) ⇒ Object

The #encode_with and #to_yaml methods are just implemented for the sake of backward compatibility ; the delegator does not correctly delegate these calls to the delegated object



16
17
18
# File 'lib/mail/parts_list.rb', line 16

def encode_with(coder) # :nodoc:
  coder.represent_object(nil, @parts)
end

#inspect_structure(parent_id = '') ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mail/parts_list.rb', line 47

def inspect_structure(parent_id = '')
  enum_for(:map).with_index { |part, i|
    i = i + 1 # Use 1-based indexes since this is for humans to read
    id = parent_id.empty? ? "#{i}" : "#{parent_id}.#{i}"
    if part.content_type == "message/rfc822"
      sub_list = Mail.new(part.body).parts
    else
      sub_list = part.parts
    end
    id + '. ' + part.inspect +
      if sub_list.any?
        "\n" + sub_list.inspect_structure(id)
      end.to_s
  }.join("\n")
end

#map!Object

Raises:

  • (NoMethodError)


39
40
41
# File 'lib/mail/parts_list.rb', line 39

def map!
  raise NoMethodError, "#map! is not defined, please call #collect and create a new PartsList"
end

#recursive_delete_ifObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mail/parts_list.rb', line 83

def recursive_delete_if
  delete_if { |part|
    if part.content_type == "message/rfc822"
      sub_list = Mail.new(part.body).parts
    else
      sub_list = part.parts
    end
    (yield part).tap {
      if sub_list.any?
        sub_list.recursive_delete_if {|part| yield part }
      end
    }
  }
end

#recursive_each(&block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mail/parts_list.rb', line 63

def recursive_each(&block)
  each do |part|
    if part.content_type == "message/rfc822"
      sub_list = Mail.new(part.body).parts
    else
      sub_list = part.parts
    end

    yield part

    sub_list.recursive_each(&block)
  end
end

#recursive_sizeObject



77
78
79
80
81
# File 'lib/mail/parts_list.rb', line 77

def recursive_size
  i = 0
  recursive_each {|p| i += 1 }
  i
end

#sortObject



104
105
106
# File 'lib/mail/parts_list.rb', line 104

def sort
  self.class.new(@parts.sort)
end

#sort!(order) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mail/parts_list.rb', line 108

def sort!(order)
  # stable sort should be used to maintain the relative order as the parts are added
  i = 0;
  sorted = @parts.sort_by do |a|
    # OK, 10000 is arbitrary... if anyone actually wants to explicitly sort 10000 parts of a
    # single email message... please show me a use case and I'll put more work into this method,
    # in the meantime, it works :)
    get_order_value(a, order) << (i += 1)
  end
  @parts.clear
  sorted.each { |p| @parts << p }
end

#to_yaml(options = {}) ⇒ Object

:nodoc:



20
21
22
# File 'lib/mail/parts_list.rb', line 20

def to_yaml(options = {}) # :nodoc:
  @parts.to_yaml(options)
end