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

enables yaml generation to drop keys with blank values #37

Closed
wants to merge 2 commits into from

Conversation

Uepsilon
Copy link

@Uepsilon Uepsilon commented Jun 18, 2020

I've seen that there was a partial solution to dropping keys with blank values but no way to use this via configuration. So I added one.

Also, since it's named remove_empty_keys I've changed the behavior so that keys with blank / empty values get removed instead of only focussing on nil values.

default behavior falls back to the way it was

@MichaelHoste
Copy link
Member

MichaelHoste commented Jun 21, 2020

Thanks for the pull request @Uepsilon, it's greatly appreciated!

I will probably merge it at the end of this week and bump the version.

In the meantime, I have just 2 questions for you.

  1. When you say:

I've seen that there was a partial solution to dropping keys with blank values

I'm not sure what partial solution you are talking about. Could you please tell me what you are referring to?

  1. Out of curiosity, why do you want to drop keys with blank value?

@Uepsilon
Copy link
Author

Uepsilon commented Jul 7, 2020

By partial solution I meant, that there is an option for the to_hash method, which already drops keys with empty values but it seems to be a dead option as it cannot be set anywhere.

reference: https://github.com/translation/rails/blob/master/lib/translation_io/flat_hash.rb#L10

     def to_hash(flat_hash, remove_empty_keys = false)
        hash = {}

        if remove_empty_keys
          flat_hash = flat_hash.reject { |k, v| v.nil? && !k.end_with?(']') }
        end

        flat_hash.each_pair do |key, value|
          build_hash_with_flat(hash, key, value)
        end

        joker_to_brackets!(hash)
      end

Since the key is called remove_empty_keys - which is also the behavior I was looking for - I've changed it to not only drop nil values but also everything that is empty.

We wanted to get rid of the empty values as we have quiet a lot of values that are only translated to certain languages but not all of them. For example things like fallback for britsh / american english.

@MichaelHoste
Copy link
Member

Hi @Uepsilon,

I merged your code and added some tests here: 93a663c

I bumped the gem version, so the new option should already be accessible in Version 1.22.

Thanks a lot for your suggestion and your help!


We wanted to get rid of the empty values as we have quiet a lot of values that are only translated to certain languages but not all of them. For example things like fallback for britsh / american english.

You made me doubt it so I double-checked. An empty string ("") will fallback correctly to the main language, but a string with only spaces (" ") will not. This new option will help for this specific case.

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

Successfully merging this pull request may close these issues.

2 participants