-
Notifications
You must be signed in to change notification settings - Fork 941
Use composition over inheritance for PartsList
#782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,27 @@ | ||
require 'delegate' | ||
|
||
module Mail | ||
class PartsList < Array | ||
class PartsList < DelegateClass(Array) | ||
attr_reader :parts | ||
|
||
def initialize(*args) | ||
@parts = Array.new(*args) | ||
super @parts | ||
end | ||
|
||
# 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 | ||
def encode_with(coder) # :nodoc: | ||
coder.represent_object(nil, @parts) | ||
end | ||
|
||
def to_yaml(options = {}) # :nodoc: | ||
@parts.to_yaml(options) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means two PartsList objects are equal and a PartsList is equal to an array of the same parts, but a PartsList is not equal to a different PartsList object with the same parts. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh I'm not sure to understand but it looks like a PartsList is equal to a different PartsList object with the same parts: http://git.io/rrPtFA. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh - not sure how that's working! Should be falling back on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is the case, normally here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tests? |
||
|
||
def attachments | ||
Mail::AttachmentsList.new(self) | ||
Mail::AttachmentsList.new(@parts) | ||
end | ||
|
||
def collect | ||
|
@@ -14,8 +33,6 @@ def collect | |
to_a | ||
end | ||
end | ||
|
||
undef :map | ||
alias_method :map, :collect | ||
|
||
def map! | ||
|
@@ -27,20 +44,20 @@ def collect! | |
end | ||
|
||
def sort | ||
self.class.new(super) | ||
self.class.new(@parts.sort) | ||
end | ||
|
||
def sort!(order) | ||
# stable sort should be used to maintain the relative order as the parts are added | ||
i = 0; | ||
sorted = self.sort_by do |a| | ||
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 | ||
self.clear | ||
sorted.each { |p| self << p } | ||
@parts.clear | ||
sorted.each { |p| @parts << p } | ||
end | ||
|
||
private | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll want to implement
respond_to_missing?
as well.Given all the boilerplate needed to implement a delegate, consider using stdlib delegate directly. May be simpler.