Skip to content
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

Refactor Mail Helper Array Assignments #95

Closed
thinkingserious opened this issue Sep 15, 2016 · 6 comments
Closed

Refactor Mail Helper Array Assignments #95

thinkingserious opened this issue Sep 15, 2016 · 6 comments
Labels
status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap

Comments

@thinkingserious
Copy link
Contributor

This issue comes from Twitter: https://twitter.com/mootpointer/status/776305972339838976

Reference code: https://github.com/sendgrid/sendgrid-ruby/blob/master/lib/sendgrid/helpers/mail/mail.rb#L864

def attachments=(attachments)
  @attachments = @attachments.nil? ? [] : @attachments
  @attachments = @attachments.push(attachments.to_json)
end

should be:

def add_attachment(attachment)
  @attachments = @attachments.nil? ? [] : @attachments
  @attachments = @attachments.push(attachment.to_json)
end

This is true for all cases where we assign to an array in the Mail Helper.

Technical details:

  • sendgrid-ruby Version: 4.0
  • Ruby Version: 2.2
@thinkingserious thinkingserious added type: community enhancement feature request not on Twilio's roadmap status: help wanted requesting help from the community labels Sep 15, 2016
@mootpointer
Copy link

Thanks for opening this. Something like the above would be what I would suggest. This should be done in concert with adding deprecation warnings to the existing setter methods, or at least signal that they do not work as standard setter methods.

@swifthand
Copy link

While looking at another issue I noticed that the default checks in all of the array assignments.

There is a bit of idiomatic Ruby used to wrap this @value.nil? ? default_value : @value, that is to place the default in the reader method as an ||= assignment, and have the class (even internally) access the array attribute via the reader method. This removes the need for a nil-or-apply-default check at every single use.

Additionally, because Array#push in Ruby is mutating, the re-assignment back to @attachments is unnecessary. Simply @attachments.push(attachment.to_json) is sufficient.

Applying this both in the case of attachments, it looks like this:

def add_attachment(attachment)
  attachments.push(attachment.to_json)
end

def attachments
  @attachments ||= []
end

The Mail#attachments method will, upon first use, detect that @attachments is falsey and perform the assignment, thus providing it with a default value of []. Subsequent calls will simply return @attachments in whatever state it currently is.

Then, in Mail#add_attachment, we can safely reference @attachments by instead accessing it via the method, and be guaranteed to always receive an Array, which we can then push onto.

@thinkingserious
Copy link
Contributor Author

Loving the feedback @swifthand!

Could you please fill out this form so we can send you some swag?

@swifthand
Copy link

swifthand commented Oct 11, 2016

@thinkingserious Gladly!

Also saw the link to the Mail Helper project. I haven't used Github's new project feature before but I'll give it a look. Does this issue fall under that as well or are these array assignments best addressed separately?

@thinkingserious
Copy link
Contributor Author

Hi @swifthand,

I think this issue belongs in the Mail Helper project. I'm not quite sure how to best manage that yet, we are new to the Projects feature too, kicking the tires.

My guess is that we will swarm on the requirements, agree on a scope of work, break up the tasks into issues and add them to the project's backlog.

@thinkingserious
Copy link
Contributor Author

Hello Everyone!

Please follow progress here: #86

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: community enhancement feature request not on Twilio's roadmap
Projects
None yet
Development

No branches or pull requests

3 participants