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

Feature/issue 16 #21

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

Conversation

bdfinlayson
Copy link

This PR introduces support for creating SirenClient instances with objects containing either stringified or symbolized keys. Please see #16 for more information.

@coveralls
Copy link

coveralls commented Nov 30, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 7215db9 on bdfinlayson:feature/issue-16 into 177176c on cha55son:master.

@coveralls
Copy link

coveralls commented Dec 4, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 8bc14c4 on bdfinlayson:feature/issue-16 into 177176c on cha55son:master.

@@ -10,16 +10,16 @@ def initialize(data, config={})
if data.class != Hash
raise ArgumentError, "You must pass in a Hash to SirenClient::Action.new"
end
@payload = data
@payload = data.deep_symbolize_keys
Copy link
Owner

Choose a reason for hiding this comment

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

Did a quick search and I couldn't find this method anywhere (other than rails). It doesn't look like it's in the ruby stdlib.

Copy link
Owner

Choose a reason for hiding this comment

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

O, okay. It's coming from the ActiveSupport dependency.

@@ -48,7 +48,7 @@
expect(client).to be_a SirenClient::Entity
end
it 'to access properties' do
expect(client.properties['name']).to eq('Test 1')
Copy link
Owner

Choose a reason for hiding this comment

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

Unfortunately we can't change the spec files (besides adding to them) or we risk breaking backwards incompatibility. I know some usages of this library that depend on the string key when accessing things like properties. It would be great to access the keys both ways but wasn't a requirement of the ticket. (I'm merely saying this so you don't feel obligated to rewrite this, I think you could revert the spec files and be alright)

Copy link
Author

Choose a reason for hiding this comment

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

Ok, good to know. I'll revert that. There's another activesupport method, with_indifferent_access, that would allow the use case you're describing. That also should allow for reverting changes to the setters like @payload[:name] = xxx back to @payload['name'], while still permitting users to access properties either by payload[:name] or payload['name] or payload.name. Probably should have just reached for with_indifferent_access to begin with. Thoughts @cha55son?

Copy link
Owner

Choose a reason for hiding this comment

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

We'd probably want to go with a more optimized solution (and may be worth pushing to another ticket) as it seems ActiveSupport::HashWithIndifferentAccess has poor performance compared to standard ruby hash lookups. https://gist.github.com/tiagoamaro/c82a27aceedfc901b081

Leaving the output keys as is and allowing input keys of strings/symbols should be sufficient. Regarding the specs, I was wrong. We do want to keep your changes where you create objects with symbols, but we also want to keep the string versions as well.

Copy link
Author

Choose a reason for hiding this comment

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

@cha55son After further reflection I think it makes the most sense to use deep_stringify_keys to handle any symbolized keys. To more easily test both scenarios where the object is initialized with a hash of stringified or symbolized keys, I've extracted the specs for the relevant SirenClient classes into shared examples. In addition to testing the default values, the tests are now also checking that the values are being set as expected. Let me know what you think! Thanks.

@coveralls
Copy link

coveralls commented Dec 16, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling b7809a9 on bdfinlayson:feature/issue-16 into 177176c on cha55son:master.

@coveralls
Copy link

coveralls commented Dec 22, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling dbaaa4e on bdfinlayson:feature/issue-16 into 177176c on cha55son:master.

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

3 participants