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 |
linkyndy
left a comment
There was a problem hiding this comment.
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.
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.
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
endI couldn't see a simpler way other than that.
There was a problem hiding this comment.
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.
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.
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.
cool! I didn't know about this parenthesis rule for block variables!
There was a problem hiding this comment.
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_keyscopied from the activesupport source codeI chose to not add
activesupportas a dependency because it is good to have the least number of dependencies!HashUtils.deep_transform_keysto transform all the keys of the hash generated byas_jsonto a StringI chose to add the option
stringify_keyson#as_jsonfor backward compatibility. I also added info about it on README