-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add the option stringify_keys to #as_json #153
Conversation
The Travis tests fail because |
This class is only instance inside mp3_parser to call #to_h
@martijnvermaat done! I will create another PR with these commits |
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.
Nice effort, Fabio! 🎉
Clean code, nice approach, concise tests. Keep it up!
spec/attributes_json_spec.rb
Outdated
|
||
result = FormatParser::ZIPParser.new.call(fi_io).as_json(stringify_keys: true) | ||
expect( | ||
result['entries'].flat_map { |entry| entry.keys.map(&:class) }.uniq |
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.
I would suggest making the expectations a bit less "smart". When dealing with tests it is usually a much more pleasant experience to be visually decoding the code under test, and not the test itself. In this instance the amount of functional sugar used occludes the fact that we want to verify that all keys we are scanning are kind_of(String)
. Can we use a more pedestrian each_pair
(or alike) to the same effect?
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.
Sure, I agree the tests should be less "smart"!
Are you suggesting something like this?
result['entries'].each do |entry|
entry.each do |key, value|
expect(key).to be_a(String)
end
end
I couldn't see a simpler way other than that.
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.
Yep that's perfect! If you opt for #each
you might want to group the key-value using ()
though 😉
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.
From what I've seen, each_pair
is an alias to each
, isn't it?
you might want to group the key-value using () though
I didn't get it. Could you give me an example pls?
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.
Sure! I think some_hash.each do |(key, value)|
is required instead of some_hash.each do |key, value|
so that the array argument to the block is expanded into two variables. Or you can use each_pair
instead
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.
cool! I didn't know about this parenthesis rule for block variables!
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.
Sorry for the long discussion about it, but I found interesting to know how it works and I think it's good to share it with you!
From what I saw, it is not necessary to use the ()
here. It would be necessary if the hash contained arrays as values and I wanted to access their values like this:
In this case, there are no arrays inside the hashes, and I just want to check their keys, so it makes no difference using ()
:
And also makes no difference using each
or each_with_pair
given they are aliases!
219fd89
to
a000efa
Compare
tks @julik for all the comments!! |
🎩 My pleasure! Will approve once tests are green |
I messed up a test! Now that I fixed it, it will be green! :) |
Hi folks! This is my 1st PR in this project.
If I did something wrong, please tell me!!
Motivation
fix #151
Proposal
HashUtils.deep_transform_keys
copied from the activesupport source codeI chose to not add
activesupport
as a dependency because it is good to have the least number of dependencies!HashUtils.deep_transform_keys
to transform all the keys of the hash generated byas_json
to a StringI chose to add the option
stringify_keys
on#as_json
for backward compatibility. I also added info about it on README