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

Proper Notification object comparison #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

cmer
Copy link

@cmer cmer commented Mar 9, 2013

Comparing two objects that evaluate to the same byte representation should be 'true'. Particularly useful in tests/specs.


def ==(other)
self.to_bytes == other.to_bytes
rescue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of rescueing would something like this work?

other.respond_to?(:to_bytes) &&
  self.to_bytes == other.to_bytes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@cmer
Copy link
Author

cmer commented Mar 10, 2013

No because to_bytes can throw an exception if the payload is too large. That's why I did it this way.

But I see what you mean. Perhaps we should actually do both.

@vanstee
Copy link
Member

vanstee commented Mar 11, 2013

Oh yeah good call. That's got me thinking now. What if you want to check equality of 2 invalid notifications? Comparing instance variables might be better than the result of to_bytes. Thoughts?


def ==(other)
other.respond_to?(:to_bytes) && self.to_bytes == other.to_bytes
rescue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we limit this to only rescuing Grocer::PayloadTooLargeError?

@cmer
Copy link
Author

cmer commented Apr 3, 2013

Added type comparison. Would love to see this merged so I can remove my local monkey patch.

def ==(other)
other.respond_to?(:to_bytes) &&
self.to_bytes == other.to_bytes &&
self.class == other.class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the Class is the same, they should both respond_to?(:to_bytes), no?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they're different, they might still both respond_to?(:to_bytes), which is why I make the check

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm saying is, you could simplify this to just be

def ==(other)
  self.class === other &&
  self.to_bytes == other.to_bytes
end

@stevenharman
Copy link
Member

comparing instance variables might be better than the result of to_bytes.

Agreed. Since to_bytes can raise when invalid, rendering any two invalid notifications NOT equal, even if they are... maybe we should be comparing the important state? For example, comparing their encoded_payload should suffice, no?

@vanstee
Copy link
Member

vanstee commented Apr 3, 2013

Yeah I agree that serializing the data is not important for equality.

@stevenharman
Copy link
Member

Oh wait... there is more to the state than just the payload... like the :identifier, :device_token, etc. Hmm...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants